From b62348b0fa098fa8428ed4e7719073ed5718218e Mon Sep 17 00:00:00 2001 From: Josh Gratton Date: Mon, 18 May 2026 13:18:55 -0400 Subject: [PATCH] fix(desktop): hide preflight-failed bootstraps + serialize WSL reconcile MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two review-feedback fixes: - getLocalEnvironmentBootstraps was exposing the bootstrap info (URL, token) for backends whose configResolve produced a preflightFailure. Those backends never actually listen — the manager calls scheduleRestart instead of spawning — so the renderer would pick up a phantom URL, POST to /api/auth/bootstrap/bearer, fail, and register a broken saved-env. Skip them in the IPC handler. - wslBackend.reconcile read pool state and settings non-atomically. The bootstrap fork + an in-flight setWslDistro IPC could both observe "no WSL instance registered", both proceed to startNew with different distros, and leave a stranded instance behind. Wrap the reconcile body in a single-permit semaphore so concurrent callers queue. --- apps/desktop/src/ipc/methods/window.ts | 7 +++++++ apps/desktop/src/wsl/DesktopWslBackend.ts | 15 +++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/apps/desktop/src/ipc/methods/window.ts b/apps/desktop/src/ipc/methods/window.ts index de9327b9cf..fc9b377292 100644 --- a/apps/desktop/src/ipc/methods/window.ts +++ b/apps/desktop/src/ipc/methods/window.ts @@ -62,6 +62,13 @@ export const getLocalEnvironmentBootstraps = makeSyncIpcMethod({ // backend mid-registration, before its first start cycle). They'll // appear on the next IPC call once they've started. if (Option.isNone(config)) continue; + // Skip instances whose preflight failed (e.g. WSL distro + // missing node, the linux server entry was never built). The + // backend manager schedules a restart instead of actually + // listening, so exposing the bootstrap would point the renderer + // at a port nothing is bound to and trigger needless + // /api/auth/bootstrap/bearer error cycles. + if (Option.isSome(config.value.preflightFailure)) continue; const { bootstrap, httpBaseUrl } = config.value; bootstraps.push({ id: instance.id, diff --git a/apps/desktop/src/wsl/DesktopWslBackend.ts b/apps/desktop/src/wsl/DesktopWslBackend.ts index e6b3f2cdfa..bd0972f83b 100644 --- a/apps/desktop/src/wsl/DesktopWslBackend.ts +++ b/apps/desktop/src/wsl/DesktopWslBackend.ts @@ -24,6 +24,7 @@ import * as Context from "effect/Context"; import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; import * as Option from "effect/Option"; +import * as Semaphore from "effect/Semaphore"; import * as NetService from "@t3tools/shared/Net"; @@ -89,6 +90,12 @@ export const layer = Layer.effect( const wslEnvironment = yield* DesktopWslEnvironment.DesktopWslEnvironment; const appSettings = yield* DesktopAppSettings.DesktopAppSettings; const net = yield* NetService.NetService; + // Serialize reconcile so the bootstrap fork and the IPC handlers + // (setWslBackendEnabled, setWslDistro) can't interleave. Without + // this, two reconciles could both observe "no WSL instance + // registered" between their pool reads and both call startNew + // with different distros, leaving the loser stranded. + const reconcileMutex = yield* Semaphore.make(1); const findExistingWslInstance = pool.list.pipe( Effect.map((instances) => instances.find((instance) => isWslInstanceId(instance.id))), @@ -155,7 +162,7 @@ export const layer = Layer.effect( }); }); - const reconcile = Effect.gen(function* () { + const reconcileBody = Effect.gen(function* () { const settings = yield* appSettings.get; const available = yield* wslEnvironment.isAvailable; const existing = yield* findExistingWslInstance; @@ -196,7 +203,11 @@ export const layer = Layer.effect( yield* wslEnvironment.preWarm(settings.wslDistro); yield* startNew({ distro: settings.wslDistro }); } - }).pipe(Effect.withSpan("desktop.wslBackend.reconcile")); + }); + + const reconcile = reconcileMutex + .withPermits(1)(reconcileBody) + .pipe(Effect.withSpan("desktop.wslBackend.reconcile")); return DesktopWslBackend.of({ reconcile }); }),