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 }); }),