Skip to content

fix(wsl): tree-kill a timed-out wsl.exe instead of only its root (#19316) - #19319

Open
marcdhansen wants to merge 1 commit into
stablyai:mainfrom
marcdhansen:fix/wsl-timeout-tree-kill
Open

marcdhansen wants to merge 1 commit into
stablyai:mainfrom
marcdhansen:fix/wsl-timeout-tree-kill

Conversation

@marcdhansen

Copy link
Copy Markdown
Contributor

Fixes #19316.

Orca already tree-kills wsl.exe in production — git's WSL lane threads terminationBarrier through all four binary: 'wsl.exe' call sites in src/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.exe via runProcess with a timeoutMs and no terminationBarrier:

site what it is
src/main/wsl/wsl-runner.ts:240 (runWslProcess) the single place Orca runs a program inside WSL
src/main/wsl/wsl-guest-environment.ts:87 (probeGuestEnvironment) the login-PATH probe

On the non-barrier path terminate() (src/shared/child-process/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) (run-process.ts:184-186), so taskkill /pid <root> /t /f runs 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_000 throttles 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.exe timeout being absent — runWslProcess 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.

What was observed

Windows 10 Pro 19045. A WSL Ubuntu distro wedged — wsl --list reported it "Running" while every wsl -d Ubuntu --exec ... hung indefinitely. Over ~2 days:

  • 8,731 orphaned conhost.exe, 9,073 total processes, 1.38M handles
  • Accumulation: 12 (Sep 4) → 2,483 (Sep 5) → 6,231 (Sep 6)
  • Process creation itself then cost tens of seconds; claude would not launch from a terminal
  • 31 stuck wsl.exe, oldest 56 hours

Sampling process creation over 151s during the leak: wsl.exe 26 new / 11 conhosts, git.exe 10 / 6, taskkill.exe 7. That taskkill traffic 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.exe means 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-only TerminateProcess and taskkill /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:

    timeoutMs: remainingMs,
    maxOutputBytes: spec.maxOutputBytes,
+   terminationBarrier: true

Tests added in src/main/wsl/wsl-runner.test.ts asserting spec.terminationBarrier === true 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).

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

  • Scope. taskkill /t is a Windows PPID walk. It reaps conhost.exe and Windows-side descendants, not the hung guest-side process inside the distro. It also will not reap an already kernel-wedged wsl.exe — the 31 stale ones needed a distro restart.
  • Latency. Where taskkill cannot verify termination, the call now settles up to 12s later (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.
  • No spawn-side change on Windows. spawn-resolution.ts:40 adds detached: true only when platform !== 'win32'; behaviour is identical up to the moment of termination.
  • The admission gate cannot refuse this. own-chromium-tree-kill-guard.ts:44-45 refuses only pids in readOrcaChromiumProcessPids(); a live wsl.exe child handle is never one. signalProcessTree already guards recycled pids via hasExited(child) before any taskkill.
  • The relay is unaffected. spawnWslRelayProcess (wsl-hook-relay-launch.ts:138) uses raw spawn, never runProcess, 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.ts uses execFile/execFileSync timeouts, which also root-kill: :77/:99 (wslUncDirectoryExists[Async]) and :287/:321 (getWslHome[Async]). The host-level wsl --list probes at :226/:249 are likely innocent — wsl --list kept working throughout this incident. Unrelated, but adjacent: wsl.ts:98 passes no windowsHide, so that probe can flash a visible console.

Suggested poka-yoke

An invariant over src/main/wsl/ requiring every runProcess spawn of wsl.exe to carry a barrier. wsl-guest-environment.ts escaped wsl-invocation-boundary.test.ts only 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 full pnpm lint/typecheck/test/build gate on this box, so CI is the arbiter.

🤖 Generated with Claude Code

…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]>
@coderabbitai

coderabbitai Bot commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

WSL guest probing and script execution now pass terminationBarrier: true to runProcess. A timeout therefore terminates the full WSL process tree. Tests verify this behavior for both the resolved guest-environment lane and the degraded loginPath: 'none' lane.

