EnvironmentTeardownError: Closing rpc while "onUserConsoleLog" was pending — intermittent on v4/v5, clean on v3, maxWorkers:1 does not help
Describe the bug
On Vitest 4 and 5, a full-suite run intermittently exits 1 with every test passing:
⎯⎯⎯⎯⎯⎯ Unhandled Errors ⎯⎯⎯⎯⎯⎯
Vitest caught 1 unhandled error during the test run.
⎯⎯⎯⎯ Unhandled Rejection ⎯⎯⎯⎯⎯
EnvironmentTeardownError: [vitest-worker]: Closing rpc while "onUserConsoleLog" was pending
This error originated in "test/platformFee.test.ts" test file. It doesn't mean the error
was thrown inside the file itself, but while it was running.
Test Files 52 passed (52)
Tests 769 passed (769)
Errors 1 error
This looks like the onUserConsoleLog variant of a race that has been fixed twice for fetch:
- #8649 —
'Closing rpc while "fetch" was pending' on v4 beta, closed via #8705 - #9458 — same shape as this one, "tests pass, run ends with errors", closed via #9676
Both fixes appear to have been scoped to the pending call they were reported against. This reproduces on 4.1.11 and 5.0.0, i.e. after both, with onUserConsoleLog as the pending call instead. #9736 is adjacent — reporter errors in test runtime event callbacks.
Two properties make it awkward:
- Every test passes. Only the teardown fails, so it reads as flake and invites a CI retry rather than a report.
- The attributed file is arbitrary. It names whichever worker happened to be closing, so it moves between runs. It is never the file that caused it.
Reproduction
I could not reduce this to a small repro — it needs enough parallel workers for the race to open, which is itself a useful data point. The shape:
- 47 test files, 32 of them using
// @vitest-environment edge-runtime(@edge-runtime/[email protected]) - each file spawns its own worker (Vitest reports ~52 workers, ~238ms startup each)
- suite completes in ~2.2s, so teardown is a large fraction of the run
- the environment under test is
[email protected], which setsedge-runtimebecause the runtime it mocks is V8 without Node APIs
Version measurements
Ten full-suite runs per version, counting runs that emitted the error:
| Vitest | Failure rate |
|---|---|
| 3.2.4 | 0/10 |
| 4.1.11 | 3/10 |
| 5.0.0 | 3/10 |
So it is introduced in 4.x and unchanged in 5.x. 3.2.4 is clean over 10 runs and, separately, over 12 runs after we pinned back to it.
Workarounds tried, none effective
| Attempt | Result |
|---|---|
maxWorkers: 1 (recommended on #8649) |
still 2/10, and 5× slower — 11.0s vs 2.2s |
isolate: false |
still 2/6, and broke tests in 2 of 6 runs |
silent: 'passed-only' |
still 3/10 |
vi.spyOn(console, 'error'/'log') in setupFiles |
still 3/10 — the edge-runtime VM has its own console, and the late log appears to happen after mocks are restored |
| Draining pending scheduled work before teardown | no effect |
maxWorkers: 1 being ineffective is the part that most distinguishes this from #8649, where it was reported to resolve the fetch variant. That suggests the console path closes its rpc on a different code path from the fetch one.
System Info
Node v24.19.0
pnpm 10.28.2
OS Darwin arm64
Vitest 4.1.11 and 5.0.0 both affected; 3.2.4 clean
Env edge-runtime (@edge-runtime/vm 5.0.0)
Used Package Manager
pnpm
Validations
- [x] Read the Contributing Guidelines
- [x] Read the docs
- [x] Checked existing issues — this is the
onUserConsoleLogvariant of #8649 and adjacent to #9736
Try running your tests with --detectAsyncLeaks. This looks like your code running unexpectedly when Vitest tears itself down. Probably because of the timeout.
This looks like the
onUserConsoleLogvariant of a race that has been fixed twice forfetch:
It wasn't "fixed". Vitest still fails with EnvironmentTeardownError when fetch is called unexpectedly. This is a user error.
Try running your tests with
--detectAsyncLeaks. This looks like your code running unexpectedly when Vitest tears itself down. Probably because of the timeout.
I think we can improve the error message with this hint and also show what the log was if it's onUserConsoleLog.
Same variant on a second convex-test + edge-runtime repo: vitest 5.0.0, vite 8.3.0, convex-test 0.0.58, Node 22.23.2, macOS arm64 — 4 of 20 npm test runs (330 files / 8937 tests, all passing) end with Closing rpc while "onUserConsoleLog" was pending, attributed to whichever worker was closing. --detectAsyncLeaks reports ~90–100 leaks and convex-test scheduled functions firing after teardown; we are draining those next and will report whether the race survives a clean leak count.