Skip to content

Commit 00b200c

Browse files
committed
fix(desktop): recover from unavailable WSL and don't false-fail slow swaps
Two bugbot reports on the WSL backend flow: High - "WSL off hides recovery control": when persisted wslMode is "wsl" but WSL is unavailable (wsl.exe removed, no distro), backend resolution still took the WSL spawn path and looped on preflight failures, while the Connections "Backend runtime" row is hidden whenever WSL is unavailable - leaving no in-app way back to Windows. resolveBackendStartConfig now checks wslEnvironment.isAvailable and falls back to the Windows backend (logging a warning) when WSL was requested but isn't usable. Added a regression test asserting the Windows path is taken in that case. Medium - "Six minute cap false failure": the swap wrapped setWslBackend + reauth + welcome wait in one 6-minute Promise.race, so a slow-but- successful swap could trip the ceiling during reauth/sync and surface "Backend swap took too long" even though the backend already switched (and getWslState would show the new mode). Track a swapCommitted flag set once setWslBackend returns; when the global ceiling fires after commit, report the switch as done with the still-syncing caveat instead of a hard failure. Genuine pre-commit failures (aborted false, or setWslBackend throwing) still take the error path.
1 parent 0b5f35d commit 00b200c

3 files changed

Lines changed: 83 additions & 3 deletions

File tree

‎apps/desktop/src/backend/DesktopBackendConfiguration.test.ts‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,4 +262,40 @@ describe("DesktopBackendConfiguration", () => {
262262
}
263263
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
264264
);
265+
266+
it.effect("falls back to the Windows backend when wsl mode is set but WSL is unavailable", () =>
267+
Effect.gen(function* () {
268+
const fileSystem = yield* FileSystem.FileSystem;
269+
const baseDir = yield* fileSystem.makeTempDirectoryScoped({
270+
prefix: "t3-desktop-backend-config-test-",
271+
});
272+
273+
yield* Effect.gen(function* () {
274+
const environment = yield* DesktopEnvironment.DesktopEnvironment;
275+
const configuration = yield* DesktopBackendConfiguration.DesktopBackendConfiguration;
276+
const config = yield* configuration.resolve;
277+
278+
// Persisted wsl mode + WSL unavailable must not strand the user on the
279+
// WSL spawn path (which loops on preflight failures while the WSL
280+
// settings row is hidden). Resolve to the Windows backend instead.
281+
assert.equal(config.executablePath, process.execPath);
282+
assert.equal(config.bootstrap.t3Home, environment.baseDir);
283+
assert.isTrue(Option.isNone(config.preflightFailure));
284+
}).pipe(
285+
Effect.provide(
286+
DesktopBackendConfiguration.layer.pipe(
287+
Layer.provideMerge(serverExposureLayer),
288+
Layer.provideMerge(
289+
DesktopAppSettings.layerTest({
290+
...DesktopAppSettings.DEFAULT_DESKTOP_SETTINGS,
291+
wslMode: "wsl",
292+
}),
293+
),
294+
Layer.provideMerge(DesktopWslEnvironment.layerTest({ isAvailable: false })),
295+
Layer.provideMerge(makeEnvironmentLayer(baseDir, { platform: "win32" })),
296+
),
297+
),
298+
);
299+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
300+
);
265301
});

‎apps/desktop/src/backend/DesktopBackendConfiguration.ts‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,21 @@ const resolveBackendStartConfig = Effect.fn("desktop.backendConfiguration.resolv
210210
const serverExposure = yield* DesktopServerExposure.DesktopServerExposure;
211211
const backendExposure = yield* serverExposure.backendConfig;
212212

213-
const useWsl = input.wslMode === "wsl" && environment.platform === "win32";
213+
// Only take the WSL spawn path when WSL is actually usable. If the user
214+
// persisted wsl mode but WSL has since become unavailable (e.g. wsl.exe
215+
// removed or no distro installed), fall back to the Windows backend
216+
// instead of looping forever on preflight failures: the Connections
217+
// "Backend runtime" row is hidden while WSL is unavailable, so a stuck
218+
// WSL primary would otherwise leave no in-app way back to Windows.
219+
const wslEnvironment = yield* DesktopWslEnvironment.DesktopWslEnvironment;
220+
const wslRequested = input.wslMode === "wsl" && environment.platform === "win32";
221+
const wslAvailable = wslRequested ? yield* wslEnvironment.isAvailable : false;
222+
if (wslRequested && !wslAvailable) {
223+
yield* Effect.logWarning(
224+
"WSL backend requested but WSL is unavailable; starting the Windows backend instead.",
225+
);
226+
}
227+
const useWsl = wslRequested && wslAvailable;
214228

215229
const bootstrap = {
216230
mode: "desktop" as const,

‎apps/web/src/components/settings/ConnectionsSettings.tsx‎

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2442,13 +2442,19 @@ export function ConnectionsSettings() {
24422442
}, 360_000);
24432443
});
24442444

2445-
// Whether the new backend's welcome event synced before the 45s ceiling.
2446-
// Stays false if the wait timed out, which softens the success copy below.
2445+
// `welcomeSynced`: whether the new backend's welcome arrived before the
2446+
// 45s ceiling — softens the success copy below if it timed out.
2447+
// `swapCommitted`: whether setWslBackend already switched the backend.
2448+
// Once true, the global timeout firing means a slow post-switch sync, not
2449+
// a failed swap, so we must not report "swap took too long" and contradict
2450+
// the now-current backend state.
24472451
let welcomeSynced = false;
2452+
let swapCommitted = false;
24482453
const runSwap = async () => {
24492454
const updated = await desktopBridge.setWslBackend(target);
24502455
if (aborted) return;
24512456
setDesktopWslState(updated);
2457+
swapCommitted = true;
24522458

24532459
if (previousPrimaryEnvId) {
24542460
useStore.getState().removeEnvironmentState(previousPrimaryEnvId);
@@ -2501,6 +2507,30 @@ export function ConnectionsSettings() {
25012507
: `The local backend is now running ${backendLocation}. It's taking a moment to finish syncing — your threads will refresh automatically.`,
25022508
});
25032509
} catch (error) {
2510+
// The global ceiling fired after the backend had already switched:
2511+
// the slow part was post-switch reauth/thread sync, not the swap
2512+
// itself. Report the switch as done — with the still-syncing caveat —
2513+
// instead of a hard failure that contradicts the now-current
2514+
// getWslState. (A genuine thrown error, where `aborted` is false,
2515+
// still falls through to the failure path below.)
2516+
if (aborted && swapCommitted) {
2517+
setPendingDesktopWslSelection(null);
2518+
const backendLocation =
2519+
target.mode === "wsl"
2520+
? `inside ${target.distro ?? "the default WSL distro"}`
2521+
: "on Windows";
2522+
toastManager.add({
2523+
type: "success",
2524+
title: "Backend restarted",
2525+
description: `The local backend is now running ${backendLocation}. It's taking a moment to finish syncing — your threads will refresh automatically.`,
2526+
});
2527+
await reauthenticatePrimaryEnvironment().catch(() => undefined);
2528+
await desktopBridge
2529+
.getWslState()
2530+
.then((state) => setDesktopWslState(state))
2531+
.catch(() => undefined);
2532+
return;
2533+
}
25042534
const message = error instanceof Error ? error.message : "Failed to update WSL backend.";
25052535
setDesktopWslError(message);
25062536
toastManager.add(

0 commit comments

Comments
 (0)