[php-src] PR #25542: Fix GH-25541: pcntl handler crashes on a thread that never ran PHP
| From: | phcorp | Date: | Tue, 17 Nov 2026 23:19:51 +0000 |
| Subject: | [php-src] PR #25542: Fix GH-25541: pcntl handler crashes on a thread that never ran PHP | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-40754@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/25542
Author: phcorp
Fixes GH-25541.
Under ZTS with
--disable-zend-signals (Homebrew's php-zts, for one),
pcntl_signal_handler() is installed directly with sigaction(). A
process-directed signal (kill(), alarm()) may be delivered to any thread
that does not block it. Every macOS process has libdispatch worker threads, and they have no TSRM
storage, so PCNTL_G(spares) dereferences NULL. It is more likely than it sounds:
pcntl_signal_dispatch() blocks all signals on the PHP thread while handlers run, so a
signal arriving then *must* go elsewhere.
The fix mirrors what zend_signal_handler_defer() already does for the zend-signals
build. When !tsrm_is_managed_thread(), the handler leaves the pcntl globals alone. It
then forwards the signal with pthread_kill() (async-signal-safe) to the thread that
last installed a pcntl handler. That thread's own handler then queues it as usual; if the
thread is blocking signals at that moment, the signal stays pending until it unblocks. The recorded
thread is cleared in RSHUTDOWN.
Trade-off: a forwarded signal's siginfo describes the pthread_kill(),
not the original sender.
**Test.** zend_test_raise_on_foreign_thread() (ZTS, non-Windows) runs
raise() on a fresh pthread. Per POSIX, that delivers the signal to that thread, so the
test is deterministic. It is skipped where zend signals are enabled, since those builds already
route a foreign-thread signal before pcntl sees it. On a debug ZTS
--disable-zend-signals build on macOS arm64:
- Without the fix, the test never completes. lldb shows thread #2 stopped with EXC_BAD_ACCESS
(address=0x0) at pcntl.c:1300, PCNTL_G(spares).
- With the fix, it passes 20 of 20 runs. ext/pcntl/tests and
ext/zend_test/tests have no failures.
- The 20 s kill -ALRM storm from the issue survives 4 of 4 runs.
Targeting PHP-8.4, as the bug is present in every maintained branch.