Skip to content

fix(sdk): kill a dead runtime's tool shells before the retry - #449

Open
ChrisKrawczyk wants to merge 5 commits into
mainfrom
fix/385-kill-dead-runtime-shells
Open

ChrisKrawczyk wants to merge 5 commits into
mainfrom
fix/385-kill-dead-runtime-shells

Conversation

@ChrisKrawczyk

@ChrisKrawczyk ChrisKrawczyk commented Dec 2, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #385.

What

When a Copilot runtime exits unexpectedly, for example after an out-of-memory kill, its sessions' background shells and the daemons they started kept running beside the retry. Teardown asked the runtime to list them, and the runtime was gone.

Now each session lists its shells while its runtime runs (on session.background_tasks_changed) and records each shell's process scope. When the runtime exits, the worker SIGKILLs those shells and whatever is left in their session or process group, before a retry can start. Each kill is confirmed; an unconfirmed one is retried at the session's next teardown. Teardown of a session whose runtime died uses that list and sends nothing to the dead runtime.

Limits

  • Covered: shells (sync or background) still running at the last listing, and the daemons in their session or process group.
  • Not covered:
  • A listing that finishes after the exit binds nothing, so a reused PID is never targeted.
  • POSIX only; on Windows nothing changes.
  • The complete fix belongs in @github/copilot-sdk: stop() or an unexpected exit should end the runtime's process tree.

Review

Reviewed by five specialists on different models over three debate rounds. Fixed: the docs promised more than the code covers; a listing after the exit could bind a reused PID; kills were not confirmed; teardown paths differed; no real-CLI test killed the runtime mid-turn. Deferred:

  • proving a newly listed PID descends from the live runtime before binding it;
  • a deadline on tasks.list.

Tests

  • Unit: kills at the runtime's exit, confirmed or retried; nothing bound after the exit; one teardown path for every case; rescoping a shell is capped.
  • Integration (Linux, real CLI):
    • SIGKILL an idle runtime with a shell and a daemon running: both stop within 10 s with no teardown, and the next turn runs on a fresh runtime.
    • SIGKILL the runtime mid-turn with a sync and a detached shell, each with a daemon: all four stop within 10 s, before the retry reaches the model. The retry answers on a fresh runtime with no lossy hand-off.
  • Full Linux suite: every stage passes except one SDK case (4778 passed, 1 failed, 27 skipped): workflow-child-runtime R9, whose 4 s window is too short under full-suite load. It passes alone and is fixed in test: widen R9's receive window so a loaded run can't end the parent first #451.
  • npm run lint and npm run check:privacy pass.

No TLA+ update: the orchestrations, hydration and dehydration are unchanged. This is worker-side process cleanup at a runtime exit.

The CHANGELOG ## Unreleased heading conflicts trivially with #447; whichever merges second needs a rebase.

A tool shell runs in its own session and outlives its Copilot runtime. When
the runtime exited unexpectedly, the teardown asked the dead runtime for its
task list, failed, and left the shells and their daemons running beside the
retry.

Each warm session now lists its shells again on
session.background_tasks_changed while its runtime runs, binding each shell's
identity and own session or process group while the runtime still sits
between the shell and the worker. At the runtime's exit, SessionManager kills
those shells and what is left of their scopes. A teardown of a session whose
runtime is gone kills them instead of calling the dead runtime. A shell
already bound keeps its binding, so the process table is read only for new
shells.

Fixes #385

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: cc944a13-3e0b-43de-8040-5863182bbdb0
- Kill a lost runtime's shells from the exit listener, before the retry, and
  confirm each kill; an unconfirmed kill is retried at the next teardown.
- Bind nothing after the runtime is lost: a list that finishes after the exit
  cannot rescope a shell to a reused PID or group.
- Route workspace release, eviction, dehydrate and destroy through one
  shell-ending path that kills directly when the runtime is already gone.
- Trace the exit report (tracked, ended, unbound, unconfirmed, stale).
- Add unit tests for these paths and a real-CLI test that kills the runtime
  mid-turn with a sync and a detached shell and checks all four writers stop
  before the retry reaches the model.
- Narrow the docs to what is covered.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: cc944a13-3e0b-43de-8040-5863182bbdb0
chkraw and others added 3 commits December 3, 2026 10:21
A kill is confirmed only when every process it had to check was signalled
or proven gone; a partial /proc table, a failed read or signal, or a group
signal is no longer proof.
A late task list from a teardown or shutdown binds and cancels nothing.
Scope tries stay bounded when the process table cannot be read.
The exit trace tells an empty listing from none and warns when no retry follows.
Opt-in fake process host modes test each case.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: cc944a13-3e0b-43de-8040-5863182bbdb0
- Every individual signal and scope-leader check reads its target again
  right before acting (/proc, or ps -p elsewhere), never from an earlier
  table, so a pid that changed hands since the listing is spared. This
  covers the new per-member group sweep and the older ps tree kill.
- Test the Linux host's /proc error mapping (ENOENT gone, EACCES/EPERM
  hidden, EIO unknown) and which rows mark a table partial.
- A runtime exit during a worker shutdown no longer reports its
  unconfirmed shells as never retried: the shutdown's own kill also
  tries them. Docs say the same.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: cc944a13-3e0b-43de-8040-5863182bbdb0
Every signal now reads its target again, and on macOS/BSD that read runs
`ps`, which blocks the event loop. A read must not start once a kill's
deadline has passed, and a `ps` read gives up at the deadline.

- runPs caps its timeout by the time left and runs no `ps` after the
  deadline; ProcessHost.snapshot/read take the deadline.
- signalProcess, killTree, the scope sweep's leader and table reads and
  killListedShellsEach check the deadline before each read.
- cancelBackgroundTasks reads no tree table once its budget is spent.
- The runtime-exit report warns when a listed shell could not be bound:
  it was not signalled and may still run.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: cc944a13-3e0b-43de-8040-5863182bbdb0

This branch has not been deployed

No deployments
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.

Processes started by a killed Copilot runtime can outlive it and race the retry

2 participants