fix(desktop): address bot review findings on WSL backend swap

- 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
This commit is contained in:
Josh Gratton committed 2026-05-16 11:38:36 -04:00
1 parent 0ae9a14c40
commit 3b9f86cd4a
5 files changed
+23 -23

No files matched your search

@@ -600,14 +600,18 @@ const makeDesktopBackendManager = Effect.fn("makeDesktopBackendManager")(functio
const waitForReady = (timeout: Duration.Duration): Effect.Effect<boolean> =>
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)),
);
@@ -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)),
@@ -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<never>((_, 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({
@@ -109,18 +109,6 @@ export function resolveInitialPrimaryEnvironmentDescriptor(): Promise<ExecutionE
});
}
// Used by the WSL backend swap: bypass the cached descriptor and re-fetch
// against the (potentially new) backend at the same URL.
export function refreshPrimaryEnvironmentDescriptor(): Promise<ExecutionEnvironmentDescriptor> {
const nextPromise = fetchPrimaryEnvironmentDescriptor();
primaryEnvironmentDescriptorPromise = nextPromise;
return nextPromise.finally(() => {
if (primaryEnvironmentDescriptorPromise === nextPromise) {
primaryEnvironmentDescriptorPromise = null;
}
});
}
export function __resetPrimaryEnvironmentBootstrapForTests(): void {
primaryEnvironmentDescriptorPromise = null;
usePrimaryEnvironmentBootstrapStore.getState().reset();
@@ -1,7 +1,6 @@
export {
getPrimaryKnownEnvironment,
readPrimaryEnvironmentDescriptor,
refreshPrimaryEnvironmentDescriptor,
resetPrimaryEnvironmentDescriptorForTests,
resolveInitialPrimaryEnvironmentDescriptor,
usePrimaryEnvironmentId,