Repository navigation
fix(sdk): stop idle pooled Copilot runtimes; end tool shells on every teardown (#331) - #377
Open
ChrisKrawczyk wants to merge 7 commits into
Open
ChrisKrawczyk wants to merge 7 commits into
ChrisKrawczyk wants to merge 7 commits into
Conversation
This was referenced Nov 24, 2026
ChrisKrawczyk
marked this pull request as ready for review
November 24, 2026 18:43
This was referenced Nov 24, 2026
Teardowns outside the sweep still wait on an unbounded disconnect, plus two idle-stop residuals
#396
Open
A worker kept one Copilot runtime process per credential, transport and workspace root for its whole life, even after every session on it had dehydrated. Each idle runtime held its memory, and its tool shells. - SessionManager counts warm sessions per client object (a client shared by several keys is counted once) and leases a client to each short-lived user: a model-catalog read, a reset's deleteSession, a dehydrate's re-resume, and a session being opened. - After copilotClientIdleStopMs (worker default 30000; env PILOTSWARM_COPILOT_CLIENT_IDLE_STOP_MS; 0 disables it) with no warm session and no lease, the client leaves the pool first, then stops, falling back to forceStop() after the 10 s shutdown deadline. The next user of the key starts a fresh runtime and resumes from saved state. A client installed through the `client` setter is never stopped. - A warm session that moves to another client key releases the old one. - Every teardown cancels the session's background shells and agent tasks (rpc.tasks.cancel): dehydrate, eviction, destroy, invalidation, reset, rebind, hydrate, client change and worker shutdown, not only workspace sessions. A tool shell runs under setsid and outlives client.stop(). After each cancel round, what is left of the shell's own session (Linux) or process group (other POSIX) is killed, so a daemon it started does not survive it. A descendant that calls setsid itself still escapes; Windows is a no-op. - summarizeSession waits at most 60 s for its title (then uses the fallback title and aborts the turn) and always stops its one-shot client, with the same forced-stop fallback. No orchestration replay change, no migration. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: cc944a13-3e0b-43de-8040-5863182bbdb0
…op test (#331) A worker shutdown cancelled each warm session's shells before it stopped the runtimes. For a turn still running, that cancel ended the turn's tool call before CopilotClient.stop did, and the model run took another step (workspace-moves M3b). Shutdown now only lists each session's shells and their process sessions or groups, stops the runtimes, then kills what it listed, so the stop stays the first thing to end a running turn. The idle-stop integration test waited for a dehydrate the live orchestration never runs (its hold window only rotates affinity). It now reclaims the warm session through the eviction sweep, covers eviction, dehydrate and shutdown for the shell cleanup, and the docs name the sweep as the usual trigger. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: cc944a13-3e0b-43de-8040-5863182bbdb0
Review fixes for the shell cleanup in this PR: - SF-1: shellProcessScope no longer refuses a scope whose chain reaches PID 1, so the sid/pgid kill runs when the worker is PID 1 in a container. - SF-2: the raw-PID fallback records (pid, kernel start ticks) at binding and signals only on exact equality, verifies each descendant from the same snapshot, fails closed on unknown identity and on win32, and checks an absolute deadline synchronously right before every signal, so an abandoned loop never signals. The scope kills use the same deadline. - SF-3: background task cancels run concurrently and share one wait. - SF-5: one process snapshot per pass. - cancelBackgroundTasks takes an absolute deadline, bounds the task list by it and skips the pause when no scope is left; listBackgroundShells returns the bound shells for shutdown. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: cc944a13-3e0b-43de-8040-5863182bbdb0
Review fixes for the idle stop and the teardown paths in this PR: - SF-6: the eviction sweep tears sessions down concurrently (8 at a time); each workspace release runs its cancel and disconnect under one shared 20 s deadline, and the cancel is never skipped. - SF-7: shutdown lists, stops and then kills shells per runtime, so one slow runtime no longer delays the kills of the others. - SF-8/SF-14: summarizeSession bounds start, create and prompt with one 60 s budget and force-stops the one-shot client when it is spent. - C-1: the vision lookup prefers the runtime the session is warm on, then its recorded key, and starts any other client under a lease; the key binding is restored after each state reset in getOrCreate. - C-2: a state reset deletes no session on a runtime that never started. - C-3: the idle grace is clamped to the largest timer delay, and PILOTSWARM_COPILOT_CLIENT_IDLE_STOP_MS is parsed strictly; junk such as "30s" or "1e3" is ignored with a warning. - SF-9..SF-13: the unit and local idle-stop tests assert process exit and the effects the review asked for. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: cc944a13-3e0b-43de-8040-5863182bbdb0
- design.md, configuration.md, the workspaces proposal and CHANGELOG: strict parsing and clamping of the idle grace, exact start identity, no signal after the deadline, per-runtime shutdown kills, the summarize budget, and that killed descendants are reaped only when a reaping init (for example tini) runs as PID 1; without one they stay zombies that hold only a PID slot. - C-7: the default agent prompt (1.25.0) and base-v2 say background shells end when the session is torn down. - agent-tuning-log: record the prompt change. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: cc944a13-3e0b-43de-8040-5863182bbdb0
After the rebase onto #376, the warm-first vision lookup skips a runtime that has exited as well as one being stopped. Say so in design.md. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: cc944a13-3e0b-43de-8040-5863182bbdb0
#331) The teardown deadline is 20 s (5 s when the runtime may be gone); a shutdown's shell kills have none and run right after each runtime's stop. The idle-stop option is parsed as strictly as the env var: a number is rounded down, and an invalid option uses the default rather than the env var. A Workflow's script tasks already ended their shells before this change. CLI 1.0.83 kept listing a self-ended detached shell as running; CLI 1.0.112 reports it completed (process-tree.ts comment only). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: cc944a13-3e0b-43de-8040-5863182bbdb0
ChrisKrawczyk
force-pushed
the
fix/329-dead-runtime
branch
from
November 25, 2026 04:27
ce74d92 to
d74443b
Compare
ChrisKrawczyk
force-pushed
the
fix/331-idle-runtime-stop
branch
from
November 25, 2026 04:27
7335928 to
769c67c
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #331
Stacked on #376 (the dead-runtime fix for #329). Review only the commits after #376's head, and retarget this PR to
mainafter #376 merges.A worker kept one Copilot runtime process per credential, transport and workspace root for its whole life, even after every session on it had dehydrated. Each idle runtime kept its memory and its tool shells. Now an unused pooled runtime stops after an idle period, and every session teardown ends that session's background shells. No orchestration version and no migration.
Idle stop
PilotSwarmWorker({ copilotClientIdleStopMs })0disables the idle stop.PILOTSWARM_COPILOT_CLIENT_IDLE_STOP_MSOn by default. The value must be a whole number of milliseconds: anything else (
30s,1e3,-1) logs a warning and uses the default. A numeric option is rounded down; a negative or non-finite one gets the same warning. An invalid option does not fall back to the env value. Values above 2^31-1 are clamped.sessionIdleTimeoutSecondsis not passed to the SDK: the idle stop works at the pool, not in the runtime.SessionManagertracks warm sessions by client object, not by key, so a client shared by several keys stays in use while any of its sessions is warm.listModels), a state or epoch reset'sdeleteSession, a dehydrate's retry re-resume, and a session being created or resumed.stop()runs with the same 10 s deadline as shutdown, falling back toforceStop(). The next user of the key starts a fresh runtime and resumes from saved state.deleteSessionon a runtime that never started. It may still create a pooled client it does not use, which the idle stop then removes (Teardowns outside the sweep still wait on an unbounded disconnect, plus two idle-stop residuals #396).clientsetter is pinned and never stopped for idleness._stopClientmarks the stop as requested, so the Warm session reused after copilot-runtime dies: turns stall ~300 s and same-worker retries fail with 'Connection is closed.' #329 runtime monitor does not report it as a lost runtime.#320
#320 (renewable GitHub credentials) adds its own client-retirement bookkeeping and conflicts with main. This PR generalizes the same idea (
retiringClients, per-client tracking, one_stopClientwith the forced-stop fallback). The plan is for #320 to rebase onto this and retire a client through_stopClientinstead of adding a second mechanism.Shells end with their session
A tool shell runs under
setsidand outlivesclient.stop(). Before this change only workspace sessions, the user's Stop and a Workflow's own script tasks ended background shells. Now every teardown of a warm session runs the same cleanup: dehydrate, eviction, destroy, invalidation, a state or epoch reset, an epoch start, a rebind, a hydrate over a warm session, a client change and a runtime loss.rpc.tasks.cancelfor each of the session's background shells and agent tasks. The cancels run concurrently and share one wait. A shell whose cancel is not confirmed is killed with the processes below it, by its exact identity.A worker shutdown sends no cancel: it lists the shells, stops the runtime, then kills them (below).
The kill is strict:
psstart (to the second) elsewhere. A shell's pid is bound only if its process started no more than 2 s after the task's reported start. Its pid and start are recorded then, and matched exactly right before each signal, from a fresh/procread on Linux and the pass's listing elsewhere. An unknown identity, or Windows, means no signal.Teardowns from the eviction sweep run concurrently, at most 8 at a time. Each one's cancel and disconnect share one 20 s deadline. At shutdown, each runtime runs list, stop and kill on its own, so a running turn still ends through
client.stop()first, and one slow runtime does not delay the kills for the others. Listing each session's shells is bounded at 5 s.Limits:
setsiditself escapes the kill, and so does a daemon whose launcher exited before the teardown (A daemon survives the session teardown when its launcher shell exits first #395).The default agent prompt (1.25.0) and
base-v2.mdnow tell agents that background shells end when the session is torn down.summarizeSession
One 60 s budget covers starting the one-shot client, creating its session and the wait for
session.idle. When it runs out, the fallback title is used; a prompt that was already sent is aborted first, waiting at most 5 s for the abort. The client is stopped infinallyon success, failure and the early return: forced at once when the budget is spent, otherwisestop()with the same 10 s deadline and forced-stop fallback.Tests
test/local/copilot-runtime-idle-stop.test.js(a real runtime with a scripted model endpoint, and PostgreSQL):test/unit/copilot-runtime-idle-stop.test.mjs:listModels, a state reset, overlapping leases and a session open;0and a caller-supplied client; the strict parse and the clamp;stop()is forced; a cold reset deletes nothing;summarizeSessionalways stops its client.test/unit/process-tree.test.mjs:Multi-model review
Three models (Claude Opus 5.5, GPT-6.2 Sol and Gemini 4 Argon) reviewed this PR as five specialists in a threaded debate, then a synthesis re-checked each finding against the code. The result was 0 must-fix, 14 should-fix, 8 to consider, 2 trade-offs and 8 observations. The fixes are in the commits "harden background shell kills" and "bound session teardowns and fix idle-stop bookkeeping", with two docs commits. All should-fix findings are fixed except SF-4 (below).
Each fix has a regression test. For SF-1, SF-2, SF-3, SF-7, SF-8, C-1 and C-3, the test was also shown to fail without the fix.
Left as follow-ups:
destroySession, the dehydrate, the resets, a hydrate over a warm session, invalidation without a deadline and thegetOrCreatepaths still wait for the disconnect without a deadline; a cold reset still creates a client it never uses; a cold vision lookup for a workspace session starts a runtime under a key no turn uses.C-5 (summarize still starts its own unpooled client for each call) is recorded in the review and not changed here; this PR only bounds it.
Left for
@github/copilot-sdkstop()should end the runtime's process tree and its tools' process sessions. Today tool shells escape throughsetsid, and a killed runtime's descendants outlive it (Processes started by a killed Copilot runtime can outlive it and race the retry #385).doStopaborts only in-process sessions;sessionIdleTimeoutSecondsis unused by PilotSwarm.Validation
All runs were on Linux (Node 24.22.1, PostgreSQL 16), one suite at a time, at
769c67cb(stacked on #376 atd74443b0, rebased on 0.8.18,1f6af957).run-tests.sh: 444 of 444 files; 4584 passed, 0 failed and 26 skipped of 4610.