Skip to content

Commit 899fee2

Browse files
thymikeeclaude
andcommitted
fix(daemon): mark every session close keeps after its teardown
A claimed session kept by a failed provider lease release was not marked, so it pinned daemon idle reap and its claim. One wrapper around close finalization now marks any session still present after teardown, whichever step failed. Align the batching and replay docs with the retry wording: retry close with the same options, leaving out --save-script once the script is published. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent ce29c0d commit 899fee2

6 files changed

Lines changed: 61 additions & 21 deletions

File tree

‎packages/daemon/src/core/session-state.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,8 @@ export type SessionState = {
157157
*/
158158
lastActivityAtMs?: number;
159159
/**
160-
* #4237: a `close` failed its platform close or cleanup, so it kept this session to withhold its
161-
* device claim for a retried `close`. Until a successful reopen, such a session does not hold the
160+
* #4237: a `close` ran its teardown but could not finish it (a failed platform close, cleanup, or
161+
* provider lease release), so it kept this session for a retried `close`. Until a successful reopen, such a session does not hold the
162162
* daemon open: idle reap may end the daemon, whose shutdown teardown retries the cleanup and leaves
163163
* a claim it still cannot confirm stale.
164164
*/

‎packages/daemon/src/core/session-store.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,8 +408,8 @@ export class SessionStore {
408408
}
409409

410410
/**
411-
* #4237: marks a session that `close` kept to withhold its device claim after a failed platform
412-
* close or cleanup, so daemon idle reap no longer waits on it. A successful reopen clears the mark;
411+
* #4237: marks a session that `close` kept after running its teardown (a failed platform close,
412+
* cleanup, or provider lease release), so daemon idle reap no longer waits on it. A successful reopen clears the mark;
413413
* otherwise it lasts until `retire` removes the session.
414414
*/
415415
markCloseEffectsUnconfirmed(ref: SessionRef): void {

‎packages/daemon/src/sessions/session-lifecycle/internal/__tests__/session-close-claim-retry.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,3 +447,24 @@ test('a session reopened after a failed close blocks the daemon idle reap again'
447447
expect(reopened).toMatchObject({ ok: true });
448448
expect(idleReapFires(sessionStore)).toBe(false);
449449
});
450+
451+
// Every session a close keeps after running its teardown is marked, whichever step failed: here the
452+
// teardown is clean and only the provider lease release fails, on a session that holds a claim.
453+
test('a claimed session kept by a failed lease release lets the daemon idle reap fire', async () => {
454+
const sessionName = 'close-lease-release-failure-claimed';
455+
const leaseRegistry = new LeaseRegistry();
456+
const sessionStore = await openSession(sessionName, { lease: { registry: leaseRegistry } });
457+
const { loader } = lifecycleOperationsSpy();
458+
const release = vi
459+
.fn<NonNullable<LeaseLifecycleProvider['release']>>()
460+
.mockRejectedValue(new AppError('COMMAND_FAILED', 'provider unreachable'));
461+
462+
const failed = await close(sessionStore, sessionName, loader, {
463+
leaseRegistry,
464+
leaseLifecycleProvider: { release },
465+
});
466+
467+
expect(failed).toMatchObject({ ok: false, error: { retriable: true } });
468+
expect(sessionStore.get(sessionName)?.deviceClaim).toBeDefined();
469+
expect(idleReapFires(sessionStore)).toBe(true);
470+
});

‎packages/daemon/src/sessions/session-lifecycle/internal/session-close.ts‎

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -302,19 +302,21 @@ export async function handleSessionCloseCommands(
302302
lifecycle: admission.runtime,
303303
});
304304
if ('response' in repair) return repair.response;
305-
const closed = await runCloseTeardownAndRelease({
306-
env: params.host.env,
307-
req,
308-
ref,
309-
logPath,
310-
sessionStore,
311-
leaseRegistry,
312-
leaseLifecycleProvider,
313-
lifecycle: admission.runtime,
314-
clearRuntimeHints: admission.clearRuntimeHints,
315-
repairArmed: repair.repairArmed,
316-
platformResourceCleanup,
317-
});
305+
const closed = await markSessionKeptAfterTeardown(sessionStore, ref, () =>
306+
runCloseTeardownAndRelease({
307+
env: params.host.env,
308+
req,
309+
ref,
310+
logPath,
311+
sessionStore,
312+
leaseRegistry,
313+
leaseLifecycleProvider,
314+
lifecycle: admission.runtime,
315+
clearRuntimeHints: admission.clearRuntimeHints,
316+
repairArmed: repair.repairArmed,
317+
platformResourceCleanup,
318+
}),
319+
);
318320
if (closed.kind === 'response') return closed.response;
319321
return buildCloseSuccessResponse({
320322
session,
@@ -325,6 +327,23 @@ export async function handleSessionCloseCommands(
325327
});
326328
}
327329

