From 792bcd4dc01b5b2252a53006f1e2228eca5b8ebd Mon Sep 17 00:00:00 2001 From: repo-worker-tmp-code-b Date: Tue, 17 Nov 2026 04:23:49 -0600 Subject: [PATCH] fix(acp-adapter): delete owned serve sessions on shutdown (#8313, queue task 20119) --- bin/opencode-acp-adapter.mjs | 69 ++++++++++++++++++++++++++----- bin/opencode-acp-adapter.test.mjs | 34 +++++++++++++++ 2 files changed, 93 insertions(+), 10 deletions(-) diff --git a/bin/opencode-acp-adapter.mjs b/bin/opencode-acp-adapter.mjs index 90898ba127..797713677f 100755 --- a/bin/opencode-acp-adapter.mjs +++ b/bin/opencode-acp-adapter.mjs @@ -29,8 +29,12 @@ // session id) // There is no v1 route shape left in this file: every serve call // above is the pinned server's v2 contract (#4706 rule 1, #4614). -// No DELETE /api/session/:id call exists here — see the #4707 -// closing-audit disposition of the sessions map (issue #4707). +// Shutdown deletes the serve sessions this adapter created +// (DELETE /api/session/:id for every key of the sessions map): +// the pinned serve never expires idle sessions server-side, so +// without that they would outlive the adapter on the ONE standing +// shared serve (#8313; the gap the #4707 closing audit filed). +// Best-effort and bounded — exit is never blocked on the serve. // // Prompt lifecycle (design #4706, closing audit #4707) — the four // rules every state container below obeys: @@ -755,6 +759,57 @@ async function dispatch(message) { } } +// --------------------------------------------------------------------------- +// Shutdown session cleanup (#8313) +// --------------------------------------------------------------------------- + +// The pinned serve never expires idle sessions server-side (verified +// against the flake.lock-pinned fork: its inactivity sweep only +// interrupts executions and evicts location caches; session records +// are removed solely through the explicit DELETE route). Every serve +// session this adapter created would therefore outlive it on the ONE +// standing shared serve, so shutdown deletes the sessions it owns — +// the keys of the sessions map. Best-effort and bounded: each delete +// is abort-timed and exit proceeds on the same overall bound whether +// or not the serve answers. Crash case: if the adapter is killed +// hard (SIGKILL, host death) no cleanup runs and its sessions persist +// until the serve restarts — accepted residual, recorded on #8313. +const SHUTDOWN_CLEANUP_BUDGET_MS = 2000; + +async function deleteOwnedServeSessions() { + const ids = [...sessions.keys()]; + if (ids.length === 0) return; + debug(`shutdown: deleting ${ids.length} owned serve session(s)`); + const deadline = Date.now() + SHUTDOWN_CLEANUP_BUDGET_MS; + await Promise.allSettled( + ids.map(async (id) => { + const remaining = deadline - Date.now(); + if (remaining <= 0) return; + try { + await fetch(`${serverBase}/api/session/${encodeURIComponent(id)}`, { + method: 'DELETE', + headers: authHeaders(), + signal: AbortSignal.timeout(remaining), + }); + } catch { + // Best-effort: a failed delete never blocks or fails shutdown. + } + }), + ); +} + +let shutdownStarted = false; +function shutdown() { + if (shutdownStarted) return; + shutdownStarted = true; + shuttingDown = true; + const forceExit = setTimeout(() => process.exit(0), SHUTDOWN_CLEANUP_BUDGET_MS + 500); + void deleteOwnedServeSessions().finally(() => { + clearTimeout(forceExit); + process.exit(0); + }); +} + function main() { void runEventStream(); const rl = createInterface({ input: process.stdin, crlfDelay: Infinity }); @@ -770,14 +825,8 @@ function main() { } void dispatch(message); }); - rl.on('close', () => { - shuttingDown = true; - process.exit(0); - }); - process.on('SIGTERM', () => { - shuttingDown = true; - process.exit(0); - }); + rl.on('close', shutdown); + process.on('SIGTERM', shutdown); debug(`started; server=${serverBase}`); } diff --git a/bin/opencode-acp-adapter.test.mjs b/bin/opencode-acp-adapter.test.mjs index 092854b9f5..ae99ae0497 100644 --- a/bin/opencode-acp-adapter.test.mjs +++ b/bin/opencode-acp-adapter.test.mjs @@ -127,6 +127,11 @@ function startStubServe(script) { emit({ type: 'session.execution.succeeded', data: { sessionID: SESSION_ID } }); return; } + if (req.method === 'DELETE' && url.pathname === `/api/session/${SESSION_ID}`) { + res.writeHead(200, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify({ data: true })); + return; + } res.writeHead(404, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ error: 'not found' })); }); @@ -663,3 +668,32 @@ test('late terminal event from a reconciled prompt does not finish the next prom 'second prompt waited for its own completion event, not the stale one', ); }); + +test('stdin close deletes the serve sessions the adapter created (#8313)', async (t) => { + const { stub, acp } = await setup(t, textEvents()); + const result = await acp.request('session/new', { cwd: '/home/user/code', mcpServers: [] }); + assert.equal(result.sessionId, SESSION_ID); + const exited = new Promise((resolve) => acp.child.once('exit', resolve)); + acp.child.stdin.end(); + const code = await Promise.race([exited, sleep(5000).then(() => 'timeout')]); + assert.equal(code, 0, 'adapter exits after stdin close'); + const deletes = stub.calls.filter((c) => c.method === 'DELETE'); + assert.deepEqual( + deletes.map((c) => c.pathname), + [`/api/session/${SESSION_ID}`], + ); +}); + +test('SIGTERM deletes the serve sessions the adapter created (#8313)', async (t) => { + const { stub, acp } = await setup(t, textEvents()); + await acp.request('session/new', { cwd: '/home/user/code', mcpServers: [] }); + const exited = new Promise((resolve) => acp.child.once('exit', resolve)); + acp.child.kill('SIGTERM'); + const code = await Promise.race([exited, sleep(5000).then(() => 'timeout')]); + assert.equal(code, 0, 'adapter exits on SIGTERM'); + const deletes = stub.calls.filter((c) => c.method === 'DELETE'); + assert.deepEqual( + deletes.map((c) => c.pathname), + [`/api/session/${SESSION_ID}`], + ); +});