Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/native-observer-aborted-checkpoint.md
Original file line number Diff line number Diff line change
@@ -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.
4 changes: 4 additions & 0 deletions packages/ui-server/src/brain/native-journal-observer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,10 @@ export function createNativeJournalObserver(native: NativeJournalManager, worker
const shutdown = new AbortController(), retained = new Set<string>();
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);
Expand Down
17 changes: 16 additions & 1 deletion packages/ui-server/tests/native-journal-observer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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});}};
Expand Down Expand Up @@ -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<typeof fixture>) => { 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<typeof fixture>) => { 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"));
Expand Down
Loading