Repository navigation
fix(wsl): tree-kill a timed-out wsl.exe instead of only its root (#19316) - #19319
marcdhansen wants to merge 1 commit into
Conversation
…blyai#19316) `runWslProcess` and `probeGuestEnvironment` both spawn `wsl.exe` through `runProcess` with a `timeoutMs` and no `terminationBarrier`. On the non-barrier path `terminate()` (`run-process.ts:310`) is a bare `child.kill(signal)`, so a timeout reaps the `wsl.exe` root and nothing else. With a barrier, `stopAndSettle` calls `signalBarrierTree()` before any `terminate(child)`, so `taskkill /pid <root> /t /f` runs while the root is still alive and takes the Windows-side descendants with it. Orca already tree-kills `wsl.exe` in production: git's WSL lane threads `terminationBarrier` through all four `binary: 'wsl.exe'` call sites in `git/command-runner/wsl-command-resolution.ts`. The WSL runner did not. This is that wiring, not new machinery. The probe site matters more than its size suggests: on a wedged distro it is what fails FIRST, and it is what pushes every later call onto the shell-free lane, so it sits on the critical path of every occurrence. `TRANSIENT_RETRY_MS` throttles it to roughly one attempt per 5s per distro — a standing residue for as long as the wedge lasts. Not a missing timeout: `runWslProcess` already computes `deadline = Date.now() + (spec.timeoutMs ?? DEFAULT_WSL_TIMEOUT_MS)` with `DEFAULT_WSL_TIMEOUT_MS = 30_000`. Adding a timeout would be a no-op. The defect is what the timeout kills. Observed on Windows 10 Pro 19045: a distro wedged (`wsl --list` reported Running while every `--exec` hung) and accumulated 8,731 orphaned conhosts / 9,073 processes / 1.38M handles over two days, after which process creation itself cost tens of seconds. Stated as observation, not proven mechanism. 8,731 conhosts against only 31 stuck `wsl.exe` means the root died in nearly every case and the console host outlived it anyway, and I could not reproduce that on a healthy distro: tracking each conhost by pid and creation time, it died in 6/6 attributed cases under BOTH root-only `TerminateProcess` and `taskkill /t /f`. Something wedge-specific holds the console open. What the code supports is narrower and sufficient — a root-only kill cannot reap whatever holds the console; a tree-kill while the root lives can. Scope: `taskkill /t` is a Windows PPID walk. It reaps conhost and Windows-side descendants, never the hung guest-side process inside the distro, and it will not reap an already kernel-wedged `wsl.exe`. Tests assert the flag on both the `loginPath: 'preferred'` and `loginPath: 'none'` lanes — the latter because that degraded lane is the one a wedged distro actually takes. Same seam-contract shape the repo already uses for git (`runner-wsl-direct-read.test.ts:290`, `:323`). Co-Authored-By: Claude Opus 5 <[email protected]>
📝 WalkthroughWalkthroughWSL guest probing and script execution now pass Merge Risk: 🔵 Low · up to WSL timeouts now terminate the full process tree, reducing orphaned Windows-side processes. The probe path lacks a direct regression assertion for this setting, so a future change could silently remove that protection. 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Team
Run ID: 7978c3e2-918f-42c0-8e12-6b3e4801f7ed
📒 Files selected for processing (3)
src/main/wsl/wsl-guest-environment.tssrc/main/wsl/wsl-runner.test.tssrc/main/wsl/wsl-runner.ts
Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.
| seedWslGuestEnvironmentForTests(undefined, ENVIRONMENT) | ||
| await runWslProcess({ loginPath: 'preferred', shell: 'bash', script: 'true' }) |
There was a problem hiding this comment.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
#!/bin/bash
set -euo pipefail
rg -n -C 5 \
'getWslGuestEnvironment|probeGuestEnvironment|terminationBarrier' \
src/main/wsl --glob '*.test.ts'Repository: stablyai/orca
Length of output: 13789
🏁 Script executed:
#!/bin/bash
set -euo pipefail
printf '%s\n' '--- changed files ---'
git diff --stat
printf '%s\n' '--- relevant diff ---'
git diff -- src/main/wsl/wsl-runner.test.ts src/main/wsl/wsl-runner.ts src/main/wsl/wsl-guest-environment.ts src/main/wsl/wsl-guest-environment.test.ts
printf '%s\n' '--- runner implementation and imports ---'
sed -n '1,240p' src/main/wsl/wsl-runner.ts
printf '%s\n' '--- guest-environment implementation ---'
sed -n '1,280p' src/main/wsl/wsl-guest-environment.ts
printf '%s\n' '--- guest-environment test setup and probe assertions ---'
sed -n '1,210p' src/main/wsl/wsl-guest-environment.test.tsRepository: stablyai/orca
Length of output: 27034
🤖 get_repo_knowledge executed:
get_repo_knowledge stablyai/orca /tmp/coderabbit-repo-knowledge/stablyai-orca-89dc44e4/learnings /tmp/coderabbit-repo-knowledge/stablyai-orca-89dc44e4/conventions
Length of output: 19643
Add an assertion for terminationBarrier in the guest-environment probe tests.
The preferred-login test seeds the environment, and the loginPath: 'none' test skips probing. The direct probe tests call getWslGuestEnvironment, but do not inspect the mocked runProcess options. A removed terminationBarrier would therefore pass. Assert terminationBarrier: true in src/main/wsl/wsl-guest-environment.test.ts.
There was a problem hiding this comment.
ℹ️ No critical issues — a correct, well-scoped fix with two small nits inline.
Reviewed changes
terminationBarrier: trueon bothwsl.exespawn sites —runWslProcess(wsl-runner.ts) andprobeGuestEnvironment(wsl-guest-environment.ts) now tree-kill on timeout viataskkill /pid <root> /t /finstead of the barechild.kill()the non-barrier path uses, so a timed-outwsl.exeno longer strands its Windows-side descendants (theconhost.exeleak in #19316).- Two seam-contract tests —
wsl-runner.test.tsasserts the flag on both theloginPath: 'preferred'andloginPath: 'none'lanes, the latter being the degraded lane a wedged distro actually takes.
DeepSeek Pro (free via Pullfrog for OSS) | 𝕏
| // the critical path of every occurrence. `TRANSIENT_RETRY_MS` throttles it to | ||
| // roughly one attempt per 5s per distro, which is a standing residue for as | ||
| // long as the wedge lasts. | ||
| terminationBarrier: true |
There was a problem hiding this comment.
This is the site the writeup calls the one that "fails first" on a wedged distro, yet unlike the runner it gets no test in this PR. wsl-guest-environment.test.ts already mocks runProcess, so a symmetric expect(spec.terminationBarrier).toBe(true) after a probe timeout would pin it the same way.
| // (`git/command-runner/wsl-command-resolution.ts`) -- so this is wiring, not | ||
| // new machinery or a new risk. | ||
| // | ||
| // Observed: a wedged distro (`wsl --list` reports Running while every |
There was a problem hiding this comment.
The Observed: paragraph (exact conhost counts, Windows build, the 6/6 healthy-distro experiment) is incident archaeology that belongs in the issue/PR, not a permanent code comment — it reads stale the moment those numbers are secondhand. Keep the Why: mechanism paragraph and trim this one down to a sentence.
|
Additional data point: You noted you couldn't reproduce on a healthy distro — I have that case, though with Environment: Windows 11 26100.9106, WSL 2.7.13.0, Orca 1.4.197, several agent sessions on worktrees that live inside the WSL filesystem ( Measured (distro confirmed healthy — t= 0s wslhost=12 orphaned=8 ~5 orphaned Amplifier: Honest limits: I have not tied each leaked |

Fixes #19316.
Orca already tree-kills
wsl.exein production — git's WSL lane threadsterminationBarrierthrough all fourbinary: 'wsl.exe'call sites insrc/main/git/command-runner/wsl-command-resolution.ts(lines 101, 132, 144, 156). The WSL runner does not. That asymmetry is this report.The two sites
Both spawn
wsl.exeviarunProcesswith atimeoutMsand noterminationBarrier:src/main/wsl/wsl-runner.ts:240(runWslProcess)src/main/wsl/wsl-guest-environment.ts:87(probeGuestEnvironment)On the non-barrier path
terminate()(src/shared/child-process/run-process.ts:310) is a barechild.kill(signal), so a timeout reaps thewsl.exeroot and nothing else. With a barrier,stopAndSettlecallssignalBarrierTree()before anyterminate(child)(run-process.ts:184-186), sotaskkill /pid <root> /t /fruns while the root is still alive and reaps the Windows-side descendants with it.The probe site matters more than its size suggests: on a wedged distro it is what fails first, and it is what pushes every later call onto the shell-free lane.
TRANSIENT_RETRY_MS = 5_000throttles it to roughly one attempt per 5s per distro, which is a standing residue for as long as the wedge lasts.Note this is not the
wsl.exetimeout being absent —runWslProcesscomputesdeadline = Date.now() + (spec.timeoutMs ?? DEFAULT_WSL_TIMEOUT_MS)withDEFAULT_WSL_TIMEOUT_MS = 30_000. Adding a timeout would be a no-op. The defect is what the timeout kills.What was observed
Windows 10 Pro 19045. A WSL Ubuntu distro wedged —
wsl --listreported it "Running" while everywsl -d Ubuntu --exec ...hung indefinitely. Over ~2 days:conhost.exe, 9,073 total processes, 1.38M handlesclaudewould not launch from a terminalwsl.exe, oldest 56 hoursSampling process creation over 151s during the leak:
wsl.exe26 new / 11 conhosts,git.exe10 / 6,taskkill.exe7. Thattaskkilltraffic is the git lane tree-killing correctly — both behaviours were visible side by side in one measurement.Honest limits on the mechanism claim
I am not claiming "one orphaned conhost per probe" as a proven mechanism, and the numbers argue against the simple version: 8,731 conhosts against only 31 stuck
wsl.exemeans the root died in the overwhelming majority of cases and the console host outlived it anyway.I tried to reproduce it on a healthy distro and failed to: spawning
wsl -d Ubuntu --exec sleep 1000, then killing the root two ways and tracking each conhost by PID + creation time, the conhost died in 6/6 attributed cases under both root-onlyTerminateProcessandtaskkill /t /f. (An earlier before/after count comparison appeared to show a difference; that was background churn — repeat runs produced negative "leaks". Discard it.)So: something wedge-specific holds the console open, and I could not isolate it on a healthy box. What the code alone supports is narrower and still sufficient — a root-only kill cannot reap whatever holds the console; a tree-kill issued while the root is alive can. The 8,731 figure is the observation that prompted the search, not proof of the mechanism.
Patch
Both sites, identical one-liner:
Tests added in
src/main/wsl/wsl-runner.test.tsassertingspec.terminationBarrier === trueon both theloginPath: 'preferred'andloginPath: 'none'lanes — the latter because that degraded lane is the one a wedged distro actually takes. Same seam-contract shape the repo already uses for git (runner-wsl-direct-read.test.ts:290,:323).Verified locally:
src/main/wsl→ 153 passed, 11 skipped; oxlint and oxfmt clean. Reverting the source and keeping the tests → 2 fail (expected undefined to be true), so they guard the behaviour rather than restate it. I could not run the full CI gate on this box.Disclosures
taskkill /tis a Windows PPID walk. It reapsconhost.exeand Windows-side descendants, not the hung guest-side process inside the distro. It also will not reap an already kernel-wedgedwsl.exe— the 31 stale ones needed a distro restart.PROCESS_EXIT_GRACE_MS = 2_000+BARRIER_UNVERIFIED_EXIT_GRACE_MS = 10_000) versus timeout + 2s unconditionally before. On win32 taskkill normally verifies and settles immediately, so this is a note rather than a regression.spawn-resolution.ts:40addsdetached: trueonly whenplatform !== 'win32'; behaviour is identical up to the moment of termination.own-chromium-tree-kill-guard.ts:44-45refuses only pids inreadOrcaChromiumProcessPids(); a livewsl.exechild handle is never one.signalProcessTreealready guards recycled pids viahasExited(child)before any taskkill.spawnWslRelayProcess(wsl-hook-relay-launch.ts:138) uses rawspawn, neverrunProcess, and is a sibling of the probes rather than a descendant, so no probe tree-kill can reach it.Same class, not fixed here
src/main/wsl/wsl.tsusesexecFile/execFileSynctimeouts, which also root-kill::77/:99(wslUncDirectoryExists[Async]) and:287/:321(getWslHome[Async]). The host-levelwsl --listprobes at:226/:249are likely innocent —wsl --listkept working throughout this incident. Unrelated, but adjacent:wsl.ts:98passes nowindowsHide, so that probe can flash a visible console.Suggested poka-yoke
An invariant over
src/main/wsl/requiring everyrunProcessspawn ofwsl.exeto carry a barrier.wsl-guest-environment.tsescapedwsl-invocation-boundary.test.tsonly because that guard exempts its own owner directory (line 33), so it was on no allowlist and nothing tracked it. The repo already likes this shape — that boundary test is exactly it.Verification in this PR:
src/main/wsl→ 153 passed, 11 skipped; oxlint and oxfmt clean. Reverting the source and keeping the tests → 2 fail (expected undefined to be true). I could not run the fullpnpm lint/typecheck/test/buildgate on this box, so CI is the arbiter.🤖 Generated with Claude Code