Skip to content

fix(terminal): preserve signal masks across concurrent worker spawns - #20

Merged
steipete merged 1 commit into
mainfrom
claude/terminal-worker-threads
Sep 28, 2026
Merged

steipete merged 1 commit into
mainfrom
claude/terminal-worker-threads

Conversation

@steipete

Copy link
Copy Markdown

What does this PR do?

Fixes lost Ctrl-C in Bun.Terminal children on macOS when two threads spawn processes at the same time.

posix_spawn_bun in src/jsc/bindings/bun-spawn.cpp blocks all signals around the spawn and then restores the previous mask. It used sigprocmask, which changes the whole process's mask on Darwin, not just the calling thread's. When two threads (for example two Workers) spawn at the same time, the second saves the first one's temporary all-blocked mask as its "old" mask and later restores it. The process then keeps every signal blocked, and children spawned from then on start with SIGINT blocked. For a terminal child, writing \x03 still generates SIGINT in the kernel, but the foreground program never acts on it. Terminal writes themselves were fine, which made this look like a lost-input bug.

The fix uses pthread_sigmask for the save, block and restore, and for the child's mask before exec. pthread_sigmask is per-thread on every platform. On Linux, glibc and musl already implement sigprocmask per-thread, so behavior there is unchanged. Upstream Bun main still calls sigprocmask here.

How did you verify your code works?

macOS arm64 release build of this branch on openclaw/main 8282146:

  • New terminal-spawn.test.ts case "concurrent Worker terminals preserve child signal masks": two Workers, gated with Atomics so their spawns overlap, each spawn 8 terminal children, send \x03, and wait for the child's INT trap. It times out on the unfixed release build and passes on this one. With the fix it passed 3/3 runs. The original two-worker repro passed 56 iterations per worker.
  • test/js/bun/terminal/ plus spawn, spawn-signal, and exit-code: 323 pass, 29 skip, 0 fail. An earlier run of the same set on a machine at load average about 37 (another Bun build running) hit the 5 s default timeout in the existing "ESRCH while a terminal child exits" case from fix(spawn): avoid blocking macOS ESRCH exit waits #11. Rerun alone at the same load, it passed in 0.4 to 1.1 s, and the next full run was clean, so it is load-sensitive and unrelated to this change.
  • Windows is unaffected (ConPTY, no POSIX signal mask); the new case is skipped there.

Use pthread_sigmask around POSIX spawning. On Darwin, overlapping sigprocmask calls save and restore another worker’s temporary process-wide blocked mask, leaving child SIGINT blocked even though terminal writes succeed.

Add synchronized Worker terminal SIGINT coverage. The regression times out on the unfixed release and passes with this change; the original two-worker repro passes 56 iterations per worker. Release terminal/spawn suites retain only the two baseline internal-for-testing import errors. Windows x64/arm64 Rust type checks pass; runtime validation was macOS arm64.
@steipete
steipete merged commit ccae1e4 into main Sep 28, 2026
6 of 7 checks passed
@steipete
steipete deleted the claude/terminal-worker-threads branch September 28, 2026 04:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant