Repository navigation
Description
Problem
Async\signal() behaves non-deterministically when called from multiple OS threads simultaneously. On POSIX systems, a process-directed signal (e.g. SIGTERM, SIGINT) is delivered to exactly one thread chosen by the OS — all other threads that registered a handler via signal() never receive it, and their Future stays pending forever.
Reproduction Scenario
// Main thread
$pool = new ThreadPool(workers: 2, coroutine: true);
// Thread 1 — registers signal handler
$pool->submit(function (): void {
await(signal(Signal::SIGTERM)); // may never resolve
echo "Thread 1: got SIGTERM\n";
});
// Thread 2 — also registers signal handler
$pool->submit(function (): void {
await(signal(Signal::SIGTERM)); // may never resolve
echo "Thread 2: got SIGTERM\n";
});
// Send SIGTERM to the process → only ONE of the two handlers fires.
// The other hangs forever.With spawn_thread() the same problem appears: if both the main coroutine and a spawned thread call signal(SIGTERM), the signal is delivered to one — the other is silently lost.
Root Cause
POSIX (IEEE Std 1003.1) §2.4.1:
"Signals generated for the process shall be delivered to exactly one of those threads within the process which has the signal unblocked."
The OS selects the recipient thread arbitrarily. There is no broadcast. A signal() call in Thread B simply loses the race if Thread A is selected.
Impact
- Queue workers that run signal handlers in worker threads (e.g. for graceful shutdown) cannot rely on
signal()— the shutdown signal may be swallowed by a thread that does nothing with it. - Supervisor/worker patterns where both the supervisor coroutine and a worker thread await
SIGTERMare silently broken.
Expected Behavior
One of the following solutions:
Option A — Process-wide signal broadcast
When multiple coroutines/threads register signal() for the same signal, all registered handlers are notified (fan-out). The runtime intercepts the signal in the main thread and broadcasts to all waiters across all threads.
SIGTERM → main thread scheduler → broadcast → Thread 1 Future resolves
→ Thread 2 Future resolves
→ coroutine Future resolves
Option B — Restrict signal() to main thread only
Document and enforce that signal() may only be called from the main thread. Throw Async\SignalException if called from a spawned thread. Provide a ThreadChannel-based pattern for propagating signals to worker threads.
Option C — Global signal registry
Provide a process-level signal multiplexer that any thread can subscribe to:
// any thread
Async\Signal::subscribe(Signal::SIGTERM, function (): void {
// called in every subscriber regardless of which thread received the signal
});Current Workaround
Register signal() only in the main thread, then propagate shutdown via ThreadChannel:
// main thread only
spawn(function () use ($shutdownChannel): void {
await(signal(Signal::SIGTERM));
$shutdownChannel->send(true); // notify all worker threads
});This works but is not obvious, undocumented, and easy to get wrong.
Environment
- Multi-threaded programs using
spawn_thread()orThreadPool - POSIX systems (Linux, macOS)
- Affects graceful shutdown patterns
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
#109: tests for multi-thread signal delivery + CHANGELOG
Merge pull request #116 from true-async/109-signal-delivery-is-non-de…
#109: 011 — don't print raw signal number, it differs on Darwin/BSD
#138 fuzzy-tests: Async\signal() chaos coverage (#109 backstop)
fix(signal): #109 reactor-owned zend_sigaction delegation + valid sig…