php-src icon indicating copy to clipboard operation
php-src copied to clipboard

ZTS with --disable-zend-signals: execution timeout delivered to a non-PHP thread is lost and kills the process

Open phcorp opened this issue 1 week ago • 2 comments

Description

On a ZTS build configured with --disable-zend-signals, the execution timeout signal can be delivered to a thread that never ran PHP, such as a libdispatch worker on macOS. zend_timeout_handler() then prints a message and returns without setting EG(timed_out), so the timeout is lost. The handler is installed with SA_RESETHAND, so the signal's disposition is back to SIG_DFL and the next one terminates the process.

The script below needs zend_test_raise_on_foreign_thread(), which GH-25542 adds to zend_test. It raises the signal on a new thread, which makes the delivery deterministic.

<?php
set_time_limit(1);
zend_test_raise_on_foreign_thread(SIGALRM);
$end = hrtime(true) + 3_000_000_000;
while (hrtime(true) < $end);
echo "no timeout\n";

Resulted in this output (5 of 5 runs, process terminated by SIGALRM):

zend_timeout_handler() called in a thread not managed by PHP. The expected signal handler will not be called. This is probably a bug.

But I expected this output instead:

Fatal error: Maximum execution time of 1 second exceeded in %s on line %d

With zend signals enabled, GH-25542 forwards the signal to the PHP thread and the script produces the expected fatal error. Without zend signals, only pcntl_signal_handler() forwards. zend_timeout_handler() does not, so this configuration is not covered.

SIGALRM is the timeout signal on macOS arm64. I have only tested this platform.

PHP Version

PHP-8.4 branch (8.4.29-dev), debug ZTS build with --disable-zend-signals, with GH-25542 applied for the test helper

Operating System

macOS (arm64)

phcorp avatar Nov 18 '26 09:11 phcorp

GH-25542 now includes a fix for this issue, with a regression test.

phcorp avatar Nov 18 '26 15:11 phcorp

Fix proposed in #25581, on top of #25580. The timer signal is re-routed the same way, the timeout handler no longer uses SA_RESETHAND under ZTS, and a PHP thread without a timeout ignores a stale timer signal.

phcorp avatar Nov 19 '26 08:11 phcorp