fix(desktop): probe the distro's ports before binding the WSL backend #7702
main fromrefs/pull/7702/head·opened View on GitHubWhat Changed#
resolveDesktopBackendPort now takes the set of ports the WSL distro already
listens on and skips them before probing the Windows host. DesktopWslEnvironment
learns that set in one wsl.exe -- cat /proc/net/tcp /proc/net/tcp6 round trip,
collecting the port of every row in state 0A (LISTEN) regardless of bind
address, since any listener there blocks the backend's 0.0.0.0 bind. A probe
that fails returns None, logs a warning naming the fallback, and the scan
reverts to today's Windows-only behavior rather than blocking startup.
The probe is budgeted at PRE_WARM_TIMEOUT (10s) rather than the 5s its
immediate neighbours in that file use. Those neighbours always run after the VM
is up, whereas in wsl-only mode this is the first wsl.exe call of the session
and pays the cold start; a warm round trip measures ~100ms here, so the headroom
only applies to a distro that is genuinely slow to boot. The call is guarded by
isAvailable, a cached filesystem check that is hardcoded false off win32, so a
machine without WSL never spawns it.
The choice happens in bootstrap, ahead of serverExposure.configureFromSettings,
because that is the port's only ingress: the renderer origin pinned by
registerDesktopProtocol, the advertised loopback and LAN endpoints, Tailscale
Serve, and the backend's own bind all derive from it. resolvePrimary runs again
on every backend restart, so correcting the port there would let it drift away
from an origin that is already registered and cannot be re-pointed.
buildWslPrimaryConfig still passes backendExposure.port verbatim.
The dual-mode secondary scan in DesktopWslBackend had the identical flaw and
gets the same treatment, one probe per scan rather than one per candidate port.
Non-WSL primaries pass an empty set and take the existing path unchanged. The
"Connecting to WSL" splash moved ahead of the probe, since the probe is what
cold-starts the VM, and the quitting check that gated the splash now gates the
probe as well — a bootstrap racing a quit does neither.
Tests: eight new cases across DesktopApp.test.ts (new file),
DesktopWslBackend.test.ts and DesktopWslEnvironment.test.ts. A distro-held
3773 yields 3774 and is never offered to canListenOnHost; the dual-mode
secondary skips distro-held ports and probes exactly once; the parser collects
LISTEN rows from both address families, ignores an ESTABLISHED row on the same
port, and returns None on garbage; and a TestClock case pins the probe budget
along with the degrade-to-unknown contract — still pending at 9s, None at 10s.
All eight fail without the fix. Targeted run over eight desktop suites: 81
passing. apps/desktop typecheck, lint and format
pass. The two failures already present on main (Net.findAvailablePort and the
ProviderRegistry codex re-probe) reproduce there and are untouched.
Why#
With wslOnly enabled the port is scanned from the Electron main process — the
Windows side — and handed to a backend that binds inside the distro. WSL2's
localhost forwarding does not reserve a WSL-side listener's port in the Windows
port namespace, so the two disagree and a Windows probe is not evidence about
the distro.
Anyone who installed the background service inside their distro hits this. The
service holds 3773, the scan logs selected backend port via sequential scan
having just declared 3773 free, and the backend dies on
listen EADDRINUSE: address already in use 0.0.0.0:3773
about every 20 seconds, forever, with nothing shown in the app. The renderer
surfaces it as an unhandled PrimaryEnvironmentRequestError thrown from a route
beforeLoad, which points at authentication rather than at a port.
Skipping the taken port rather than failing with a better message is the right call: the app can simply start on 3774, and someone running both the service and the desktop app wanted both to work.
Not addressed: the probe is not a guarantee, since something can take the port
between the probe and the launch, so a launch-time EADDRINUSE still deserves a
readable failure — a separate concern. wslDistroPortsProbed is logged so a
probe that failed is visible after the fact.
Checklist#
- This PR is small and focused
- I explained what changed and why
- I included before/after screenshots for any UI changes — N/A, the existing splash is unchanged, only its timing
- I included a video for animation/interaction changes — N/A, no motion changes
Changes by Claude Opus 5 running in Claude Code.
Note
Probe WSL distro ports before binding backend to prevent EADDRINUSE#
- Adds
probeListeningPortstoDesktopWslEnvironmentto discover listening TCP ports inside the WSL distro by parsing/proc/net/tcpand/proc/net/tcp6with a 10-second timeout. - Updates
resolveDesktopBackendPortandscanForWslPortto accept aportsHeldInDistroset and skip those ports during allocation. - In WSL-only mode, shows the 'Connecting to WSL' splash before probing to account for cold-boot delays.
- Risk: If
probeWslListeningPortstimes out or fails, it returnsNone, causing the app to fall back to the previous Windows-only loopback scan which may result inEADDRINUSEinside the distro.
- 1
github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the [Cursor dashboard](https://www.cursor.com/dashboard/bugbot).
Want fixes drafted automatically? Bugbot Autofix can create code changes for findings. A team admin can enable Autofix in the Cursor dashboard.
Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 92bd9af158bc0818e8cd00f6af34ffb55f5a290b. Configure [here](https://www.cursor.com/dashboard/bugbot).apps/desktop/src/wsl/DesktopWslEnvironment.ts line 805Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -723,0 +779,21 @@779+ stdin: "ignore",780+ stdout: "pipe",781+ stderr: "ignore",782+ killSignal: "SIGTERM",783+ forceKillAfter: PROCESS_TERMINATE_GRACE,784+ },785+ );786+ const handle = yield* spawner.spawn(command);787+ const stdoutBytes = yield* Stream.runCollect(handle.stdout);788+ // Deliberately not gated on the exit code: `cat` reports failure when a789+ // kernel without IPv6 has no /proc/net/tcp6, and the IPv4 table it did790+ // print is still worth reading. parseWslListeningPorts decides whether the791+ // output is a real table.792+ yield* handle.exitCode;793+ return parseWslListeningPorts(decodeUtf8(concatChunks(stdoutBytes)));794+ }),795+ ).pipe(796+ Effect.timeoutOption(LISTENING_PORTS_TIMEOUT),797+ Effect.map(Option.flatten),798+ Effect.orElseSucceed(() => Option.none<ReadonlySet<number>>()),799+ );Cold-start probe drops distro ports#
Medium Severity
In
wsl-onlybootstrap the listening-port probe is the firstwsl.execall, so it also cold-starts the VM, but it is capped at five seconds. Dual-modepreWarmallows ten. A timeout becomesNone, the scan skips no distro listeners, and the backend can still bind3773while the background service already holds it.Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 92bd9af158bc0818e8cd00f6af34ffb55f5a290b. Configure [here](https://www.cursor.com/dashboard/bugbot).Additional Locations (1)
Confirmed and fixed in
4cd11a5.The reasoning gap was real. I placed
probeListeningPortsImplnext togetDistroIpImpland inherited its budget by proximity, and that analogy does not hold:getDistroIpis called fromDesktopBackendConfiguration.ts:505, afterrunWslPreflightand afterbuildWslPrimaryConfig'spreWarm, whilegetUserHomebacks the folder-picker IPC long after startup. Both 5s callers are always warm-VM calls. This probe, in wsl-only bootstrap, is the firstwsl.execall of the session.LISTENING_PORTS_TIMEOUTis nowPRE_WARM_TIMEOUT(10s) — this repo's considered cold-start budget, whichDesktopWslBackendalready uses so "the readiness probe doesn't race wsl.exe's first-spawn cold start".Measured on the reporting machine, warm: ~100ms across five runs, and the probe does see the 3773 listener. I could not measure a cold start, since forcing one needs
wsl --shutdownand that would have killed the developer's live distro and the service under investigation — so the 10s figure is the repo's existing budget, not an observation of mine.Pre-warming before probing was the other candidate. I rejected it as redundant:
wsl.exe -d X -- sh -c ...boots the VM exactly aswsl.exe -- truedoes, so it would double the round trips and the worst case for identical protection.One thing your finding surfaced beyond the timeout itself: the degradation was silent. A probe that returns
Nonenow logs a warning on the wsl-only path naming the Windows-only fallback, so an EADDRINUSE death afterwards has a line explaining why we did not know better. ATestClockcase pins both the budget and the degrade-to-unknown-never-to-empty contract (still pending at 9s,Noneat 10s); it fails at the old 5s value.Approvability#
Verdict: Not approved
Macroscope's review found this PR not approvable — This PR introduces new WSL port-probing functionality (parsing /proc/net/tcp via wsl.exe) and changes the bootstrap flow and port selection logic at multiple points. Despite the 'fix' label, this is a new capability with cross-platform complexity that warrants human review.
You can add or adjust custom eligibility rules. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).What happened#
T3 Code worked before an update. After updating, the Windows desktop app failed to open with
PrimaryEnvironmentRequestError, caused byDesktopLocalEnvironmentAuthSessionBootstrapError.Diagnosis#
An older T3 backend remained running in Ubuntu WSL with PID 465404 and parent PID 1. It had started on October 12 and held
0.0.0.0:3773.The updated desktop repeatedly launched a new WSL backend on port 3773. Each attempt failed with
EADDRINUSEand exited with code 1.At commit
98a6246d07624432a6e6d0ab4fb87bede90a49a6,DesktopApp.ts:77checks port availability from the Windows desktop process.DesktopLocalEnvironmentAuth.ts:116exchanges the desktop bootstrap credential with the configured backend. The port conflict prevents the intended backend from starting.Why the older backend survived the update remains unconfirmed.
Steps to reproduce#
Observed sequence; not yet reproduced independently:
- Run the Windows nightly desktop with an Ubuntu WSL backend.
- Update the desktop while an older backend survives.
- Open the updated desktop.
- Startup fails; backend logs repeatedly report port 3773 already in use.
Version#
0.0.46-nightly.20261013.3066Environment#
Windows x64, OS build
10.0.26300; Ubuntu WSL backend. Triage CLI Node version:v26.8.2.Evidence#
Failure time: 2026-10-13T16:54:34.873Z PrimaryEnvironmentRequestError DesktopLocalEnvironmentAuthSessionBootstrapError: Failed to create the local desktop bearer session Backend log: listen EADDRINUSE: address already in use 0.0.0.0:3773 backend exited with code=1 Existing listener: PID 465404, PPID 1 <WSL_HOME>/.t3/wsl-runtime/<older-runtime-hash>/t3 --bootstrap-fd 0 LISTEN 0.0.0.0:3773Related issues#
#7702 describes the same WSL port-selection failure. #13747 describes orphaned desktop backends retaining their ports.
Fix applied#
Executed:
wsl -d Ubuntu --exec kill -TERM 465404Verified that the old process exited and port 3773 became free. After reopening the desktop, the user confirmed T3 Code opens successfully. A new backend (PID 653109) is listening on port 3773. No settings or database changes were made.
Produced by#
Codex, gpt-6.1-sol, via
t3 triage.
Sign in to join this conversation.

Important
Review skipped#
Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
.coderabbit.yamlfile in this repository. To trigger a single review, invoke the@coderabbitai reviewcommand.⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID:
23951f5b-946a-48b7-b615-7017a868f990You can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.Use the checkbox below for a quick retry:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share