330+
/**
331+
* #4237: the one owner of "a kept session is marked". A close that ran its teardown either retires
332+
* the session or keeps it for a retried close, whichever step failed (platform close, cleanup, or
333+
* provider lease release). Every kept session is marked, so daemon idle reap never waits on it.
334+
*/
335+
async function markSessionKeptAfterTeardown<Result>(
336+
sessionStore: SessionStore,
337+
ref: SessionRef,
338+
finalize: () => Promise<Result>,
339+
): Promise<Result> {
340+
try {
341+
return await finalize();
342+
} finally {
343+
if (sessionStore.resolveCurrent(ref)) sessionStore.markCloseEffectsUnconfirmed(ref);
344+
}
345+
}
346+
328347
type SessionCloseFinalization =
329348
| { kind: 'response'; response: DaemonResponse }
330349
| {
@@ -398,7 +417,6 @@ async function runCloseTeardownAndRelease(params: {
398417
// The withheld claim names a live owner, so `--stale` cannot reclaim it. Retiring the
399418
// session would leave nothing able to release it; keeping it, and its lease, lets a retried
400419
// close rerun the teardown.
401-
sessionStore.markCloseEffectsUnconfirmed(ref);
402420
throw claimBlockingError;
403421
}
404422
}

‎website/docs/docs/batching.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ acquisition that would recover another session's durable resources, can also be
2626
another app in the same session keeps its owned resource handles. An Android test IME alone is
2727
restored by ordinary `close`, including a bounded step. Restoration may settle after the remaining
2828
batch time. If it fails on a session that holds a device claim, the session and its claim stay; inspect
29-
the device, then run `close` again to retry the cleanup. A session without a claim is retired. Run a refused step separately with its original arguments and options, the same `--session` value, and
29+
the device, then retry `close` with the same options, leaving out `--save-script` once the script is
30+
published. A session without a claim is retired. Run a refused step separately with its original arguments and options, the same `--session` value, and
3031
the same private CLI context. Inspect `details.partialResults` and
3132
`details.dispatchedSteps` before retrying the batch: earlier steps may already have changed the app.
3233
The batch still authenticates and renews its own lease while it runs.

‎website/docs/docs/replay-e2e.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ affects `close` when it would dispose owned resources or release a provider leas
2424
that would recover another session's durable resources. Opening another app in the same session
2525
keeps its owned resource handles. An Android test IME alone is restored by authored `close`, even
2626
when restoration settles after the remaining replay time. A failed restore on a session that holds a
27-
device claim keeps the session and its claim; inspect the device, then run `close` again to
28-
retry the cleanup. A session without a claim is retired. The name is not a complete command. Inspect the failed
27+
device claim keeps the session and its claim; inspect the device, then retry `close` with
28+
the same options, leaving out `--save-script` once the script is published. A session without a claim is retired. The name is not a complete command. Inspect the failed
2929
script path and line, then run that original operation separately with its original arguments and
3030
options, the same `--session` value, and the same private CLI context. If steps remain and the
3131
report offers `resume.alternateFrom`, use it with `planDigest` after completing the refused step directly.

0 commit comments

Comments
 (0)