fix: reap finalized runtimes before session start

This commit is contained in:
2026-07-21 16:04:08 +02:00
parent 019f6b282e
commit a040c083cc
2 changed files with 86 additions and 8 deletions
+28 -8
View File
@@ -35,6 +35,10 @@ export function sessionRoutes(
string,
ReturnType<PiProcessManager["createFor"]>
>();
const runtimeManifestReaders = new WeakMap<
ReturnType<PiProcessManager["createFor"]>,
() => Promise<any>
>();
const failurePersistenceClaimed = new WeakSet<
ReturnType<PiProcessManager["createFor"]>
>();
@@ -82,11 +86,33 @@ export function sessionRoutes(
const storageFailure = (reply: any) => reply.code(503).send({ error: "session storage is unavailable" });
const releaseIfFinalized = async (
id: string, rt: ReturnType<PiProcessManager["createFor"]>,
): Promise<boolean> => {
if (d.mgr.get(id) !== rt || boundRuntimes.get(id) !== rt) return false;
const readManifest = runtimeManifestReaders.get(rt);
if (!readManifest || (await readManifest())?.status !== "finalized") return false;
if (!d.mgr.teardownIfCurrent(id, rt)) return false;
if (boundRuntimes.get(id) === rt) boundRuntimes.delete(id);
return true;
};
const releaseFinalizedRuntimes = async (): Promise<void> => {
await Promise.all([...boundRuntimes.entries()].map(([id, rt]) =>
withSessionLifecycle(id, () => releaseIfFinalized(id, rt)).catch((error: unknown) => {
console.error(`[session:${id}] stale runtime cleanup failed:`, error);
}),
));
};
const bindRuntime = (
id: string, rt: ReturnType<PiProcessManager["createFor"]>, runner: any, workspace?: string,
) => {
const previous = boundRuntimes.get(id);
boundRuntimes.set(id, rt);
if (typeof runner.sessionShow === "function") {
runtimeManifestReaders.set(rt, () => runner.sessionShow(id, workspace));
}
try {
rt.bridge.onClientEvent((e) => {
// Child termination is asynchronous. Ignore queued events from a runtime once a newer
@@ -115,14 +141,7 @@ export function sessionRoutes(
if (d.mgr.get(id) !== rt && boundRuntimes.get(id) === rt) {
boundRuntimes.delete(id);
} else if (typeof runner.sessionShow === "function") {
void withSessionLifecycle(id, async () => {
if (d.mgr.get(id) !== rt || boundRuntimes.get(id) !== rt) return;
const manifest = await runner.sessionShow(id, workspace);
if (manifest?.status !== "finalized") return;
if (d.mgr.teardownIfCurrent(id, rt) && boundRuntimes.get(id) === rt) {
boundRuntimes.delete(id);
}
}).catch((error: unknown) => {
void withSessionLifecycle(id, () => releaseIfFinalized(id, rt)).catch((error: unknown) => {
console.error(`[session:${id}] terminal runtime cleanup failed:`, error);
});
}
@@ -189,6 +208,7 @@ export function sessionRoutes(
let s: Settings;
try { s = await d.getSettings(principal); } catch { return storageFailure(reply); }
const runner = runnerFor(principal);
await releaseFinalizedRuntimes();
const ensure = await d.readiness.ensure(s.workspace ?? "", principal);
if (!ensure.ok) return reply.code(503).send({ error: READINESS_FAILURE_MESSAGE });
// Local-only: verify the DWH is reachable BEFORE creating the session, so a dropped