From 604de5d3451d401b67df23672fc088a4250b50c0 Mon Sep 17 00:00:00 2001 From: Josh Gratton Date: Thu, 28 May 2026 17:22:26 -0400 Subject: [PATCH] fix(desktop): don't let spec.onShutdown abort stop() teardown MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Inside stop(), Ref.modify atomically flips state to "no active run, no restart fiber" before spec.onShutdown is invoked, but the physical teardown (Fiber.interrupt + closeRun) runs *after* the mutex releases. If onShutdown raised, the whole mutex effect failed and both cleanup steps were skipped — leaving the child process and restart fiber running while state claimed nothing was active. The next start() would then spawn a second backend on top. Wrap the onShutdown call in Effect.ignore so its failure can't abort the surrounding teardown. Matches the pattern closeRun already uses for the same reason. --- apps/desktop/src/backend/DesktopBackendManager.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/apps/desktop/src/backend/DesktopBackendManager.ts b/apps/desktop/src/backend/DesktopBackendManager.ts index b86f4ee086..beb0a5311b 100644 --- a/apps/desktop/src/backend/DesktopBackendManager.ts +++ b/apps/desktop/src/backend/DesktopBackendManager.ts @@ -656,7 +656,15 @@ export const makeBackendInstance = Effect.fn("makeBackendInstance")(function* ( restartFiber: Option.none>(), }, ]); - yield* spec.onShutdown?.() ?? Effect.void; + // Ignore failures from spec.onShutdown so a downstream throw + // can't abort the rest of stop(). Ref.modify above already + // flipped state to "no active run / no restart fiber", and the + // physical cleanup (Fiber.interrupt + closeRun) runs after the + // mutex releases. If onShutdown were allowed to propagate, both + // would be skipped and the child process + restart fiber would + // be orphaned while state claimed nothing was running — the + // next start() would then spawn a second backend on top. + yield* (spec.onShutdown?.() ?? Effect.void).pipe(Effect.ignore); return result; }), );