From 3b9f86cd4a4e9226f27d54bbb88e7cea360f072a Mon Sep 17 00:00:00 2001 From: Josh Gratton Date: Mon, 11 May 2026 10:20:51 -0400 Subject: [PATCH] fix(desktop): address bot review findings on WSL backend swap MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - drain stdout/stderr concurrently in runWslShell so node-gyp output on both pipes can't deadlock the child - short-circuit waitForReady when desiredRunning flips off, so an external stop() during swap doesn't waste the full timeout - guard runSwap continuations after the 180s flow timeout so an orphaned IPC resolution can't overwrite rolled-back UI state - drop unused refreshPrimaryEnvironmentDescriptor — descriptor URL is stable across the swap and consumers re-fetch lazily --- apps/desktop/src/backend/DesktopBackendManager.ts | 10 +++++++--- apps/desktop/src/wsl/DesktopWslEnvironment.ts | 9 ++++++--- .../components/settings/ConnectionsSettings.tsx | 14 ++++++++++---- apps/web/src/environments/primary/context.ts | 12 ------------ apps/web/src/environments/primary/index.ts | 1 - 5 files changed, 23 insertions(+), 23 deletions(-) diff --git a/apps/desktop/src/backend/DesktopBackendManager.ts b/apps/desktop/src/backend/DesktopBackendManager.ts index f049bfc7a8..bd992c1726 100644 --- a/apps/desktop/src/backend/DesktopBackendManager.ts +++ b/apps/desktop/src/backend/DesktopBackendManager.ts @@ -600,14 +600,18 @@ const makeDesktopBackendManager = Effect.fn("makeDesktopBackendManager")(functio const waitForReady = (timeout: Duration.Duration): Effect.Effect => Effect.gen(function* () { + const current = yield* Ref.get(state); + // Return false early if an external `stop()` flipped desiredRunning off + // — no point polling for a backend that is being torn down. + if (!current.desiredRunning) return { done: true, ready: false }; const ready = yield* Ref.get(desktopState.backendReady); - if (ready) return true; - return false; + return ready ? { done: true, ready: true } : { done: false, ready: false }; }).pipe( Effect.repeat({ - until: (ready) => ready, + until: (status) => status.done, schedule: Schedule.spaced(Duration.millis(100)), }), + Effect.map((status) => status.ready), Effect.timeoutOption(timeout), Effect.map(Option.getOrElse(() => false)), ); diff --git a/apps/desktop/src/wsl/DesktopWslEnvironment.ts b/apps/desktop/src/wsl/DesktopWslEnvironment.ts index dc48882abc..75f45d8a69 100644 --- a/apps/desktop/src/wsl/DesktopWslEnvironment.ts +++ b/apps/desktop/src/wsl/DesktopWslEnvironment.ts @@ -102,9 +102,12 @@ const runWslShell = ( Effect.gen(function* () { const spawnerService = yield* spawner; const handle = yield* spawnerService.spawn(command); - const stdoutBytes = yield* Stream.runCollect(handle.stdout); - const stderrBytes = yield* Stream.runCollect(handle.stderr); - const exitCode = yield* handle.exitCode; + // Drain stdout and stderr concurrently so neither pipe buffer can fill + // and stall the child (node-gyp rebuild emits large output on both). + const [stdoutBytes, stderrBytes, exitCode] = yield* Effect.all( + [Stream.runCollect(handle.stdout), Stream.runCollect(handle.stderr), handle.exitCode], + { concurrency: "unbounded" }, + ); return { exitCode: exitCode as unknown as number, stdout: decodeUtf8(concatChunks(stdoutBytes)), diff --git a/apps/web/src/components/settings/ConnectionsSettings.tsx b/apps/web/src/components/settings/ConnectionsSettings.tsx index 2a102f0d73..d579e263d6 100644 --- a/apps/web/src/components/settings/ConnectionsSettings.tsx +++ b/apps/web/src/components/settings/ConnectionsSettings.tsx @@ -2398,15 +2398,18 @@ export function ConnectionsSettings() { // the backend to come up (cold WSL boots + node-pty prep can be slow), // the welcome wait is 45s after that, and reauth has its own retry loop. // Cap the total so a hung step can't strand the modal forever. + let aborted = false; + let flowTimeoutHandle: number | null = null; const flowTimeout = new Promise((_, reject) => { - window.setTimeout( - () => reject(new Error("Backend swap took too long. Check WSL is responsive and try again.")), - 180_000, - ); + flowTimeoutHandle = window.setTimeout(() => { + aborted = true; + reject(new Error("Backend swap took too long. Check WSL is responsive and try again.")); + }, 180_000); }); const runSwap = async () => { const updated = await desktopBridge.setWslBackend(target); + if (aborted) return; setDesktopWslState(updated); if (previousPrimaryEnvId) { @@ -2419,6 +2422,7 @@ export function ConnectionsSettings() { // Descriptor is the same URL across the swap, so it'll be re-fetched // lazily by the next consumer — no need to force a refresh here. await reauthenticatePrimaryEnvironment(); + if (aborted) return; // Wait for the new backend's welcome event so the modal stays open // until threads are actually ready. Bounded by 45s. @@ -2429,6 +2433,7 @@ export function ConnectionsSettings() { window.setTimeout(resolve, 45_000); }), ]); + if (aborted) return; if (previousPrimaryEnvId) { useStore.getState().removeEnvironmentState(previousPrimaryEnvId); @@ -2437,6 +2442,7 @@ export function ConnectionsSettings() { try { await suppressReconnect(() => Promise.race([runSwap(), flowTimeout])); + if (flowTimeoutHandle !== null) window.clearTimeout(flowTimeoutHandle); setPendingDesktopWslSelection(null); toastManager.add({ diff --git a/apps/web/src/environments/primary/context.ts b/apps/web/src/environments/primary/context.ts index 4ea36a9189..dea1683c4d 100644 --- a/apps/web/src/environments/primary/context.ts +++ b/apps/web/src/environments/primary/context.ts @@ -109,18 +109,6 @@ export function resolveInitialPrimaryEnvironmentDescriptor(): Promise { - const nextPromise = fetchPrimaryEnvironmentDescriptor(); - primaryEnvironmentDescriptorPromise = nextPromise; - return nextPromise.finally(() => { - if (primaryEnvironmentDescriptorPromise === nextPromise) { - primaryEnvironmentDescriptorPromise = null; - } - }); -} - export function __resetPrimaryEnvironmentBootstrapForTests(): void { primaryEnvironmentDescriptorPromise = null; usePrimaryEnvironmentBootstrapStore.getState().reset(); diff --git a/apps/web/src/environments/primary/index.ts b/apps/web/src/environments/primary/index.ts index dafdcd58a5..ce37ae2671 100644 --- a/apps/web/src/environments/primary/index.ts +++ b/apps/web/src/environments/primary/index.ts @@ -1,7 +1,6 @@ export { getPrimaryKnownEnvironment, readPrimaryEnvironmentDescriptor, - refreshPrimaryEnvironmentDescriptor, resetPrimaryEnvironmentDescriptorForTests, resolveInitialPrimaryEnvironmentDescriptor, usePrimaryEnvironmentId,