From aebb4fa3e566abd9c9872d32548fe2006d13bccd Mon Sep 17 00:00:00 2001 From: Josh Gratton Date: Mon, 18 May 2026 11:50:44 -0400 Subject: [PATCH] perf(web): park the local-secondary auto-retry on hosts with no WSL The renderer's local-secondary reconciler was polling getLocalEnvironmentBootstraps on a fixed backoff (2s, 4s, 8s, 16s, 30s, 45s, 60s, 60s) regardless of whether the user had any WSL backend configured. On Windows hosts without WSL enabled that's eight IPC round-trips plus a setTimeout chain doing zero useful work. Probe desktopBridge.getWslState() once at module init. When the user hasn't enabled the WSL backend, latch the auto-retry chain so it parks. The settings page calls markSecondariesConfigured(state.enabled) after every WSL toggle so flipping WSL on later wakes the loop back up. For users who *do* have WSL enabled, behavior is unchanged: the loop still rides out the full 4-minute desktop-bootstrap TTL window covering cold boot. --- .../settings/ConnectionsSettings.tsx | 13 ++++- apps/web/src/environments/local/index.ts | 55 ++++++++++++++++--- 2 files changed, 59 insertions(+), 9 deletions(-) diff --git a/apps/web/src/components/settings/ConnectionsSettings.tsx b/apps/web/src/components/settings/ConnectionsSettings.tsx index 7e657ef528..567555ca92 100644 --- a/apps/web/src/components/settings/ConnectionsSettings.tsx +++ b/apps/web/src/components/settings/ConnectionsSettings.tsx @@ -84,7 +84,10 @@ import { type ServerClientSessionRecord, type ServerPairingLinkRecord, } from "~/environments/primary"; -import { reconcileLocalSecondaryEnvironments } from "~/environments/local"; +import { + markSecondariesConfigured, + reconcileLocalSecondaryEnvironments, +} from "~/environments/local"; import type { WsRpcClient } from "~/rpc/wsRpcClient"; import { type SavedEnvironmentRecord, @@ -2387,7 +2390,13 @@ export function ConnectionsSettings() { setIsUpdatingWslBackend(true); setDesktopWslError(null); try { - setDesktopWslState(await apply()); + const next = await apply(); + setDesktopWslState(next); + // Keep the renderer's auto-retry loop awake while the user has + // WSL enabled, and let it park otherwise. Without this the + // loop would never resume after a fresh desktop install where + // the initial probe latched "no WSL configured". + markSecondariesConfigured(next.enabled); // The desktop orchestrator may take a moment to register the new // WSL instance with bootstrap info — fire the renderer reconciler // now to remove stale entries (toggle-off, distro change), then diff --git a/apps/web/src/environments/local/index.ts b/apps/web/src/environments/local/index.ts index 90ba8543db..c1edca037b 100644 --- a/apps/web/src/environments/local/index.ts +++ b/apps/web/src/environments/local/index.ts @@ -69,6 +69,17 @@ const AUTO_RETRY_DELAYS_MS = [2_000, 4_000, 8_000, 16_000, 30_000, 45_000, 60_00 let autoRetryHandle: ReturnType | null = null; let autoRetryAttempt = 0; +// Latched once we've confirmed the user has no WSL backend configured +// on this host. The auto-retry loop polls getLocalEnvironmentBootstraps +// every few seconds for up to ~4 minutes (the desktop-bootstrap TTL +// window); on machines where the user will never enable WSL that's +// just wasted IPC. setWslBackendEnabled IPC handlers in the settings +// page already call reconcile with resetBudget: true, so flipping the +// switch on later resumes the loop. We default to false (keep +// retrying) so any pre-existing WSL setup still gets retried during +// cold boot. +let knownNoSecondariesConfigured = false; + export interface LocalSecondaryReconcileBootstrapSnapshot { readonly id: string; readonly label: string; @@ -394,19 +405,25 @@ async function reconcileOnce(): Promise { function scheduleAutoRetry(): void { if (autoRetryHandle !== null) return; + // For hosts where the user has no WSL backend configured the retry + // loop never has work to do, so we'd otherwise IPC every 2-60s + // forever. setWslBackendEnabled clears this latch (via + // markSecondariesConfigured) when the user flips WSL on later. + if (knownNoSecondariesConfigured) return; if (autoRetryAttempt >= AUTO_RETRY_DELAYS_MS.length) { // Budget exhausted. Surface this through the store so the sidebar // can switch from "Connecting..." to "Couldn't connect, retry?". patchReconcileState({ budgetExhausted: true }); return; } - // Note: we deliberately don't short-circuit when the current bootstraps - // list lacks any secondary entry. The desktop pool only publishes a - // backend's bootstrap once its first start cycle has produced a config - // (see desktop.ipc.window.getLocalEnvironmentBootstraps), so an in-flight - // WSL cold boot looks identical to "no WSL configured" from this side. - // We have to keep polling until either a secondary actually shows up or - // the budget runs out. + // Note: we deliberately don't short-circuit on an empty bootstraps + // list past the knownNoSecondariesConfigured check above. The desktop + // pool only publishes a backend's bootstrap once its first start + // cycle has produced a config (see + // desktop.ipc.window.getLocalEnvironmentBootstraps), so an in-flight + // WSL cold boot looks identical to "no WSL configured" from this + // side. We have to keep polling until either a secondary actually + // shows up or the budget runs out. const delay = AUTO_RETRY_DELAYS_MS[autoRetryAttempt]; autoRetryAttempt += 1; autoRetryHandle = setTimeout(() => { @@ -455,3 +472,27 @@ function runReconcile(options: { readonly resetBudget: boolean }): Promise export function reconcileLocalSecondaryEnvironments(): Promise { return runReconcile({ resetBudget: true }); } + +// Called by the settings page when the user enables/disables WSL. +// Flips the "no secondaries here" latch so the auto-retry loop wakes +// back up (or stays parked) accordingly. Asynchronously probes +// getWslState at module init so non-WSL hosts park immediately +// instead of burning ~4 minutes of polls before the budget runs out. +export function markSecondariesConfigured(configured: boolean): void { + knownNoSecondariesConfigured = !configured; +} + +if (typeof window !== "undefined") { + // Best-effort: any failure (no bridge, IPC error, missing field) + // keeps the default "stay polling" behavior intact. + void (async () => { + try { + const state = await window.desktopBridge?.getWslState(); + if (state && !state.enabled) { + knownNoSecondariesConfigured = true; + } + } catch { + // ignore — fall through to default polling + } + })(); +}