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
test: stop worker-teardown console race from failing unit shards - #39813
Unit shards (test / tests node 24 N/12) sometimes fail even though every test passed:
EnvironmentTeardownError: [vitest-worker]: Closing rpc while "onUserConsoleLog" was pending
Recent hits: runs 42440720687, 42442217733, 42369290376, 42368861195 and 42368064977. The error was blamed on runtime-home-wsl-managed-accounts.test.ts twice, project-removal-terminal-stop.test.ts once, and the since-deleted project-host-setup-delete-guard.test.ts three times.
Root cause
When a worker finishes a file, Vitest waits for the RPC calls already in flight. It then rejects any call still pending. Console output is sent over RPC as onUserConsoleLog, and that call is not awaited or caught. So a console.* call from fire-and-forget work that fires during this window becomes an unhandled rejection, and the shard fails. You can reproduce it every time with a test whose afterAll starts setInterval(() => console.log('x'), 0).
To find where the late logs come from, I traced every console.* call made after a file's last hook across the full suite. About 45 test files emit output after they finish (mostly agent-hooks server specs, plus runtime and renderer specs). The two specs blamed in CI that still exist do this:
project-removal-terminal-stop: removeProject → releaseWorktreeCreatePreparationsForRepo → void worktreePreparationGit.run(...) runs real git worktree list on fixture paths (/tmp/repo, /srv/foreign-copy). It warns [git] repository folder missing after the file has ended.
runtime-home-wsl-managed-accounts: with the platform faked as win32, prepareForCodexLaunch starts the legacy WSL auth drain and the WSL session bridge in the background. They spawn wsl.exe (ENOENT) and warn after the file has ended.
Fix
config/vitest.config.ts: a root onUnhandledError ignores only EnvironmentTeardownErrors for "onUserConsoleLog". The only thing lost is a log line from a file whose results are already reported, and CI shards run with silent: 'passed-only', which throws those lines away anyway. Teardown errors for any other RPC method, and all real unhandled errors, still fail the run. I confirmed this locally: an injected Promise.reject still fails with rc=1.
Runtime test setup (setup.spec.ts, shared by about 160 runtime specs): mock the background preparation reclaim so project removal no longer spawns real Git.
runtime-home-wsl-managed-accounts.test.ts: stub the background WSL drain and bridge starters. These specs only check home routing, and the stubs also keep a real Windows runner from touching an actual WSL distro.
Fixing all ~45 late-logging files one by one would not prevent new ones, so the config filter is the safety net. The two spec fixes remove the leaks CI actually reported.
Evidence
Repro test: fails 2 out of 2 runs on main with the exact CI error, passes 2 out of 2 with this change.
After the fix, the late-console trace is clean for both specs.
src/main/runtime and src/main/codex-accounts (1024 files) pass, except 2 failures specific to my machine (no zsh; a locally installed CLI). Those files don't import the changed modules.
Vitest rejects a console-log RPC still in flight when a worker tears
down, failing a shard whose tests all passed. Ignore only that rejection
(the lost line belongs to a finished file and passed-only shards drop
it anyway), and stop the two specs seen in CI from spawning real git and
wsl.exe in fire-and-forget background work.
The Vitest configuration adds a filter for an EnvironmentTeardownError whose message includes "onUserConsoleLog". The WSL managed-accounts tests mock two background-start functions. Test setup also mocks releaseWorktreeCreatePreparationsForRepo.
Merge Risk: Minimal · up to 0c29f
No merge-blocking failure is established. Focused tests would protect the teardown filter against regressions.
Pre-merge checks: 5
Check name
Status
Explanation
Title check
The title "test: stop worker-teardown console race from failing unit shards" states the main change: a fix that stops a Vitest worker-teardown console race from failing unit test shards. The changeset…
Description check
The description follows the repository template closely. It covers the problem and root cause, the fix with per-file detail, evidence from local runs and a repro test, and the scope of the change. It …
Docstring Coverage
No eligible functions found in the changed files. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat…
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: Title check
Explanation
The title "test: stop worker-teardown console race from failing unit shards" states the main change: a fix that stops a Vitest worker-teardown console race from failing unit test shards. The changeset adds a root onUnhandledError filter in config/vitest.config.ts for EnvironmentTeardownError on onUserConsoleLog, plus mocks in setup.spec.ts and the WSL test file that stop background work from logging after a file ends. The title is specific, uses a conventional prefix, and is under 72 characters.
Full details: Description check
Explanation
The description follows the repository template closely. It covers the problem and root cause, the fix with per-file detail, evidence from local runs and a repro test, and the scope of the change. It omits the ELI5, Visual Proof, Risk level, and Linked Issue sections, and it does not tick the Testing checkboxes or give a Risk level. The 'Fixes #' line is also left unfilled. Those gaps are non-critical or can be filled quickly, so the description is mostly complete.
Full details: Docstring Coverage
Explanation
No eligible functions found in the changed files. Skipping docstring coverage check. 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 0 functions across 3 files.
Finishing Touches
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.
Test the configured callback with the onUserConsoleLog teardown error, another RPC teardown error, and a real unhandled error. If the filter is broadened, it can hide errors that should fail the run. If it is removed, the intermittent teardown failure can return.
Review info
Run configuration
Source mode: Auto
Repository: Unavailable
Branch: Unavailable
File: No configuration file from this repository applied
Commit: Unavailable
Layers: Repository UI
Review profile: CHILL
Plan: Advanced
Run ID: 8880bec7-b48d-4d28-8937-e1373bdfc5c9
Commits
Reviewing files that changed from the base of the PR and between 4fabde2 and 0c29f65.
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.
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.
Problem
Unit shards (
test / tests node 24 N/12) sometimes fail even though every test passed:Recent hits: runs 42440720687, 42442217733, 42369290376, 42368861195 and 42368064977. The error was blamed on
runtime-home-wsl-managed-accounts.test.tstwice,project-removal-terminal-stop.test.tsonce, and the since-deletedproject-host-setup-delete-guard.test.tsthree times.Root cause
When a worker finishes a file, Vitest waits for the RPC calls already in flight. It then rejects any call still pending. Console output is sent over RPC as
onUserConsoleLog, and that call is not awaited or caught. So aconsole.*call from fire-and-forget work that fires during this window becomes an unhandled rejection, and the shard fails. You can reproduce it every time with a test whoseafterAllstartssetInterval(() => console.log('x'), 0).To find where the late logs come from, I traced every
console.*call made after a file's last hook across the full suite. About 45 test files emit output after they finish (mostly agent-hooks server specs, plus runtime and renderer specs). The two specs blamed in CI that still exist do this:project-removal-terminal-stop:removeProject→releaseWorktreeCreatePreparationsForRepo→void worktreePreparationGit.run(...)runs realgit worktree liston fixture paths (/tmp/repo,/srv/foreign-copy). It warns[git] repository folder missingafter the file has ended.runtime-home-wsl-managed-accounts: with the platform faked as win32,prepareForCodexLaunchstarts the legacy WSL auth drain and the WSL session bridge in the background. They spawnwsl.exe(ENOENT) and warn after the file has ended.Fix
config/vitest.config.ts: a rootonUnhandledErrorignores onlyEnvironmentTeardownErrors for"onUserConsoleLog". The only thing lost is a log line from a file whose results are already reported, and CI shards run withsilent: 'passed-only', which throws those lines away anyway. Teardown errors for any other RPC method, and all real unhandled errors, still fail the run. I confirmed this locally: an injectedPromise.rejectstill fails with rc=1.setup.spec.ts, shared by about 160 runtime specs): mock the background preparation reclaim so project removal no longer spawns real Git.runtime-home-wsl-managed-accounts.test.ts: stub the background WSL drain and bridge starters. These specs only check home routing, and the stubs also keep a real Windows runner from touching an actual WSL distro.Fixing all ~45 late-logging files one by one would not prevent new ones, so the config filter is the safety net. The two spec fixes remove the leaks CI actually reported.
Evidence
src/main/runtimeandsrc/main/codex-accounts(1024 files) pass, except 2 failures specific to my machine (no zsh; a locally installed CLI). Those files don't import the changed modules.pnpm check:changedpasses.No production code changed. No tests were added.