You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix(wsl): never boot a terminated distro off a list taken while it was still stopping - #35143
When you stop a WSL distro (wsl --terminate) while Orca has terminals open in it, Orca could start the distro again a few seconds later on its own. Orca must never boot a distro in the background (rule E9). The cause is that WSL keeps listing a distro as "running" for a moment while it shuts down, and Orca trusted those mid-shutdown lists. Now it doesn't.
What Changed
A bridge pane's exit marks its distro as possibly stopped.WslBridgeBootIdentity.noteExit, which runs synchronously on every bridge exit, now calls noteWslDistroMayHaveStopped. Every background "is it running?" check for that distro (boot id, UNC, file watcher, guest pane processes) then needs a list started after the stop has settled (WSL_STOP_SETTLE_MS). Its own stop-observation listener now only drops the cached boot id, as before.
The host-loss verdict reads a settled list.checkWslBridgeDistro anchors its reads at settledWslRunningReadAnchor(distro, exitAt), newly exported from the gate (it is the gate's existing settle logic). A distro that is still stopping now reads as lost, not running.
The boot-id reread is settled.rebootedSinceSpawn keeps using isWslDistroRunningForBackgroundWork. Because of (1), that check now needs a list started after the exit settled, so wsl.exe -d <distro> --exec cat …/boot_id can't run off a mid-stop list.
Deferred work drains only on settled lists.onConfirmedRunningWslDistros now also passes the probe's start time. The gate drains deferred work (deferUntilWslDistroRunning: file-watcher retries, hook-relay restarts, history-tombstone cleanup, CLI registration repair) only for distros whose stop had settled before that probe started.
The hook-relay restart re-reads a settled list. When deferred work drains WslRelayRecovery's restart, it now reads a settled running list (isDistroRunning(distro, now)) right before spawning the relay through wsl.exe. If the distro has stopped, it waits again.
Setup-script import inspection is gated. It runs in the background (sidebar prompt card, runtime RPC repo.setupScriptImports). Both hooks:inspectSetupScriptImports and inspectRepoSetupScriptImports now return [] without opening anything when blockedWslDistroForPath(repo.path) reports the distro stopped or unverifiable.
A clean exit closes at once. A bridge pane's code-0 exit is the user's own; awin shows a terminate exits -1. It now finishes immediately, with no settle wait or running-list read. It still marks the distro as possibly stopped. The user doesn't see that mark, and it covers an in-guest poweroff or wsl --shutdown that exits 0. Non-zero exits keep the full E9 handling. This combines with perf(terminal): a clean WSL bridge exit skips the boot-id read #35138's code-0 skip of the boot-id read.
The guest-process fast path is settled.readWslGuestPaneProcesses({ running: true }), used by Sleep and the quit read, used to trust the cached running list whenever it was taken. It now trusts it only if that list was probed after any stop of the distro settled (wasWslDistroLastSeenRunningSettled, built on peekConfirmedRunningWslDistrosObservation).
No stall after a skipped drain. When a mid-stop list skips a distro with deferred work, the gate schedules one unref'd wsl --list --running just past the settle, so the work doesn't wait for an unrelated probe.
Why
On the awin verify (build f59bd43), wsl --terminate <distro> with two bridge panes open was followed about 3.7 s later by two Orca-spawned wsl.exe processes and a boot of the distro. The hook relay's node then came up; orcad stayed down. Two holes allow this, plus two ungated background paths seen in the awin logs:
On build f59bd43 (the boot seen on awin): any successful running list drained every task deferred for the listed distros. A list probed mid-stop, for example by a bridge exit's own verdict check, still named the stopping distro and released deferred restarts. Some of these spawn wsl.exe straight away: the hook-relay restart launches the relay (the node that came up), and CLI repair runs the installer after an unsettled list. The new test reproduces this on f59bd43 itself.
awin logs (f59bd43): the bridge-exit verdicts were lost, so that path didn't fire on that build. About 2 s after each terminate, 29 ungated \\wsl.localhost\<distro>\…\{package.json, conductor.json, …} opens came from setup-script import inspection. A UNC open boots a stopped distro, so these are gated now (item 6). The hook relay's link died about 1.9 s after the terminate and scheduled a restart. A drained deferral used to restart it without checking again (item 5).
I chose to fix the shared gate rather than each caller, because every background caller already goes through isWslDistroRunningForBackgroundWork and the deferred-work drain. Settling once there covers the file watcher, the relay, cleanup and CLI repair together.
Flag-off: this changes behavior with ORCA_INTERNAL_WSL_HOSTS unset too. WSL bridge panes, the file watcher, the hook relay and the deferred-work gate all exist without the WSL host flag. User-risk: low. A pane's own clean exit (code 0) still closes instantly. A non-zero bridge exit now takes about 0.5 s longer to resolve, because its verdict waits for the existing 500 ms settle before reading the list. Background work for that distro waits the same 500 ms and needs a fresh list. Deferred restarts that a mid-stop list used to release now wait for a settled list showing the distro running. The user's own acts are unchanged: opening a terminal still marks the distro woken and drains deferred work immediately. SSH and remote wire are unaffected; this is local WSL code only.
Linked Issue
Internal WSL-via-orcad tracker, E9 (never boot a stopped distro from a background path), from the awin verify on f59bd43.
Visual Proof
N/A: no UI.
Testing
src/main/daemon/daemon-wsl-bridge-exit-no-boot.test.ts. It uses the real gate and running-list cache and the production boot-identity dependencies; only ../wsl and the boot-id exec are faked. Fake timers. WSL keeps listing the distro for 300 ms after the terminate.
Two panes exit together: both verdicts are lost, and the boot id is read only once, at spawn. Fails on main (two reads, verdicts running).
A list probed mid-stop drains no deferred work. Fails on main and on f59bd43; I ran the case against an f59bd43 checkout.
A settled list showing the distro running still drains deferred work. Liveness check.
wsl-hook-relay-recovery.test.ts: a drained restart whose settled re-read says stopped doesn't restart; it waits, then restarts once a later settled read says running. Fails on main, which restarts immediately.
Distro-loss suite: a code-0 exit closes with no running-list read, even when the list never answers, and still marks a possible stop. A non-zero exit marks it too.
daemon-wsl-bridge-exit.test.ts (9/9): the coalesced-probe case now waits for its probe, which starts after the settle anchor.
Guest-process fast path: the Sleep/quit fast path skips probing only on a settled list. In the no-boot suite, a list from before the stop, or one probed mid-stop, can't vouch for a live pane.
Settle probe: a skipped drain runs once the settle probe lists the distro.
worktree-hook-inspection-unc-drive.test.ts: a stopped distro gets [] with no readFile or stat call.
The existing bridge-exit, boot-identity, distro-loss and relaunch suites and the gate suite pass. Across src/main/daemon, src/main/wsl, src/main/agent-hooks, src/main/ipc/filesystem and src/main/cli, 6152 tests pass. Six fail locally, and they fail the same way without this change: real watcher, packaged CLI assets, quick-open and history-manager timing on macOS. pnpm tc, typecheck:ci, code-quality, anti-slop and ast-grep pass.
Automated tests added/updated
Status
Ready for two reviewers. Not gated here: hooks:check and hooks:readIssueCommand also read orca.yaml / .orca/issue-command over UNC. They feed launch preflight, where returning error for a stopped distro would skip setup or issue commands on a user launch, so they need a separate decision. On awin, the main log for a terminate should now read [pty] WSL bridge exit: lost, and no wsl.exe should be spawned until the user acts.
Checklist
This PR is small and focused
I explained what changed and why
N/A for visuals with reason
Self-reviewed for correctness, security, and performance
Cross-platform, SSH/remote impact considered (Windows/WSL-only code paths; SSH unaffected)
pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (CI runs the rest)
The regression test fails on main (the boot-id path and the drain path) and on f59bd43 (the drain path), and passes with this fix.
Checks run: pnpm tc, typecheck:ci, code-quality:changed, anti-slop, oxlint, ast-grep, plus the daemon, wsl, agent-hooks, ipc/filesystem and cli suites (6152 passed). Six tests fail, and the same ones fail without this change (macOS environment and timing).
Open question: none, but the awin main log line for the terminate would confirm which path booted the distro.
The WSL background gate now filters running-distro observations using each probe’s start time and a settled-running floor. Bridge boot-identity handling marks distros that may have stopped, and bridge exit checks use settled read anchors for their initial probe and retry. Deferred recovery rechecks distro liveness before restarting. Guest pane checks use settled observations, and hook-import inspection returns no candidates for blocked WSL paths.
Priority: Normal
Merge Risk: Moderate · up to 19096
This change makes WSL background checks wait for a settled running-distro list before booting or releasing work. Two concerns remain. An earlier exit check could still act on a list taken mid-shutdown if a later exit moves the settle floor. A bridge exit with code 0 now closes the pane and clears its history without host-loss classification, which would matter only if a distro stop can report code 0. Resolve or explicitly accept both before merging.
Pre-merge checks: 3 · 2
Failed checks: 2 warnings
Check name
Status
Explanation
Resolution
Description check
The description thoroughly explains the user impact, implementation, rationale, testing, scope, and known limitations. However, the required Linked Issue section contains no actual issue link or issue…
Add the actual issue reference or link under Linked Issue, such as Fixes #1234 or the applicable internal tracker identifier. Complete any remaining required checklist items or explain why they do not apply.
Docstring Coverage
Docstring coverage is 75.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documenta…
Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed checks: 3 passed
Check name
Status
Explanation
Title check
The title clearly and specifically summarizes the primary WSL shutdown regression fix.
Linked Issues check
Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check
Check skipped because no linked issues were found for this pull request.
Full details: Description check
Explanation
The description thoroughly explains the user impact, implementation, rationale, testing, scope, and known limitations. However, the required Linked Issue section contains no actual issue link or issue reference.
Resolution
Add the actual issue reference or link under Linked Issue, such as Fixes #1234 or the applicable internal tracker identifier. Complete any remaining required checklist items or explain why they do not apply.
Full details: Docstring Coverage
Explanation
Docstring coverage is 75.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 12 functions across 15 files.
Resolution
Write docstrings for the functions missing them to satisfy the coverage threshold.
Finishing Touches: 💡 1
📝 Generate docstrings: 💡
Commit to this branch
Create a new PR
Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
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.
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 0 remain after this review.
The reason will be displayed to describe this comment to others. Learn more.
Major · Functional Correctness · Quick win
Recheck the stop floor after waiting.
If another pane exits while settledWslRunningReadAnchor waits, noteWslDistroMayHaveStopped advances the floor. This function still returns the earlier anchor. The first exit check can then probe during the second stop window, receive a list that still names the distro, and return 'running' for a stopping distro. Recalculate the floor after the wait, and continue waiting if it advanced. As per coding guidelines, “Never collapse ‘can't tell’ into ‘dead.’” The same stop-settle boundary must not treat a premature running list as a settled verdict.
STATUS: added the two gates from the awin evidence. Head d15574f, rebased on main.
The hook-relay restart re-reads a settled running list before it spawns the relay. If the distro has stopped, it defers again.
Setup-script import-candidate inspection, both the IPC handler and the runtime RPC, returns [] without any UNC open while the distro is stopped or unverifiable.
Each has a test. Both new tests fail on main.
Checks run: pnpm tc, typecheck:ci, code-quality:changed, anti-slop, oxlint, ast-grep, plus the hooks, agent-hooks, repo RPC and no-boot suites (1623 passed).
Open question: hooks:check and hooks:readIssueCommand still read over UNC. They feed launch preflight, so they're left for a separate decision.
…s still stopping
WSL keeps listing a distro for a moment while it stops. A bridge pane's exit now marks its distro
as possibly stopped, its host-loss verdict and boot-id reread only count a running list started
after that settles, and deferred background work (file watcher and hook relay restarts, history
cleanup, CLI repair) is drained only by a settled list. Before, a bridge exit's own list could
read 'running' mid-stop and lead to a boot-id exec, and any confirmed list probed mid-stop
drained deferred restarts that spawn wsl.exe, booting the distro the user had just terminated.
…a settled running read
- A deferred hook-relay restart re-reads a settled running list right before spawning the relay
(wsl.exe), and waits again if the distro stopped; a drained deferral is only a hint.
- Setup-script import-candidate inspection (sidebar prompt, runtime RPC) opens nothing under
\\\\wsl.localhost while the distro is stopped or unverifiable: those UNC opens booted it about 2 s
after each terminate on the awin verify.
…cess fast path
- A bridge pane's code-0 exit is the user's own (a terminate gives -1): it now closes at once,
with no stop mark, settle wait or running-list read. Non-zero exits keep the E9 discipline.
- readWslGuestPaneProcesses' fast path for a live pane (Sleep, quit) trusts the cached running
list only when it was probed after any stop of the distro settled.
- When a mid-stop list skipped deferred work, one unref'd probe just past the settle drains it,
instead of waiting on an unrelated probe.
…it check test's probe timing
A code-0 bridge exit still closes at once, but now also marks its distro as possibly stopped, so
an in-guest poweroff or wsl --shutdown that exits 0 cannot leave background reads trusting an old
running list. The coalesced-probe test waits for the probe, which now starts after the settle
anchor, before answering it.
STATUS: reviewer A's changes are pushed. Head 19096e5, rebased on main.
The daemon-wsl-bridge-exit test now waits for its probe before answering it. 9 of 9 pass, with no hang and no EISDIR.
A code-0 exit still closes instantly, and it now also calls noteWslDistroMayHaveStopped.
Every touched file was run on its own and its pass count equals its total. The daemon, wsl, agent-hooks and hooks suites pass in full (516 files, 5353 tests).
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 0 remain after this review.
Do not treat every WSL bridge code 0 as a clean exit.
noteExit only delays later background reads. Code 0 still bypasses finishWslBridgeExit and calls finishExit, which clears the pane history. The bridge tests identify poweroff and wsl --shutdown as possible distro stops, and this branch does not inspect the optional cause. Route code 0 through settled classification.
Suggested fix
--- "a/src/main/daemon/daemon-pty-adapter.ts"+++ "b/src/main/daemon/daemon-pty-adapter.ts"@@ -229,7 +229,7 @@
}
// A terminate never exits 0 (awin: -1 per bridge pane; `exit`/`exit 0` gave 0): a clean exit is
// the user's own, so it closes at once, without the settle wait or classification.
- if (wslDistro && event.payload.code !== 0 && isProvenProcessExit(event.payload.code)) {+ if (wslDistro && isProvenProcessExit(event.payload.code)) {
const exitAt = performance.now()
void this.finishWslBridgeExit(event.sessionId, event.payload, wslDistro, exitAt)
return
Committable suggestion
IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
ELI5
When you stop a WSL distro (
wsl --terminate) while Orca has terminals open in it, Orca could start the distro again a few seconds later on its own. Orca must never boot a distro in the background (rule E9). The cause is that WSL keeps listing a distro as "running" for a moment while it shuts down, and Orca trusted those mid-shutdown lists. Now it doesn't.What Changed
A bridge pane's exit marks its distro as possibly stopped.
WslBridgeBootIdentity.noteExit, which runs synchronously on every bridge exit, now callsnoteWslDistroMayHaveStopped. Every background "is it running?" check for that distro (boot id, UNC, file watcher, guest pane processes) then needs a list started after the stop has settled (WSL_STOP_SETTLE_MS). Its own stop-observation listener now only drops the cached boot id, as before.The host-loss verdict reads a settled list.
checkWslBridgeDistroanchors its reads atsettledWslRunningReadAnchor(distro, exitAt), newly exported from the gate (it is the gate's existing settle logic). A distro that is still stopping now reads aslost, notrunning.The boot-id reread is settled.
rebootedSinceSpawnkeeps usingisWslDistroRunningForBackgroundWork. Because of (1), that check now needs a list started after the exit settled, sowsl.exe -d <distro> --exec cat …/boot_idcan't run off a mid-stop list.Deferred work drains only on settled lists.
onConfirmedRunningWslDistrosnow also passes the probe's start time. The gate drains deferred work (deferUntilWslDistroRunning: file-watcher retries, hook-relay restarts, history-tombstone cleanup, CLI registration repair) only for distros whose stop had settled before that probe started.The hook-relay restart re-reads a settled list. When deferred work drains
WslRelayRecovery's restart, it now reads a settled running list (isDistroRunning(distro, now)) right before spawning the relay throughwsl.exe. If the distro has stopped, it waits again.Setup-script import inspection is gated. It runs in the background (sidebar prompt card, runtime RPC
repo.setupScriptImports). Bothhooks:inspectSetupScriptImportsandinspectRepoSetupScriptImportsnow return[]without opening anything whenblockedWslDistroForPath(repo.path)reports the distro stopped or unverifiable.A clean exit closes at once. A bridge pane's code-0 exit is the user's own; awin shows a terminate exits -1. It now finishes immediately, with no settle wait or running-list read. It still marks the distro as possibly stopped. The user doesn't see that mark, and it covers an in-guest poweroff or
wsl --shutdownthat exits 0. Non-zero exits keep the full E9 handling. This combines with perf(terminal): a clean WSL bridge exit skips the boot-id read #35138's code-0 skip of the boot-id read.The guest-process fast path is settled.
readWslGuestPaneProcesses({ running: true }), used by Sleep and the quit read, used to trust the cached running list whenever it was taken. It now trusts it only if that list was probed after any stop of the distro settled (wasWslDistroLastSeenRunningSettled, built onpeekConfirmedRunningWslDistrosObservation).No stall after a skipped drain. When a mid-stop list skips a distro with deferred work, the gate schedules one unref'd
wsl --list --runningjust past the settle, so the work doesn't wait for an unrelated probe.Why
On the awin verify (build f59bd43),
wsl --terminate <distro>with two bridge panes open was followed about 3.7 s later by two Orca-spawnedwsl.exeprocesses and a boot of the distro. The hook relay's node then came up; orcad stayed down. Two holes allow this, plus two ungated background paths seen in the awin logs:wsl.exestraight away: the hook-relay restart launches the relay (the node that came up), and CLI repair runs the installer after an unsettled list. The new test reproduces this on f59bd43 itself.lost, so that path didn't fire on that build. About 2 s after each terminate, 29 ungated\\wsl.localhost\<distro>\…\{package.json, conductor.json, …}opens came from setup-script import inspection. A UNC open boots a stopped distro, so these are gated now (item 6). The hook relay's link died about 1.9 s after the terminate and scheduled a restart. A drained deferral used to restart it without checking again (item 5).runningmid-stop, and then the boot-id check ranwsl.exe --exec cat boot_id, which boots the distro once it has stopped.I chose to fix the shared gate rather than each caller, because every background caller already goes through
isWslDistroRunningForBackgroundWorkand the deferred-work drain. Settling once there covers the file watcher, the relay, cleanup and CLI repair together.Flag-off: this changes behavior with
ORCA_INTERNAL_WSL_HOSTSunset too. WSL bridge panes, the file watcher, the hook relay and the deferred-work gate all exist without the WSL host flag.User-risk: low. A pane's own clean
exit(code 0) still closes instantly. A non-zero bridge exit now takes about 0.5 s longer to resolve, because its verdict waits for the existing 500 ms settle before reading the list. Background work for that distro waits the same 500 ms and needs a fresh list. Deferred restarts that a mid-stop list used to release now wait for a settled list showing the distro running. The user's own acts are unchanged: opening a terminal still marks the distro woken and drains deferred work immediately. SSH and remote wire are unaffected; this is local WSL code only.Linked Issue
Internal WSL-via-orcad tracker, E9 (never boot a stopped distro from a background path), from the awin verify on f59bd43.
Visual Proof
N/A: no UI.
Testing
src/main/daemon/daemon-wsl-bridge-exit-no-boot.test.ts. It uses the real gate and running-list cache and the production boot-identity dependencies; only../wsland the boot-id exec are faked. Fake timers. WSL keeps listing the distro for 300 ms after the terminate.lost, and the boot id is read only once, at spawn. Fails on main (two reads, verdictsrunning).wsl-hook-relay-recovery.test.ts: a drained restart whose settled re-read says stopped doesn't restart; it waits, then restarts once a later settled read says running. Fails on main, which restarts immediately.daemon-wsl-bridge-exit.test.ts(9/9): the coalesced-probe case now waits for its probe, which starts after the settle anchor.worktree-hook-inspection-unc-drive.test.ts: a stopped distro gets[]with noreadFileorstatcall.The existing bridge-exit, boot-identity, distro-loss and relaunch suites and the gate suite pass. Across
src/main/daemon,src/main/wsl,src/main/agent-hooks,src/main/ipc/filesystemandsrc/main/cli, 6152 tests pass. Six fail locally, and they fail the same way without this change: real watcher, packaged CLI assets, quick-open and history-manager timing on macOS.pnpm tc,typecheck:ci, code-quality, anti-slop and ast-grep pass.Status
Ready for two reviewers. Not gated here:
hooks:checkandhooks:readIssueCommandalso readorca.yaml/.orca/issue-commandover UNC. They feed launch preflight, where returningerrorfor a stopped distro would skip setup or issue commands on a user launch, so they need a separate decision. On awin, the main log for a terminate should now read[pty] WSL bridge exit: lost, and nowsl.exeshould be spawned until the user acts.Checklist
N/Afor visuals with reasonpnpm lint,pnpm typecheck,pnpm test, andpnpm buildpass (CI runs the rest)