Skip to content

test: stop worker-teardown console race from failing unit shards - #39813

Merged
OrcaWin merged 1 commit into
mainfrom
OrcaWin/vitest-teardown-console-flake
Nov 8, 2026
Merged

OrcaWin merged 1 commit into
mainfrom
OrcaWin/vitest-teardown-console-flake

Conversation

@OrcaWin

@OrcaWin OrcaWin commented Nov 8, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

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.
  • pnpm check:changed passes.

No production code changed. No tests were added.

Files Added Deleted Net
Test 2 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​17 0 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​17
Prod 1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​3 0 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​3

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

coderabbitai Bot commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

Walkthrough

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: Passed 5

Check name Status Explanation
Title check Passed 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 Passed 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 Passed 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 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: 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.

Share

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

Nitpick comments: 1

config/vitest.config.ts: 1

145-147: Trivial · Maintainability & Code Quality · Quick win

Add focused coverage for the error filter.

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.

Files selected for processing: 3

  • config/vitest.config.ts
  • src/main/codex-accounts/runtime-home-wsl-managed-accounts.test.ts
  • src/main/runtime/orca-runtime-test-mocks/setup.spec.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.

@OrcaWin
OrcaWin merged commit de3da48 into main Nov 8, 2026
218 of 254 checks passed
@OrcaWin
OrcaWin deleted the OrcaWin/vitest-teardown-console-flake branch November 8, 2026 10:55
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