Merge Risk: 🔵 Low · up to 7e062

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)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 2 functions across 3 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the primary change: tree-killing timed-out wsl.exe processes instead of only terminating the root process.
Description check ✅ Passed The description provides detailed scope, rationale, linked issue, implementation details, tests, limitations, and verification results. It does not reproduce every template heading or explicitly state…
Linked Issues check ✅ Passed The changes satisfy issue #19316. Both relevant wsl.exe runProcess sites now use terminationBarrier: true, existing timeout behavior is preserved, and tests cover the preferred-login and shell-free de…
Out of Scope Changes check ✅ Passed The changes are limited to the two WSL process-launch sites and regression tests directly related to preventing root-only termination leaks. No unrelated code changes are identified.
  • Fix all pre-merge checks with AI

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

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Team

Run ID: 7978c3e2-918f-42c0-8e12-6b3e4801f7ed

📥 Commits

Reviewing files that changed from the base of the PR and between 83b1558 and 7e06233.

📒 Files selected for processing (3)
  • src/main/wsl/wsl-guest-environment.ts
  • src/main/wsl/wsl-runner.test.ts
  • src/main/wsl/wsl-runner.ts

Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.

Comment on lines +164 to +165
seedWslGuestEnvironmentForTests(undefined, ENVIRONMENT)
await runWslProcess({ loginPath: 'preferred', shell: 'bash', script: 'true' })

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📐 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.ts

Repository: 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.

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ No critical issues — a correct, well-scoped fix with two small nits inline.

Reviewed changes

  • terminationBarrier: true on both wsl.exe spawn sites — runWslProcess (wsl-runner.ts) and probeGuestEnvironment (wsl-guest-environment.ts) now tree-kill on timeout via taskkill /pid <root> /t /f instead of the bare child.kill() the non-barrier path uses, so a timed-out wsl.exe no longer strands its Windows-side descendants (the conhost.exe leak in #19316).
  • Two seam-contract tests — wsl-runner.test.ts asserts the flag on both the loginPath: 'preferred' and loginPath: 'none' lanes, the latter being the degraded lane a wedged distro actually takes.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using 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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@jaydcrowe1989

Copy link
Copy Markdown

Additional data point: wslhost.exe accumulation on a healthy distro (Windows 11 26100, WSL 2.7.13)

You noted you couldn't reproduce on a healthy distro — I have that case, though with wslhost.exe rather than conhost.exe, so it may be a sibling of this defect rather than the same site.

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 (\\wsl.localhost\Ubuntu-24.04\...).

Measured (distro confirmed healthy — wsl -d Ubuntu-24.04 --exec echo returns instantly throughout): with no wsl invocations of my own, purely Orca background activity, wslhost.exe climbed steadily over 3 minutes:

t= 0s wslhost=12 orphaned=8
t= 60s wslhost=15 orphaned=11
t=120s wslhost=23 orphaned=15
t=180s wslhost=28 orphaned=21

~5 orphaned wslhost.exe/min, where "orphaned" = ParentProcessId no longer a live PID. Earlier in the day (during a genuine wedge — wsl --list "Running" but every --exec hanging, matching your report) this had reached 306 orphaned wslhost.exe (~3.4 GB) and wsl / wsl --shutdown themselves hung until the backlog was killed.

Amplifier: .wslconfig with networkingMode=mirrored + [experimental] hostAddressLoopback=true made it markedly worse; reverting to default NAT reduced but did not eliminate the wslhost accumulation.

Honest limits: I have not tied each leaked wslhost to a specific call site, and can't distinguish the runner/probe paths (#19319 / #19341) from the relay (spawnWslRelayProcess, which you note is out of scope) as the source. Every leaked host shared one --vm-id with a unique dead parent, consistent with one short-lived wsl.exe per orphan. Flagging in case the healthy-distro reproduction and the wslhost (vs conhost) angle are useful for scoping whether the relay path needs the same barrier.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Windows: WSL runner and guest-environment probe time out with a root-only kill, so a wedged distro leaks console hosts (git already tree-kills wsl.exe)

2 participants