Skip to content

fix(wsl): tree-kill timed-out wsl.exe probes on the execFile path - #19341

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

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

Conversation

@marcdhansen

Copy link
Copy Markdown
Contributor

Same defect class as #19319, different mechanism. #19319 fixed the runProcess path (terminationBarrier); this fixes the execFile/execFileSync path in src/main/wsl.ts, where Node's timeout kills only the direct child and leaves the console host behind on Windows.

Sites

  • wslUncDirectoryExists / wslUncDirectoryExistsAsync
  • listWslDistros (wsl --list --quiet)
  • getWslHome / getWslHomeAsync

windowsHide: true was also absent on three of these, so the probes could flash a visible console.

Sync vs async, and why not a single mechanism

The async paths migrate to runProcess + terminationBarrier: true. The sync paths cannot: runProcessSync hands timeoutMs straight to spawnSync, which has the identical root-only-kill defect — there is no termination barrier on the sync path, so migrating them would fix nothing. They instead get a bounded, synchronous taskkill /pid <pid> /t /f in the catch block, routed through the same admitProcessTreeKill guard signalProcessTree uses, because by the time execFileSync throws ETIMEDOUT the root is already reaped.

On listWslDistros

An earlier draft of this work proposed exempting the wsl --list probes, on the grounds that wsl --list kept working during the incident that prompted #19316. That is an empirical fact about one wedge, not a structural difference — its option literal is byte-identical to the other two sync sites. It gets the same fix.

Verification

  • Red: source stashed, tests kept → Test Files 4 failed, Tests 25 failed | 41 passed.
  • Green: src/main/wsl → 178 passed | 11 skipped, independently re-run by the author of this PR.
  • oxlint clean; oxfmt --check clean after formatting; check-max-lines-ratchet.mjs OK (execFileUtf8 extracted to wsl-exec-utf8.ts to stay inside the 300-line budget).

Note for reviewers: corepack pnpm on a Windows checkout currently re-triggers postinstall and fails a node-gyp rebuild of @vscode/windows-process-tree, unrelated to this change; the suite was run via the vitest binary directly.

Relates to #19316.

🤖 Generated with Claude Code

…anism)

`src/main/wsl.ts` spawns `wsl.exe` via `execFile`/`execFileSync` with a
`timeout`. That timeout kills only the direct child -- it does not walk the
process tree -- so on Windows the console host and any Windows-side
descendants survive. This is the same defect class fixed in stablyai#19319 for
`runWslProcess`/`probeGuestEnvironment` (`terminationBarrier: true` on the
`runProcess` path), but on a different mechanism: an `execFile`/`execFileSync`
timeout rather than `runProcess`'s non-barrier `terminate()`.

Async sites migrated to `runProcess` + `terminationBarrier: true`
(feasible -- `runProcess` is async, and windowsHide comes free via
`resolveSpawn`):
  - `wslUncDirectoryExistsAsync`
  - `execFileUtf8` (shared helper, extracted to wsl-exec-utf8.ts to keep
    wsl.ts under its 300-line budget), used by `listWslDistrosAsync`,
    `listRunningWslDistrosAsync`, `getWslHomeAsync`

Sync sites (`wslUncDirectoryExists`, `listWslDistros`, `getWslHome`) stay on
`execFileSync` and get a bounded, best-effort `taskkill /t /f` fallback
instead (new `wsl-timeout-tree-kill.ts`). Migration is not feasible there:
`runProcessSync` hands `timeoutMs` straight to Node's `spawnSync`, which has
the identical root-only-kill defect -- there is no termination barrier on the
sync path, so switching to it would fix nothing. The fallback taskkill is
gated through `admitProcessTreeKill` (`win-taskkill-tree` scope), the same
pid-recycling guard `signalProcessTree` uses, since by the time
`execFileSync` throws `ETIMEDOUT` the root is already reaped and its pid is
back in Windows' recycling pool.

`listWslDistros` (`wsl --list --quiet`) gets the identical fix, not an
exemption: its execFileSync option literal is byte-identical to
`wslUncDirectoryExists`'s and `getWslHome`'s (`stdio`, `timeout: 5000`,
`cwd`). `wsl --list` staying up during the original incident was an
empirical observation about that one wedge, not a structural property of
this code path -- nothing distinguishes it from the other two probes.

Also: `windowsHide` was absent on all three sync sites (only
`execFileUtf8` set it), so a hung probe could flash a visible console
window. Added to all three in the same commit -- same option literal.

Red-green: stashed wsl.ts + wsl-timeout-tree-kill.ts (source only, kept the
new/updated tests) and ran
`corepack pnpm vitest run --config config/vitest.config.ts src/main/wsl.test.ts
src/main/wsl-timeout-tree-kill.test.ts src/main/wsl-running-distros.test.ts
src/main/wsl-distro-list-single-flight.test.ts` against that reverted tree:
25 failed / 41 passed (4 files). Restored the source and re-ran: 168 passed /
11 skipped, 0 failed, across the full `src/main/wsl` suite.

Co-Authored-By: Claude Opus 5 <[email protected]>
@coderabbitai

coderabbitai Bot commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Team

Run ID: e044057f-ce5b-45da-b07a-7552c4e63653

📥 Commits

Reviewing files that changed from the base of the PR and between 1d1b73c and 8b0fae2.

