From 302e97b72db973af74f65286fbf5c99968b44930 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 24 Oct 2026 03:11:21 +0200 Subject: [PATCH] A lease lost while the state root is opening is a fence, not a crash The startup persist now sits behind the same gate as every other write; its LeaseLost ended serve() as an unhandled rejection. onLost has already run, so serve returns without reporting active. Co-Authored-By: Claude Opus 5.5 --- scripts/vili-daemon.mjs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/scripts/vili-daemon.mjs b/scripts/vili-daemon.mjs index 2bd7576..7e2ba16 100644 --- a/scripts/vili-daemon.mjs +++ b/scripts/vili-daemon.mjs @@ -778,9 +778,14 @@ export async function serve(rt) { stopWorker().finally(() => runtime.reportHealth?.("degraded")); }; runtime.watch(onLost); // held() reports a loss through this the moment any gate sees it - guard(); - await mkdir(path.join(args.stateRoot, "jobs"), { recursive: true }); - await persistSurfaces(); + try { + guard(); + await mkdir(path.join(args.stateRoot, "jobs"), { recursive: true }); + await persistSurfaces(); + } catch (error) { + if (error instanceof LeaseLost) return; // lost while opening the state root: onLost has run, and nothing is active + throw error; + } surfaceTimer = setInterval(() => { runtime.watch(onLost); if (lost) return; @@ -788,6 +793,7 @@ export async function serve(rt) { if (!(error instanceof LeaseLost)) console.error(`persist failed: ${error?.name || "Error"}`); }); }, 5000); + if (lost) return; runtime.reportHealth?.("active"); console.log("Vili holds the state root"); }