Skip to content

signal(): delivery is non-deterministic in multi-thread programs #109

Closed

Description

@EdmondDantes

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 SIGTERM are 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() or ThreadPool
  • POSIX systems (Linux, macOS)
  • Affects graceful shutdown patterns

Activity

  1. self-assigned this
    on May 10, 2026
  2. added
    bug
    on May 10, 2026
  3. changed the issue type fromtoon May 10, 2026
  4. added this to the TrueAsync 0.7.0 milestone on May 10, 2026
  5. moved this from Todo to In Progress in True Async Boardon May 10, 2026
  6. added a commit that references this issue on May 11, 2026
  7. added a commit that references this issue on May 11, 2026
  8. moved this from In Progress to Done in True Async Boardon May 11, 2026
  9. added a commit that references this issue on May 11, 2026
  10. added a commit that references this issue on May 24, 2026
  11. added a commit that references this issue on Jul 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Type

Projects

Relationships

None yet

Development

No branches or pull requests

Issue actions