{"record":{"id":"1c5beba084592d9e","repo":"vitest-dev/vitest","slug":"the-worker-thread-was-torn-down-or-never-initializ","errorCode":null,"errorMessage":"The worker thread was torn down or never initialized. This is a bug in Vitest.","messagePattern":"The worker thread was torn down or never initialized\\. This is a bug in Vitest\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/vitest/src/node/pools/workers/threadsWorker.ts","lineNumber":86,"sourceCode":"    await thread.terminate()\n    await flushed\n\n    thread.stdout.unpipe(this.stdout)\n    this.stdout.setMaxListeners(this.stdout.getMaxListeners() - 1)\n\n    thread.stderr.unpipe(this.stderr)\n    this.stderr.setMaxListeners(this.stderr.getMaxListeners() - 1)\n\n    this._thread = undefined\n  }\n\n  deserialize(data: unknown): unknown {\n    return data\n  }\n\n  private get thread() {\n    if (!this._thread) {\n      throw new Error(`The worker thread was torn down or never initialized. This is a bug in Vitest.`)\n    }\n    return this._thread\n  }\n}\n","sourceCodeStart":68,"sourceCodeEnd":91,"githubUrl":"https://github.com/vitest-dev/vitest/blob/1fa9837ec26533512fdcad8baebf249771bd340a/packages/vitest/src/node/pools/workers/threadsWorker.ts#L68-L91","documentation":"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.","triggerScenarios":"A threadsWorker method dereferences 'thread' after close() set this._thread = undefined, or before the Worker was constructed.","commonSituations":"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.","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."],"exampleFix":null,"handlingStrategy":"type-guard","validationCode":"function isThreadAlive(worker: { _thread?: unknown }): boolean {\n  return Boolean(worker._thread)\n}","typeGuard":"const isThreadAlive = (worker: { _thread?: unknown }): worker is { _thread: object } => Boolean(worker._thread)","tryCatchPattern":"try {\n  worker.postMessage(msg)\n} catch (err) {\n  if (err instanceof Error && err.message.includes('torn down')) {\n  return\n  }\n  throw err\n}","preventionTips":["Avoid sending to a pool after teardown.","Let Vitest manage worker lifecycle; do not send manually.","Upgrade Vitest to benefit from lifecycle race fixes."],"tags":["internal-bug","worker-pool","threads","lifecycle","race"],"backgroundTag":null,"analyzedSha":"1fa9837ec26533512fdcad8baebf249771bd340a","analyzedAt":"2026-08-11T16:11:39.638Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}