[php-src] PR #25580: Fix GH-25541: Signal on a thread that never ran PHP crashes or kills the process
| From: | phcorp | Date: | Thu, 19 Nov 2026 08:12:42 +0000 |
| Subject: | [php-src] PR #25580: Fix GH-25541: Signal on a thread that never ran PHP crashes or kills the process | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-40801@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/25580
Author: phcorp
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:
- with
--disable-zend-signals, pcntl_signal_handler() reads
PCNTL_G() through a NULL TSRM pointer and crashes;
- with zend signals (the default), zend_signal_handler_defer() falls back to the
original disposition, so SIG_DFL kills 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 after sigreturn) and sends it to the process again with kill(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 and
abort() 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:
- It changes the signal mask of threads the embedder owns, for signals PHP handles. At request
start, PHP threads unblock re-routed signals, as zend_sigaction() already does for the
signals it registers.
- siginfo of the delivered signal names this process as the sender.
- The PHP thread that receives the signal is not necessarily the one that registered the handler
(GH-25549).
- On macOS, a signal re-sent while every thread blocks it is attached to the main thread, so if the
main thread doesn't run PHP it is lost. Linux and FreeBSD keep it pending process-wide. Also on
macOS, SIGSEGV and SIGBUS sent with 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:
1. Is changing a foreign thread's mask acceptable for embedders, or should it be opt-in?
2. This adds three ZEND_API functions 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?