Repository navigation
Conversation
…ls the process Under ZTS, a process signal can be delivered to a thread that has no TSRM storage, e.g. a Go runtime thread in FrankenPHP or any thread an embedder creates. pcntl_signal_handler() then read PCNTL_G() through a NULL pointer (--disable-zend-signals), and zend_signal_handler_defer() fell back to the original disposition, so SIG_DFL killed the process although the script registered a handler. On such a thread, for a signal PHP handles, add the signal to the mask saved in the interrupted context and send it to the process again. The kernel delivers it to a thread that does not block it, which converges on a PHP thread. Threads unblock re-routed signals when they start a request. Faults and abort() keep their default action on the thread they hit. Re-routing is enabled on Linux, macOS and FreeBSD, where it was verified.
phcorp
requested review from
devnexen,
dstogov and
kocsismate
as code owners
November 19, 2026 08:12
This was referenced Nov 19, 2026
Member
|
I do not feel particularly confident overall, can this test be tried ? --TEST--
GH-25541 (Signal whose handler was removed keeps its default action on a thread that never ran PHP)
--EXTENSIONS--
pcntl
posix
zend_test
--SKIPIF--
<?php
if (!function_exists('zend_test_start_foreign_thread')) die('skip ZTS only');
?>
--FILE--
<?php
$pid = pcntl_fork();
if ($pid === 0) {
pcntl_signal(SIGUSR1, function () {});
pcntl_signal(SIGUSR1, SIG_DFL);
zend_test_start_foreign_thread();
/* The PHP thread blocks the signal, so the kernel picks the other thread. */
pcntl_sigprocmask(SIG_BLOCK, [SIGUSR1]);
posix_kill(posix_getpid(), SIGUSR1);
usleep(500000);
echo "child survived", PHP_EOL;
exit(0);
}
pcntl_waitpid($pid, $status);
var_dump(pcntl_wifsignaled($status));
var_dump(pcntl_wifsignaled($status) && pcntl_wtermsig($status) === SIGUSR1);
?>
--EXPECT--
bool(true)
bool(true) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Under ZTS, the kernel may deliver a process signal to any thread that does not block it, including threads that never ran PHP and have no TSRM storage. Examples are the Go runtime threads in FrankenPHP, or any thread an embedding application creates. When that happens:
--disable-zend-signals,pcntl_signal_handler()readsPCNTL_G()through a NULL TSRM pointer and crashes;zend_signal_handler_defer()falls back to the original disposition, soSIG_DFLkills the process even though the script registered a pcntl handler.Reproducer (ZTS, with the new zend_test hook):
pcntl_signal(SIGUSR1, fn() => print "ok\n"); zend_test_raise_on_foreign_thread(SIGUSR1); pcntl_signal_dispatch();. On a stock 8.4 build, the process dies from SIGUSR1, or crashes with--disable-zend-signals.Fix: on a thread without TSRM, for a signal a PHP thread registered a handler for and the embedder did not, the handler adds the signal to the mask saved in its
ucontext(so the thread blocks it aftersigreturn) and sends it to the process again withkill(getpid(), signo). The kernel then delivers it to a thread that does not block it, and foreign threads stop taking it after the first time, so it ends up on a PHP thread. A thread unblocks re-routed signals when it starts a request, which covers threads created by a masked thread. Faults andabort()keep their default action on the thread they hit; signals with no PHP handler, or handled by the embedder, keep the original disposition, as before. There's no new thread and no lock. Re-routing is enabled on Linux, macOS and FreeBSD, where I measured it (glibc and musl, arm64 and amd64; macOS arm64; FreeBSD 15.1 arm64).Not covered:
zend_sigaction()already does for the signals it registers.kill()carry a fault code, so they are treated as faults.GH-25548 (the execution timer) is a separate PR on top of this one.
Questions for reviewers:
ZEND_APIfunctions on 8.4 so pcntl can use them without zend signals. Is that OK on a stable branch, or should pcntl keep its own copy?Merge-up notes (checked locally: applies to 8.5, 8.6 and master; tests pass there with and without zend signals):
# ifdef HAVE_STRUCT_SIGINFO_T/# endifaround the re-route inpcntl_signal_handler(), since ext/pcntl simplification. #13975 removed that macro. Otherwise it still compiles, but signals are dropped with--disable-zend-signals, which no CI job builds.zend_atomic_bool,zend_atomic_bool_load_ex()andzend_atomic_bool_store_ex()becomeatomic_bool,atomic_load()andatomic_store()inZend/zend_signal.c.