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

  1. Inspect logs for the event that tore the thread down before this access.
  2. Avoid sending to a pool after calling close()/teardown.
  3. Upgrade Vitest to pick up lifecycle/race fixes.
  4. 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

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


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)