From 18d03b62199dc102a68b6d88ec753208d9e913a9 Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Tue, 3 Nov 2026 17:10:58 +0000 Subject: [PATCH] fix(ui-server): never retain a native checkpoint refused on an aborted signal The journal observer added a checkpoint id to its retained set before the native lease owner checked the signal. On an already-aborted signal the owner refused before recording any lease, the cleanup release was refused for the same reason, and the id stayed retained. close() then retried it and threw "native checkpoint(s) remain retained after shutdown" (#3521). Check the signal in the same synchronous step that hands the id to the manager. The test fixture's release now refuses an unrecorded id, like the lease owner, and two tests cover a caller abort and a shutdown that begins while an observation awaits initialization. Co-Authored-By: Claude Opus 5.5 --- .../native-observer-aborted-checkpoint.md | 5 +++++ .../src/brain/native-journal-observer.ts | 4 ++++ .../tests/native-journal-observer.test.ts | 17 ++++++++++++++++- 3 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 .changeset/native-observer-aborted-checkpoint.md diff --git a/.changeset/native-observer-aborted-checkpoint.md b/.changeset/native-observer-aborted-checkpoint.md new file mode 100644 index 0000000000..c1a406a8f5 --- /dev/null +++ b/.changeset/native-observer-aborted-checkpoint.md @@ -0,0 +1,5 @@ +--- +"@schlessera/brain-ui-server": patch +--- + +Server shutdown no longer fails intermittently with "native checkpoint(s) remain retained after shutdown" (#3521). When a journal observation's signal was already aborted, for example because shutdown began while the observation was starting, the observer kept a checkpoint id that the native lease owner had refused to record. That id could never be released. The observer now checks the signal before keeping the id, so a refused checkpoint is never retained. diff --git a/packages/ui-server/src/brain/native-journal-observer.ts b/packages/ui-server/src/brain/native-journal-observer.ts index 717483541c..d8282cf9ba 100644 --- a/packages/ui-server/src/brain/native-journal-observer.ts +++ b/packages/ui-server/src/brain/native-journal-observer.ts @@ -289,6 +289,10 @@ export function createNativeJournalObserver(native: NativeJournalManager, worker const shutdown = new AbortController(), retained = new Set(); const signalFor = (signal?: AbortSignal) => signal ? AbortSignal.any([signal, shutdown.signal]) : shutdown.signal; async function checkpoint(signal: AbortSignal) { + // The lease owner refuses an aborted signal before recording any lease, so + // an id retained past that refusal could never be released (#3521). Check + // in the same synchronous step that hands the id to the manager. + signal.throwIfAborted(); const id = randomUUID(); retained.add(id); try { const answer = await native.checkpoint(id, signal); diff --git a/packages/ui-server/tests/native-journal-observer.test.ts b/packages/ui-server/tests/native-journal-observer.test.ts index 82bcde58fb..db11201e8a 100644 --- a/packages/ui-server/tests/native-journal-observer.test.ts +++ b/packages/ui-server/tests/native-journal-observer.test.ts @@ -39,7 +39,8 @@ function fixture(stream: Buffer, before: Entry[], after: Entry[]) { const manager: NativeJournalManager = { async checkpoint(id, signal) { signal?.throwIfAborted(); live.add(id); const first = number++ === 0; return { id, path: first ? previous : next, ...(first ? identities.before : identities.after), readOnly: true }; }, async delta(_from,_to,signal) { signal?.throwIfAborted(); return {stream:new ReadableStream({start(controller){controller.enqueue(stream);controller.close();}}),completed:failCompletion ? Promise.reject(new Error("Odysseus helper failed")) : Promise.resolve({exitCode:0,bytes:stream.length})}; }, - async release(id) { if(failRelease){failRelease=false;throw new Error("Odysseus release uncertain");} live.delete(id);released.push(id);return {}; }, + // Like the lease owner, refuse a root whose checkpoint was never recorded (#3521). + async release(id) { if(!live.has(id)) throw new Error("This native root has no recorded checkpoint lease"); if(failRelease){failRelease=false;throw new Error("Odysseus release uncertain");} live.delete(id);released.push(id);return {}; }, }; const observer = createNativeJournalObserver(manager); return {root, previous, next, identities, manager, observer, live, released, failCompletion(){failCompletion=true;},failRelease(){failRelease=true;},async close(){await observer.close();rmSync(root,{recursive:true,force:true});}}; @@ -125,6 +126,20 @@ test("cancellation after checkpoint creation releases original id even when repl finally{await f.close();} }); +// #3521: the manager refuses an already-aborted checkpoint before recording its lease. +for (const [name, start] of [ + ["a caller signal aborted before the observation's checkpoint", (f: ReturnType) => { const controller = new AbortController(); controller.abort(new Error("Odysseus stopped")); return f.observer.observe(controller.signal); }], + ["shutdown that begins while an observation awaits initialization", (f: ReturnType) => { const observing = f.observer.observe(); void f.observer.close().catch(() => {}); return observing; }], +] as const) test(`${name} never retains a checkpoint the manager did not record`, async () => { + const f = fixture(warmStream, [], []); + try { + await f.observer.initialize(); + await expect(start(f)).rejects.toThrow(); + await expect(f.observer.close(), "NATIVE_OBSERVER_UNRECORDED_CHECKPOINT_NOT_RETAINED").resolves.toBeUndefined(); + expect(f.live.size).toBe(0); + } finally { await f.close(); } +}); + test("symlinks are metadata; descendants are never followed outside the sealed tree",async()=>{ const f=fixture(warmStream,[],[]); symlinkSync("/outside",join(f.previous,"odysseus-link"));