Repository navigation
Conversation
| #endif | ||
| ZEND_GET_MODULE(pcntl) | ||
| #endif | ||
|
|
There was a problem hiding this comment.
diff --git a/Zend/zend_signal.c b/Zend/zend_signal.c
index 83f8aa0852d..d4df3cbcd0a 100644
--- a/Zend/zend_signal.c
+++ b/Zend/zend_signal.c
@@ -79,6 +79,11 @@ static const int zend_sigs[] = { TIMEOUT_SIG, SIGHUP, SIGINT, SIGQUIT, SIGTERM,
static zend_signal_entry_t global_orig_handlers[NSIG];
static sigset_t global_sigmask;
+#ifdef ZTS
+static pthread_t global_handler_threads[NSIG];
+static zend_atomic_bool global_handler_thread_set[NSIG];
+#endif
+
/* {{{ zend_signal_handler_defer
* Blocks signals if in critical section */
static void zend_signal_handler_defer(int signo, siginfo_t *siginfo, void *context)
@@ -91,6 +96,11 @@ static void zend_signal_handler_defer(int signo, siginfo_t *siginfo, void *conte
* Or it could be delivered to a thread that didn't execute PHP yet.
* In the latter case we act as if SIGG(active) is false. */
if (tsrm_is_shutdown() || !tsrm_is_managed_thread()) {
+ if (!tsrm_is_shutdown() && zend_atomic_bool_load(&global_handler_thread_set[signo-1])) {
+ pthread_kill(global_handler_threads[signo-1], signo);
+ errno = errno_save;
+ return;
+ }
/* Forward to default handler handler */
zend_signal_handler(signo, siginfo, context);
return;
@@ -245,6 +255,18 @@ ZEND_API void zend_sigaction(int signo, const struct sigaction *act, struct siga
SIGG(handlers)[signo-1].handler = (void *) act->sa_handler;
}
+#ifdef ZTS
+ if (SIGG(handlers)[signo-1].handler == (void *) SIG_DFL || SIGG(handlers)[signo-1].handler == (void *) SIG_IGN) {
+ if (zend_atomic_bool_load(&global_handler_thread_set[signo-1])
+ && pthread_equal(global_handler_threads[signo-1], pthread_self())) {
+ zend_atomic_bool_store(&global_handler_thread_set[signo-1], false);
+ }
+ } else {
+ global_handler_threads[signo-1] = pthread_self();
+ zend_atomic_bool_store(&global_handler_thread_set[signo-1], true);
+ }
+#endif
+
memset(&sa, 0, sizeof(sa));
if (SIGG(handlers)[signo-1].handler == (void *) SIG_IGN) {
sa.sa_sigaction = (void *) SIG_IGN;
@@ -357,6 +379,15 @@ void zend_signal_deactivate(void)
* state that is reset below will not be accessed. */
*((volatile int *) &SIGG(active)) = 0;
+#ifdef ZTS
+ for (int signo = 1; signo < NSIG; signo++) {
+ if (zend_atomic_bool_load(&global_handler_thread_set[signo-1])
+ && pthread_equal(global_handler_threads[signo-1], pthread_self())) {
+ zend_atomic_bool_store(&global_handler_thread_set[signo-1], false);
+ }
+ }
+#endif
+
SIGG(running) = 0;
SIGG(blocked) = 0;
SIGG(depth) = 0;There was a problem hiding this comment.
Thanks, you were right. On a default ZTS build, a signal on a foreign thread killed the process even with a pcntl handler. I applied your change, with two additions: faults (SIGSEGV, SIGBUS, SIGFPE, SIGILL) are not forwarded, since the faulting thread would otherwise spin forever, and signals at or above NSIG are not tracked. pcntl now tracks the owner per signal too, and the test runs with zend signals enabled.
There was a problem hiding this comment.
Update: I replaced this approach after testing it further. Calling pthread_kill() from the signal handler aborts the process on macOS when the interrupted thread holds a libpthread lock, which a stress test hit in 8 of 8 runs. The handler now only sets a flag and writes to a pipe, and a helper thread does the pthread_kill(). The PR description explains the design and its limitations.
There was a problem hiding this comment.
Thanks for the review, and sorry this PR grew so large.
Your pthread_kill()-from-the-handler version was right about the bug, but I couldn't keep it. On macOS it aborts with "Trying to recursively lock an os_unfair_lock" when the interrupted thread holds a libpthread lock: pthread_kill() isn't async-signal-safe there. With glibc it can also hang in a child forked from a multithreaded parent. The helper thread I used instead works, but it is far too much code for 8.4.
So I'm proposing a smaller scope, in two PRs against PHP-8.4:
- Fix GH-25541: Signal on a thread that never ran PHP crashes or kills the process #25580 fixes ext/pcntl (ZTS): a process-directed signal delivered to a non-PHP thread crashes in pcntl_signal_handler #25541 only, in about 160 lines of C (plus about 60 lines of comments) and three phpt files. On a thread without TSRM, for a signal PHP handles and the embedder doesn't, the handler adds the signal to the mask saved in its ucontext and sends it to the process again with
kill(getpid(), signo). The kernel then delivers it to a thread that doesn't block it, which converges on a PHP thread. There's no new thread and no lock, andkill()is async-signal-safe. I measured it on Linux glibc and musl (arm64 and amd64), FreeBSD 15.1 arm64 and macOS arm64. The one gap is on macOS: a signal re-sent while every thread blocks it is attached to the main thread, so it is lost if the main thread doesn't run PHP. - Fix GH-25548: Execution timeout on a thread that never ran PHP is lost #25581 builds on it for the execution timer (ZTS with --disable-zend-signals: execution timeout delivered to a non-PHP thread is lost and kills the process #25548), in about 40 lines of C, without the 2-second heuristic.
GH-25549 (several PHP threads with different handlers) stays open; it needs process-wide handler state, as discussed in GH-10193. I've closed this PR in favour of the two new ones; happy to reopen it if you'd rather keep the discussion here.
Under ZTS with --disable-zend-signals, 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, including one with no TSRM storage, such as the libdispatch workers of every macOS process. PCNTL_G() then dereferences NULL. Mirror zend_signal_handler_defer(): when the thread is not managed by TSRM, leave the pcntl globals alone and forward the signal with pthread_kill() to the thread that last installed a handler.
On a default ZTS build, a signal delivered to a thread that never ran PHP reached zend_signal_handler(), which used the original disposition. A signal with a pcntl handler or the execution timeout therefore ended the process. Record the thread that installed each handler in zend_sigaction() and forward the signal to it. Track the thread per signal in pcntl too. With a single slot, a signal could be sent to a thread that had no handler for it, and the end of any request that installed a handler stopped forwarding for the other threads. Never forward SIGSEGV, SIGBUS, SIGFPE or SIGILL. The faulting instruction would run again when the handler returned, so the thread would spin forever. Run the test with zend signals as well, as no CI job disables them.
SIGTRAP can be synchronous too: the trapping instruction runs again when the handler returns, so the thread would spin forever. A handler installed before PHP started used to receive the signals delivered to threads that never ran PHP. Forwarding them to a PHP thread bypassed it, and could end the request with a bogus execution timeout.
When PHP starts again in the same process, the handler left by the previous startup was saved as the original one. A signal delivered to a thread that never ran PHP then made zend_signal_handler_defer() call itself until the stack overflowed.
The thread and its flag were two separate variables, updated without any synchronization: - a thread finishing its request could erase the record of a thread that had just installed a handler, so that the signal then took the default action; - pthread_kill() could be given a thread that had already exited; - after fork(), the child could forward to a thread that does not exist there. Keep one table in Zend, used with and without zend signals, protected by a spinlock that is held across pthread_kill(). A thread forgets its signals before it exits, which waits for any forward in progress. The lock holds the pid of its holder, so that a forked child can take over a lock whose holder does not exist there. pthread_atfork() handlers are avoided on purpose: holding the lock across fork() deadlocks with a forwarding thread interrupted while it holds an allocator lock. pcntl_fork() keeps the record of the forking thread in the child. Forget a signal before its handler goes away, and record it before the handler is installed, so that a forwarded signal does not take the default action. Skip the fault test under sanitizers, which handle faults themselves.
Calling pthread_kill() from a signal handler aborts the process on macOS when the interrupted thread holds the libpthread lock that it takes, and the spinlock made the handler wait for other threads. The handler now only sets an atomic flag and writes to a pipe. A helper thread calls pthread_kill() in normal context, under a mutex that a thread also takes to remove its record before it exits. A thread-specific key removes the record of a thread that exits without shutting down a request. Each thread that handles a signal is recorded, so that the next one takes over when the owner leaves. Do not read thread-local storage on a thread that never ran PHP, which can allocate on first access when PHP is a shared library, and keep the original handlers saved by an earlier startup.
A signal delivered to a PHP thread that has no handler for it is now forwarded to a thread that does, instead of taking the default action or being lost. Without zend signals, the disposition of a signal is process-wide, so it is only reset when no other thread handles the signal. The change and the registrations are kept consistent under the forwarding lock. Use tsrm_sigmask() in pcntl_signal_dispatch(), as sigprocmask() changes the mask of every thread on macOS.
Without zend signals, zend_timeout_handler() printed a message and lost the timeout, and SA_RESETHAND left the default action for the next signal. Forward the signal to the thread that armed the timer instead, and keep the handler installed under ZTS.
Cover a signal delivered to a thread that never ran PHP, handover between the threads that handle a signal, an owner that exits, concurrent registration and fork, with and without zend signals. No job tested the build without zend signals, so use it in the nightly variation job.
Recording every zend_sigaction() started the helper thread in each ZTS request, which makes unshare(CLONE_NEWUSER) fail. Register pcntl handlers and running execution timers explicitly instead. A timer does not keep a signal disposition in place for other threads, so that a thread cannot keep forwarding a signal to itself. When the owner has left, send the signal to the process again so that it takes its current action, and hand over a signal that the leaving thread blocks. Do not let one thread's SIG_IGN disable the handler of another. Fix double frees when a thread exits during shutdown or in a forked child, and synchronize the initialization check.
Keep the forwarding infrastructure in Zend/zend_signal_forward.c, with a small API, and define its policy: the owner of a signal is the thread that most recently registered a handler for it, or otherwise a thread with an armed timer. Do not call raise(), fprintf() or SIGG() on a thread without TSRM storage. Timer signals carry a generation, so a forward delayed past a re-armed timer is dropped, and the signal of a timer that was just disarmed no longer ends the process. A timer signal reaching a thread without a time limit goes to the thread that armed the timer. Without an owner, a signal takes its default action instead of being lost in a forked child. Releasing a handler keeps the disposition that an armed timer or a thread ignoring the signal still needs. Requests that register nothing no longer take the forwarding mutex. A forked child uses a new mutex instead of initializing a possibly locked one.
A handler now only writes to the pipe when it sets a pending flag from zero, and a write error other than EAGAIN clears the flag again instead of reporting success. The helper scans the flags when it starts, and a helper that lost its pipe can be replaced by the next registration. A forked child no longer reads the inherited thread list, which another thread may have been updating, and drops the timer of the calling thread, as timers are not inherited. A pcntl_rfork() child sharing the file descriptor table of its parent no longer closes the pipe of the parent. When a thread exits during a request, stop its timer and do not leave its handler installed, which made another thread forward a signal to itself forever. Start the timer after its owner is recorded, and only use forwarding where atomics are lock-free.
A read that failed after consuming a notification lost the signal, so the helper now replaces its pipe and scans again. The signal of a timer that was disarmed while the helper stalled is no longer sent again, the grace period survives 2038, and the pipe stays clear of the standard streams. The helper exits after a minute without anything to forward, and the next registration restarts it. Debug builds get a hook to reach these states deterministically. The tests no longer disable LeakSanitizer or skip AddressSanitizer.
The execution timer checked for a handler, installed its own and recorded itself in separate steps. A pcntl_signal() call in between was overwritten, and a handler released in between left the running timer with the default action, which ended the process when it expired. These steps now happen in one critical section. A thread that started to exit can no longer register again, the helper stops before shutdown stops tracking exiting threads, and a failed initialization in a forked child no longer makes a handled signal fatal. Debug builds can now stall a thread inside the signal handler and make the helper's startup fail, which covers races and failure paths that no test could reach before.
00f482d to
d5ce545
Compare
The helper now runs from first use until shutdown. If its pipe fails, it delivers what is pending and starts a new helper with a new pipe. This removes the idle timeout and the races that came with it. When pcntl_unshare() or pcntl_setns() fail with EINVAL, they retry with the helper stopped, as the kernel may require a process with a single thread. Forwarding is only compiled on systems where pthread_getspecific() was checked to be safe in a signal handler. A forked child can now arm the execution timer over a handler inherited from a thread that does not exist there, and shutdown no longer waits for a helper that cannot be woken up. The test scenarios move to their own file, and ext/pcntl no longer changes php_signal.c.
This PR is superseded. Its helper thread and pipe (about 1,300 lines) were too much for a stable branch.
The branch is kept for reference.