Flaky: test_remove_ctrl_c_caught_by_hook_stops_batch hangs on macOS
test_remove_ctrl_c_caught_by_hook_stops_batch::case_1_exit_130 failed on macOS in run 42575429063 (the advisory affected tests (macos) leg on #4747) with wt did not finish: after the test sent SIGINT to the job group, wt hadn't exited 20 seconds later.
#4747 changes only test files in shell_integration_prompt.rs and step_promote.rs, so this test ran against the same wt binary that main builds. The recent failed affected runs on main were setup-nu 504s (#4742), not this test. No issue mentions this test yet. One occurrence isn't enough to tell whether the race is in the fixture or in wt's signal handling.
The test was added in #4533. The hook traps INT, writes its PID, and loops on sleep 0.1. SignalJob::interrupt_admitted_child waits for the PID file and the Foreground admitted:<pid> trace record before sending SIGINT to the job group. So the signal should arrive after the trap is installed, and the hang is somewhere after that point. The -vv trace from a failing run would show whether wt recorded receiving the SIGINT.
This isn't fixed yet. I couldn't reproduce the hang, so instead of guessing at a fix I opened #4749, which makes the next occurrence show where it happened. A timed-out SignalJob now prints the job group's live processes and wt's Foreground admitted:<pid> / Foreground signal received:2 trace records. Until now that trace was deleted along with the test's tempdir.
Every reproduction attempt passed:
- Hook script alone: 2,900 runs under dash, bash 5.2, and bash 3.2.57 run as
sh(the version macOS ships as/bin/sh), each sent a SIGINT to its process group at a random point. None hung. - Full test on Linux: 60 runs with dash and 150 with bash 3.2.57 as
sh, the latter under CPU load. None hung. - Full test on macOS: the advisory macOS leg on #4749 ran it again and it passed, as did all 5,488 tests in that run.
The same trap … INT; …; while :; do sleep 0.1; done hook pattern also appears in user_hooks.rs, for_each.rs, and step_alias.rs. If it is the cause, those tests can hang the same way. The next failure's report will show which explanation holds:
- If the hook's
shis still alive after wt loggedForeground signal received:2, the hook never ran its trap. That points to the fixture, and the fix is to make it wait in a way that can't miss the signal. - If the hook is gone and wt is still running, the bug is in wt's signal handling.
It happened again on main in run 45310987282 (test (macos), commit 85f5b5bb967eb9f1783d9d343b80bd1d44634643, report #4906). Same test (case_1_exit_130), same wt did not finish after 20 s. This time the hang report from #4749 was there, and it shows the hook was the part that missed the signal:
wt pid 44267; job processes (pid ppid pgid stat comm):
44265 43889 44265 Z< <defunct>
44267 43889 44265 S< .../wt
45063 44267 44265 S< sh
73556 45063 44265 S< sleep
foreground trace:
Foreground admitted:45063
Foreground signal received:2
wt admitted the hook's sh (45063), received the group SIGINT, and is waiting for that child, which is what it should do while a hook owns the key. The hook's sh is still alive in the same process group, and its sleep has a much later pid (73556), so the while :; do sleep 0.1; done loop kept going for the full 20 s without running trap 'exit 130' INT. That fits the first of the two explanations above: the fixture is at fault, not wt's signal handling.
I couldn't find the exact window where bash 3.2 loses it. In both bash 3.2-beta and current bash, wait_for in jobs.c runs the INT trap whether the foreground sleep died of SIGINT or exited normally. So it isn't the plain "child caught SIGINT" path. The loss has to happen somewhere outside wait_for, most likely around the fork of the next sleep. That path is unconfirmed, and it is specific to macOS /bin/sh: every failure so far is on macOS, and the earlier repro attempts on Linux never hung.
Two ways to fix it, both changes to test fixtures only:
- Make the hook wait in a way that doesn't depend on a foreground child, e.g.
while :; do sleep 0.1 & wait $!; done. SIGINT interrupts thewaitbuiltin and runs the trap right away. - Have
interrupt_admitted_childsend SIGINT again until the hook's pid is gone. That models a user pressing Ctrl-C twice, though, and would weaken what this test checks (one keypress stops the batch).
The same trap … INT; …; while :; do sleep 0.1; done pattern is in user_hooks.rs, for_each.rs, and step_alias.rs, so one fixture change should cover all of them. None of this can be checked from a Linux CI sandbox. The evidence that would settle the first option is a stress run of the hook pattern under macOS /bin/sh, comparing the current loop with sleep & wait.
@max-sixty The hang is in the fixture, not in wt's signal handling. The 2026-11-24 macOS run shows wt recorded Foreground signal received:2 and was waiting on the admitted sh, which stayed in the same process group because bash 3.2 dropped that one SIGINT between forking a foreground sleep and wait_for.
https://github.com/max-sixty/worktrunk/pull/4920 keeps the test contract (one keypress, exit 130, neither worktree removed) and waits with sleep 0.1 & wait $! so the trap runs. The same loop in the other caught-SIGINT fixtures is updated the same way.