[php-src] PR #13551: Introduce new zend_module_entry hook: child_startup_func
| From: | arnaud-lb | Date: | Wed, 28 Feb 2024 16:29:05 +0000 |
| Subject: | [php-src] PR #13551: Introduce new zend_module_entry hook: child_startup_func | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-25192@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/13551
Author: arnaud-lb
Starting threads in
request_startup_func (MINIT) is unsafe because SAPIs
may fork after this hook. Yet, some modules may start threads as part of their initialisation and
have no easy way to safely do so.
For example, the curl module calls curl_global_init() in
request_startup_func to initialize libcurl, but this function starts
threads at least on MacOS.
This change introduces a new hook: child_startup_func (CHINIT). This hook
is executed once per request-handling process, which are assumed to be non-forking processes.
Modules should use this hook for their thread-creating initialization.
This fixes a crash observed in https://github.com/php/php-src/pull/13468, and
follows an idea from @bukka.
NB: Forking a process with threads may leave child processes in a corrupted state and lead to
crashes or deadlocks. In the case of https://github.com/php/php-src/pull/13468,
curl was indirectly using libdispatch during MINIT, which created threads,
and the library defensively [crashed child
processes](https://github.com/apple/swift-corelibs-libdispatch/blob/1c0abe710bbbf14693f8750248b598f26cba9e75/src/queue.c#L7440-L7450)
trying to use it again, after that. This problem is will described
[here](https://www.evanjones.ca/fork-is-dangerous.html) and
[here](https://rachelbythebay.com/w/2011/06/07/forked/).