vitest-dev/vitest · error · Error
The worker thread was torn down or never initialized. This…
Error message
The worker thread was torn down or never initialized. This is a bug in Vitest.
What it means
The threadsWorker 'thread' getter throws when this._thread is undefined - the worker thread was never initialized or was torn down. Identical in spirit to the forks worker guard, but for the worker_threads transport. Any post-teardown access to 'thread' triggers it.
Solutions
- Inspect logs for the event that tore the thread down before this access.
- Avoid sending to a pool after calling close()/teardown.
- Upgrade Vitest to pick up lifecycle/race fixes.
- Reduce concurrency or file size to see if a resource limit caused the thread to die.
Defensive patterns
Strategy: type-guard
Validate before calling
function isThreadAlive(worker: { _thread?: unknown }): boolean {
return Boolean(worker._thread)
} Type guard
const isThreadAlive = (worker: { _thread?: unknown }): worker is { _thread: object } => Boolean(worker._thread) Try / catch
try {
worker.postMessage(msg)
} catch (err) {
if (err instanceof Error && err.message.includes('torn down')) {
return
}
throw err
} Prevention
- Avoid sending to a pool after teardown.
- Let Vitest manage worker lifecycle; do not send manually.
- Upgrade Vitest to benefit from lifecycle race fixes.
When it happens
Trigger: A threadsWorker method dereferences 'thread' after close() set this._thread = undefined, or before the Worker was constructed.
Common situations: Interrupting a run (SIGINT) that tears down threads while messages are still queued, pool reuse of a disposed worker, or a worker_thread crash followed by a late operation.
Related errors
- The child process was torn down or never initialized. This…
- Unexpected message
- Browser is not initialized
- Browser provider is not defined for the project
- Calling the suite function inside test function is not…
AI-assisted analysis of vitest-dev/vitest@1fa9837ec2 (2026-08-11).
Data as JSON: /api/errors/1c5beba084592d9e.
Report an issue: GitHub.
Appendix: source
Thrown at packages/vitest/src/node/pools/workers/threadsWorker.ts:86
await thread.terminate()
await flushed
thread.stdout.unpipe(this.stdout)
this.stdout.setMaxListeners(this.stdout.getMaxListeners() - 1)
thread.stderr.unpipe(this.stderr)
this.stderr.setMaxListeners(this.stderr.getMaxListeners() - 1)
this._thread = undefined
}
deserialize(data: unknown): unknown {
return data
}
private get thread() {
if (!this._thread) {
throw new Error(`The worker thread was torn down or never initialized. This is a bug in Vitest.`)
}
return this._thread
}
}
View on GitHub (pinned to 1fa9837ec2)