Public: anyone can read this; the crew can change it.

A cancelled run's child processes are proven gone: a test starts a run that spawns a grandchild, cancels it, and finds no process left #14134

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

A new test, test/run-cancel.test.ts, proves that cancelling a run leaves nothing in its process group. The kill path needed no fix: the sleeper never survived. What the test does.

Notes

shipped production is serving it deploy 4adf612

opened this

A new test, test/run-cancel.test.ts, proves that cancelling a run leaves nothing in its process group. The kill path needed no fix: the sleeper never survived.

What the test does. It starts a run through runStep and registers it with trackRun, so the run is cancelled the way cancelRepoRun cancels one once it has marked the row. The run's command forks a shell, and that shell forks a sleeper. The test cancels through stopRun and asks the kernel whether anything is left in the group itself. kill(-pgid, 0) must report the group empty (ESRCH) within 2 s. The existing test/run-cancel-tree.test.ts only checks three pids it was told about, so a group member it never heard of could slip past. There are two cases:

  • the plain tree;
  • a sleeper that ignores SIGTERM (trap '' TERM) under shells that obey it. That case shows the group is also killed when the leading shell exits, not only when the grace period runs out.

Why no kill fix. runStep (src/apps/process.ts) already starts each step as the leader of its own process group. On cancel it signals that whole group, and it sends SIGKILL to the group once the leader exits. As a check, I changed the cancel path to signal only the leader, and both cases failed. Restoring the original made them pass again.

Skipping. Before the suite runs, a probe starts a sleeper in a new group and tries to signal that group. If this machine can't (no /proc, or a sandbox that refuses), the suite is skipped and its name says why, for example a cancelled run (skipped: the sandbox cannot signal a process group (EPERM)).

Safety. The test only ever signals the groups it created. It checks each group differs from the test process's own, and its cleanup targets only those groups.

Not done. I didn't call the keep_plan and estimate tools the issue mentions, because they weren't available in this run. docs/features/run-state.md now names the new test next to run-cancel-tree. The repository has no separate list of gate test files to update.

Checks run. bun scripts/gate.ts --fix and bun x tsc --noEmit pass. bun scripts/fast.ts named 101 test files, about 8 minutes and over the 5-minute budget, so I ran 18 of them: the cancel tests plus the repo-wide checks a new test file and a doc edit can trip (top-level await, spawn sites, polling, stray tests, feature docs, module state, timers). All 222 tests in them pass. The forge's check on the push runs the rest.

To try it: bun test test/run-cancel.test.ts

🤖 Generated with Claude Code

Closes #14119

2 earlier comments
commented

A new test, test/run-cancel.test.ts, proves that cancelling a run leaves nothing in its process group. The kill path needed no fix: the sleeper never survived.

What the test does. It starts a run through runStep and registers it with trackRun, so the run is cancelled the way cancelRepoRun cancels one once it has marked the row. The run's command forks a shell, and that shell forks a sleeper. The test cancels through stopRun and asks the kernel whether anything is left in the group itself. kill(-pgid, 0) must report the group empty (ESRCH) within 2 s. The existing test/run-cancel-tree.test.ts only checks three pids it was told about, so a group member it never heard of could slip past. There are two cases:

  • the plain tree;
  • a sleeper that ignores SIGTERM (trap '' TERM) under shells that obey it. That case shows the group is also killed when the leading shell exits, not only when the grace period runs out.

Why no kill fix. runStep (src/apps/process.ts) already starts each step as the leader of its own process group. On cancel it signals that whole group, and it sends SIGKILL to the group once the leader exits. As a check, I changed the cancel path to signal only the leader, and both cases failed. Restoring the original made them pass again.

Skipping. Before the suite runs, a probe starts a sleeper in a new group and tries to signal that group. If this machine can't (no /proc, or a sandbox that refuses), the suite is skipped and its name says why, for example a cancelled run (skipped: the sandbox cannot signal a process group (EPERM)).

Safety. The test only ever signals the groups it created. It checks each group differs from the test process's own, and its cleanup targets only those groups.

Not done. I didn't call the keep_plan and estimate tools the issue mentions, because they weren't available in this run. docs/features/run-state.md now names the new test next to run-cancel-tree. The repository has no separate list of gate test files to update.

Checks run. bun scripts/gate.ts --fix and bun x tsc --noEmit pass. bun scripts/fast.ts named 101 test files, about 8 minutes and over the 5-minute budget, so I ran 18 of them: the cancel tests plus the repo-wide checks a new test file and a doc edit can trip (top-level await, spawn sites, polling, stray tests, feature docs, module state, timers). All 222 tests in them pass. The forge's check on the push runs the rest.

To try it: bun test test/run-cancel.test.ts

🤖 Generated with Claude Code

commented

This adds test/run-cancel.test.ts, which shows that cancelling a run leaves no process from its process group alive. The step's command starts a child, and that child starts a sleeper. The test cancels the run through stopRun, then asks the kernel whether anything in the group is left, using kill(-pgid, 0). It expects that answer to be "no such process" within 2 s. Asking about the group catches processes the test never knew the ids of. A second case uses a sleeper that ignores SIGTERM; the group's SIGKILL when the step ends still takes it within the 2 s.

The test polls for that answer every 20 ms. The gate's rule "a test does not wait on a real timer" flagged the Bun.sleep(20) in that loop. A fake clock can't stand in for this wait, because the test is waiting on real processes ending, so the line now ends with // real-timer: polls real processes the kernel ends, the opt-out the rule names. The loop stops as soon as the group is gone, so a passing run waits only as long as the processes take to die.

On machines where the test can't signal a process group (no /proc, or a sandbox that refuses it), the suite is skipped and its name says why. It only ever signals groups it created itself.

To try it: bun test test/run-cancel.test.ts (2 pass here, about 0.7 s). bun scripts/gate.ts and bun x tsc --noEmit also pass.

🤖 Generated with Claude Code

commented

This adds test/run-cancel.test.ts, which shows that cancelling a run leaves no process from its process group alive. The step's command starts a child, and that child starts a sleeper. The test cancels the run through stopRun, then asks the kernel whether anything in the group is left, using kill(-pgid, 0). It expects that answer to be "no such process" within 2 s. Asking about the group catches processes the test never knew the ids of. A second case uses a sleeper that ignores SIGTERM; the group's SIGKILL when the step ends still takes it within the 2 s.

The test polls for that answer every 20 ms. Its Bun.sleep line ends with // real-timer: polls real processes the kernel ends. A fake clock can't stand in for real processes ending, and the gate rule "a test does not wait on a real timer" accepts that comment. The loop body is in braces because scripts/flake.ts's check for polls that time out without saying so finds the end of a one-line loop by its closing ;. A trailing comment hides it, so the check saw } instead of the return done(); after the loop and failed the test.

On machines where the test can't signal a process group (no /proc, or a sandbox that refuses it), the suite is skipped and its name says why. It only ever signals groups it created itself.

To try it: bun test test/run-cancel.test.ts test/flake.test.ts (38 pass here). bun scripts/gate.ts and bun x tsc --noEmit also pass.

🤖 Generated with Claude Code

Approved

Test-only change; no user-facing surface, so nothing to break on a phone or without a mouse. It asks the kernel about the whole process group (kill(-pgid,0) -> ESRCH within 2 s) after a cancel through stopRun, covers a SIGTERM-deaf sleeper, signals only groups it created, skips with a named reason, and the run-state doc names it. Small, single-purpose, no new dependency, no pipeline detour. Two things I would do next: (1) dedupe the pid/poll helpers (within, pidIn) with test/run-cancel-tree.test.ts into one shared test helper so the two cancel suites do not drift; (2) add a third case where the leader ignores SIGTERM too, so the grace-then-SIGKILL group path is proven by group, not only by pid.

Sign in to join this conversation.

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs