← Back to Pull requests

fix(runtime): hang up a client terminal's shell when stopping it #6116

Open
UncertaintyDeterminesYou4ndMe opened this pull request with 144 lines diff across 7 files
View on GitHub
UncertaintyDeterminesYou4ndMe UncertaintyDeterminesYou4ndMe commented on Oct 15, 2026 08:05 UTC
Author

Summary

Closing a terminal took about 2 s in every client. A client terminal runs exec "$SHELL" -l in a PTY, an interactive shell, and interactive zsh and bash ignore SIGTERM. So runtime.resource.stop always waited out the 2 s kill grace (DEFAULT_PROCESS_TERMINATION_GRACE_MS) until SIGKILL ended the shell, and only then answered; a client that keeps the tab until the stop is confirmed (Desktop) shows it for those 2 s.

A client terminal's run now carries interactiveShell: true. Stopping it sends SIGTERM to the process tree as before, then SIGHUP to the shell process alone, as closing a terminal window does. The shell exits at once and, by default, hangs up its jobs; the rest of its group still gets SIGTERM first. The grace and the SIGKILL after it are unchanged, for a shell that traps HUP.

Two consequences of the shell now exiting on a hang-up rather than a SIGKILL: zsh runs ~/.zlogout, and a job in the terminal that ignores both SIGTERM and SIGHUP (for example one started with nohup and trapping TERM) outlives the stop, as it outlives a closed terminal window and as it already does for any run whose root exits on SIGTERM. Before, it was killed by the SIGKILL after the grace.

Only client terminals are hung up. Other runs, including the model's PTY runs, are unchanged: their root may be the program itself, which may treat SIGHUP as a reload. A pipe run cannot carry the flag; the manager refuses it at start.

Verification

  • runtime.resource.stop of a client terminal on a dev Host built from this branch, five runs each: 2073–2086 ms with the coordinator not setting the flag (as on main), 51–61 ms with it; both end cancelled, exit code 130.

  • The new shell-run-manager tests: with a 30 s grace the stop ends in about 90 ms, the group getting only SIGTERM and the shell SIGHUP; on main's source the test fails after 30 s with the group also getting SIGKILL. A second test checks that a job in the shell's group that ignores SIGHUP is gone after the stop. The coordinator test pins the flag on a terminal start and its absence on a command start.

  • shell-run-manager and pty-process-driver (71 tests, 4 skipped as on main) and the three runtime-resource-* files in runtime-host (32 tests) pass. Not run locally: the root npm run typecheck and the full workspace suite, because my local dependencies are older than this commit's lockfile.
  • AI use

    Select exactly one:

  • No generative tool made a substantive contribution

  • Generative tooling made a substantive contribution
  • Tool(s) and scope: Claude Code found the cause, wrote the change and its tests, and reviewed them adversarially; I reviewed the result.

    Checklist

  • Tests cover the change and fail without it

  • Lint, format, typecheck and the affected suites pass locally (Biome and the affected suites pass; the root typecheck was not run locally, see Verification)
  • Does this PR entail a change in behavior?

  • Yes — described under Summary above

  • No
  • Diff: 144 lines (+140 / -4) across 7 files Reviewer: Astro-Han Review duration: 7652m
    All Vouch review confidence checks passed
    Residual risk 0.05 = Change Risk (0.42) × (1 − Review Confidence (0.89)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.42 · 144 lines diff across 7 files
    0.42
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.88 · 3 substantive comment(s)
    0.88
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    88%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.298
    Real PR Conversation Timeline
    5 items
    Astro-Han Astro-Han commented on code on Oct 15, 2026 09:10 UTC
    Code Review
    packages/runtime/src/shell-run-manager.ts:1
    @@ -1504,6 +1510,19 @@ export class ShellRunProcessManager
         lifecycle.initialSignal.resolve(applied);
         if (live.rootExited || live.finalizeOnce) return;
     
    +    // A client terminal's interactive shell ignores SIGTERM. Hang up the shell
    +    // alone, as closing a terminal window does, so it exits now rather than at
    +    // the SIGKILL after the grace. Its group has had SIGTERM; signalling the
    +    // group once the shell is gone could reach a reused process group ID.
    +    // Windows has no hang-up, and taskkill above has already ended the tree.
    +    if (live.mode === 'pty' && live.interactiveShell && process.platform !== 'win32') {

    This win32 literal adds shell-run-manager.ts to the Windows-branching closure, so windows-package-source-closure.test.mjs fails: the windows-recovery.yml path filter does not list the file. Either add it to the filter, or move the platform check into PtyProcessDriver (for example, a hangUp() that does nothing on Windows), which is already in the filter.

    Astro-Han Astro-Han commented on code on Oct 15, 2026 09:10 UTC
    Code Review
    packages/runtime/src/__tests__/shell-run-manager.test.ts:1
    @@ -2950,6 +2950,102 @@ describe('ShellRunProcessManager', () => {
         assert.equal(forced.liveCount(), 0);
         assert.equal(forced.livePtyCount(), 0);
       });
    +
    +  test('hangs up an interactive terminal shell after the SIGTERM it ignores', {
    +    skip:
    +      process.platform === 'win32'
    +        ? 'Windows tree termination has no graceful SIGTERM or SIGHUP phase'
    +        : false,
    +  }, async (t) => {
    +    // The grace outlasts the test: without the hang-up, Stop would wait 30 s
    +    // and the process group would receive SIGTERM then SIGKILL.
    +    const manager = await createTestManager(undefined, { killGraceMs: 30_000 });
    +    const run = await manager.runBackgroundBash(
    +      shellInput({
    +        cwd: await workspace(),
    +        command: 'trap "" TERM; printf "ready\\n"; while :; do sleep 0.05; done',
    +        pty: true,
    +        interactiveShell: true,
    +        timeoutMs: 60_000,
    +      }),
    +    );
    +    assertShellRun(run);
    +    const pid = run.pid;
    +    assert.ok(pid !== undefined && pid > 0);
    +    await waitForPtyText(manager, run.ref, /ready/);
    +    const kill = t.mock.method(process, 'kill');
    +    const signalsTo = (target: number) =>
    +      kill.mock.calls
    +        .filter((call) => call.arguments[0] === target)
    +        .map((call) => call.arguments[1]);
    +
    +    const startedAt = performance.now();
    +    const stopped = await manager.stopBackgroundTask('session-1', run.ref, NO_ABORT);
    +    const stopMs = performance.now() - startedAt;
    +
    +    assertShellRun(stopped);
    +    assert.equal(stopped.status, 'cancelled');
    +    assert.deepEqual(signalsTo(-pid), ['SIGTERM']);
    +    assert.deepEqual(signalsTo(pid), ['SIGHUP']);
    +    assert.ok(stopMs < 5_000, `Stop took ${stopMs} ms`);
    +    assert.equal(manager.livePtyCount(), 0);
    +  });
    +
    +  test('ends a job that ignores SIGHUP in an interactive shell process group', {

    This also passes without the fix, because the group SIGTERM ends the job. It works as a guard, but only the previous test proves the change.

    Astro-Han Astro-Han commented on code on Oct 15, 2026 09:10 UTC
    Code Review
    packages/runtime-host/src/__tests__/runtime-resource-coordinator.test.ts:1
    @@ -437,6 +438,8 @@ describe('Host Runtime Resource coordinator', () => {
             visibility: undefined,
             cwd: '/workspace',
             pty: true,
    +        // Stopping the terminal hangs up the shell instead of sending SIGTERM.

    Nit: the hang-up comes after SIGTERM, not instead of it.

    Astro-Han Astro-Han commented on Oct 15, 2026 09:10 UTC
    Reviewer

    Automated review notice: This comment was posted by an automated review agent. It is not an independent human review and does not replace one.

    Reviewed range: merge-base with main..17977153bc1d80f5500d0b1d19fffc533dc7017e (single commit, first review, no prior findings).

    The approach is sound. On POSIX, runTermination still runs the SIGTERM pass via terminateProcessTree (SIGKILL to descendants that left the root's group, then SIGTERM to -pid). Only after that does it send SIGHUP to the shell's pid alone, through node-pty's kill (process.kill(pid, 'SIGHUP')), and then it waits for the same grace and SIGKILL escalation as before. So a shell that traps HUP is still force-killed after the grace. Under job control, jobs started in an interactive terminal sit in their own process groups. The SIGTERM pass already SIGKILLs those jobs as escaped descendants, so the "nohup job outlives the stop" case in the description only applies to processes that share the shell's own group. The new code is guarded by rootExited || finalizeOnce with no await between the check and the signal, and an ESRCH is swallowed. The only remaining pid-reuse window is the gap between node-pty's native reap and the JS onExit callback. The existing SIGKILL escalation has the same window, so this is not a new class of race. On Windows, the hang-up is skipped and taskkill /t /f has already ended the tree, so nothing changes there. The pipe-mode refusal and the coordinator flag (set only when command is undefined) look right.

    Findings

    P2: PR-caused CI failure. The test job fails in scripts/windows-package-source-closure.test.mjs ("the recovery filter is exactly the Windows-branching closure of its tests"). The new process.platform !== 'win32' guard is the first win32 literal in packages/runtime/src/shell-run-manager.ts. That puts the file in the Windows-branching closure, but .github/workflows/windows-recovery.yml does not list it in its path filter. Main's file has no win32 and the latest main CI run is green, so this failure comes from the PR. There are two ways to fix it: add the file to the windows-recovery path filter (which schedules the Windows lane on every shell-run-manager edit), or move the platform decision into PtyProcessDriver, which already branches on win32 and is already in the filter. For example, a hangUp() that does nothing on Windows. Either works. The second also avoids killPty turning a SIGHUP into a full pty.kill() on Windows if the guard is ever dropped.

    P3: The second new test ("ends a job that ignores SIGHUP in an interactive shell process group") also passes on main, because the job is ended by the group SIGTERM that predates this change. It is a reasonable guard that the hang-up does not leave group members behind, but it does not prove the fix. Only the first test fails without the change: on main it would see SIGTERM and then SIGKILL on -pid and take about 30 s. Both tests run bash -c without job control, so neither exercises a real interactive login shell. That gap is acceptable given the manual timing evidence in the description.

    P3 (nit): The coordinator test comment says "hangs up the shell instead of sending SIGTERM". It is in addition to SIGTERM, as the contract doc says.

    CI / mergeability: GitHub reports the PR as MERGEABLE, with merge state BLOCKED. test fails (P2 above), while label and windows_recovery pass. No protocol files are touched, so there is no epoch impact.

    Reviewers
    Assignees
    UncertaintyDeterminesYou4ndMe UncertaintyDeterminesYou4ndMe
    Labels
    risk: low confidence: 88.8% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 41.6% (MEDIUM) · Higher = larger blast radius
    41.6%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 88.8% · Lower = rushed or shallow review
    88.8%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 4.6% · Safe to merge
    4.6%
    Repository