📒 Files selected for processing (7)
  • src/main/wsl-distro-list-single-flight.test.ts
  • src/main/wsl-exec-utf8.ts
  • src/main/wsl-running-distros.test.ts
  • src/main/wsl-timeout-tree-kill.test.ts
  • src/main/wsl-timeout-tree-kill.ts
  • src/main/wsl.test.ts
  • src/main/wsl.ts

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


📝 Walkthrough

Walkthrough

The change migrates asynchronous WSL probes from callback-based execFile calls to promise-based runProcess calls. It adds the shared execFileUtf8 adapter. It adds Windows timeout cleanup that invokes bounded taskkill tree termination. Tests now cover process results, single-flight discovery, retry behavior, timeout cleanup, and synchronous probe options.

Merge Risk: ⚪ Minimal · up to 8b0fa

Timed-out WSL probes now clean up process trees through the intended async and synchronous paths, with no unresolved material risk identified.

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description explains the defect, remediation strategies, affected probes, and verification results. It does not follow the required template structure and omits required or incomplete sections, in… Rewrite the description using every required template heading. Add an ELI5 summary, structured change and rationale sections, a Linked Issue entry using Fixes #<issue>, Visual Proof with N/A and a reason, testing checklist status and pl…
Docstring Coverage ⚠️ Warning Docstring coverage is 64.29% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 14 functions across 7 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the WSL timeout tree-kill fix and the affected execFile path. It is concise and related to the main change, although it does not mention the async runProcess changes.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Full details: Description check

Explanation

The description explains the defect, remediation strategies, affected probes, and verification results. It does not follow the required template structure and omits required or incomplete sections, including ELI5, What Changed, Linked Issue with Fixes #, Visual Proof with N/A, Review, Notes, the required checklists, and explicit AI Disclosure details.

Resolution

Rewrite the description using every required template heading. Add an ELI5 summary, structured change and rationale sections, a Linked Issue entry using Fixes #&lt;issue&gt;, Visual Proof with N/A and a reason, testing checklist status and platform details, AI Disclosure details, Review, Agent skill upstream boundary status, Notes covering the listed compatibility areas, and the remaining checklist items. Preserve the existing technical explanation and verification results.

  • 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.

@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.

Important

This PR breaks the child_process import boundary — src/shared/child-process/child-process-import-boundary.test.ts fails with two assertions, so CI will block merge.

Reviewed changes

  • Sync-probe tree-kill fallback — a new killIfTimedOut / killTimedOutWslProcessTree (wsl-timeout-tree-kill.ts) walks the wsl.exe tree after execFileSync reaps the root, gated through admitProcessTreeKill.
  • Async-probe migration — execFileUtf8 (extracted to wsl-exec-utf8.ts) and wslUncDirectoryExistsAsync move to runProcess with terminationBarrier: true, inheriting the #19319 tree-kill and windowsHide.
  • Sync-probe windowsHide — added to wslUncDirectoryExists, listWslDistros, and getWslHome, which previously flashed a console.

The approach is sound: the async paths reuse the existing termination barrier, and the sync paths correctly observe that runProcessSync has the identical root-only-kill defect it would solve nothing by migrating to. The residual recycled-pid risk on the sync taskkill is honestly documented and bounded by the same own-Chromium gate signalProcessTree uses. The one blocking problem is a repo-boundary violation, not a logic error.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using DeepSeek Pro (free via Pullfrog for OSS) | 𝕏

@@ -0,0 +1,55 @@
import { spawnSync } from 'node:child_process'

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.

import { spawnSync } from 'node:child_process' is a new direct child_process importer outside src/shared/child-process/, which breaks child-process-import-boundary.test.ts (I confirmed locally: 2 assertions fail — "has no direct importer outside the allowlist" and "holds the offender count at the pin", because this file becomes offender #156 against a pin of 155 and is absent from the allowlist). Route the taskkill through the chokepoint instead.

Technical details
# taskkill spawn must go through the child-process chokepoint

## Affected sites
- src/main/wsl-timeout-tree-kill.ts:1 — direct `node:child_process` import
- src/main/wsl-timeout-tree-kill.ts:37 — the raw `spawnSync('taskkill', ...)`

## Required outcome
- No new direct `child_process` import outside `src/shared/child-process/`; `child-process-import-boundary.test.ts` green.

## Suggested approach
- Replace the raw `spawnSync` with `runProcessSync({ program: 'taskkill', args: ['/pid', String(pid), '/t', '/f'], timeoutMs: 2000 })` from `src/shared/child-process/run-process`. The PR's own caveat (that `runProcessSync` has the root-only-kill defect) applies to the *probe*, not the kill — a `taskkill` is the kill itself, so there is no tree to terminate behind it. `runProcessSync` already resolves `windowsHide`/`shell:false` and swallows `ETIMEDOUT` into `timedOut`, which is fine for a best-effort kill.
- Or move the synchronous tree-kill into `src/shared/child-process/process-tree-termination.ts` (the owner directory, which is exempt) next to the existing async `taskkillTree`, and import it from `wsl-timeout-tree-kill.ts`. Do not add `src/main/wsl-timeout-tree-kill.ts` to the allowlist — the ratchet only shrinks and the offender pin (155) is a literal, so an allowlist addition alone still fails.

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.

1 participant