Skip to content

Fix GH-25548: Execution timeout on a thread that never ran PHP is lost - #25581

Open
phcorp wants to merge 3 commits into
php:PHP-8.4from
phcorp:fix-gh25548
Open

phcorp wants to merge 3 commits into
php:PHP-8.4from
phcorp:fix-gh25548

Conversation

@phcorp

@phcorp phcorp commented Nov 19, 2026 •

Copy link
Copy Markdown

Depends on #25580 (GH-25541); only the last commit is new.

Under ZTS where the execution timer is process-wide (setitimer(): macOS, FreeBSD, not Linux, which uses per-thread timers), the timer signal can fire on a thread that never ran PHP. Without zend signals, zend_timeout_handler() prints "called in a thread not managed by PHP" and returns, so the timeout is lost. Because it was installed with SA_RESETHAND, the next signal kills the process.

Reproducer (ZTS, --disable-zend-signals, macOS arm64): set_time_limit(30); zend_test_raise_on_foreign_thread(SIGALRM); while (true);. The timeout is lost, and the next timer signal kills the process instead of "Maximum execution time of 30 seconds exceeded" being reported.

Fix: under ZTS, install the timeout handler with SA_SIGINFO and without SA_RESETHAND. On a thread without TSRM, re-route the signal with the helper from GH-25541; with zend signals, that already happens. A PHP thread that has no timeout armed now ignores the timer signal. Before this, a stale signal arriving after set_time_limit(0) reported "Maximum execution time of 0 seconds exceeded". This replaces the 2-second window from #25542.

Not covered: setitimer() gives the process one timer, so with several PHP threads the last set_time_limit() wins, and the signal may reach a PHP thread other than the one that armed it. That thread ignores it if it has no timeout of its own, so the other thread's timeout is lost. This is the same as today; only per-thread timers (ZEND_MAX_EXECUTION_TIMERS, Linux only) fix it.

Questions for reviewers:

  1. When a PHP thread without a timeout gets the signal, is ignoring it right, or would you prefer a hard error?
  2. Should this go to 8.4, or only to master?

…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.
A signal whose handler was set back to SIG_DFL kept being sent on from a
thread that never ran PHP. If every PHP thread blocked it, the signal
stayed pending instead of taking its default action.

Count the PHP threads whose handler is neither SIG_DFL nor SIG_IGN, and
only re-route while that count is positive. The count follows every
write to a thread's handlers and drops when a thread's storage is freed.

zend_test_start_foreign_thread() starts a thread that never runs PHP and
stays alive, so a test can block the signal on the PHP thread only.
…lost

Under ZTS without zend signals, the process-wide execution timer can fire on
a thread that has no TSRM storage. zend_timeout_handler() printed a message
and returned, and as it was installed with SA_RESETHAND, the next signal
killed the process.

Install the handler with SA_SIGINFO and without SA_RESETHAND under ZTS, and
re-route the signal from such a thread to the PHP threads, as phpGH-25541 does
for pcntl. With zend signals, the timeout handler is already re-routed.

A PHP thread that has no timeout armed ignores the signal: it is stale (the
timer was disarmed) or belongs to another thread's timer.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant