{"record":{"id":"78b5082eb4b6b35e","repo":"denoland/deno","slug":"err-inspector-not-worker","errorCode":"ERR_INSPECTOR_NOT_WORKER","errorMessage":"Current thread is not a worker","messagePattern":"Current thread is not a worker","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"ext/node/polyfills/inspector.js","lineNumber":120,"sourceCode":"  #nextId = 1;\n  #messageCallbacks = new SafeMap();\n  #pendingMessages = [];\n  #drainScheduled = false;\n  #isDraining = false;\n\n  connect() {\n    if (this.#connection) {\n      throw new ERR_INSPECTOR_ALREADY_CONNECTED(\"The inspector session\");\n    }\n    this.#connection = op_inspector_connect(\n      false,\n      (m) => this.#enqueueMessage(m),\n    );\n  }\n\n  connectToMainThread() {\n    if (lazyWorkerThreads().isMainThread) {\n      throw new ERR_INSPECTOR_NOT_WORKER();\n    }\n    if (this.#connection) {\n      throw new ERR_INSPECTOR_ALREADY_CONNECTED(\"The inspector session\");\n    }\n    this.#connection = op_inspector_connect(\n      true,\n      (m) => this.#enqueueMessage(m),\n    );\n  }\n\n  #onMessage(message) {\n    const parsed = JSONParse(message);\n    try {\n      if (parsed.id) {\n        const callback = this.#messageCallbacks.get(parsed.id);\n        this.#messageCallbacks.delete(parsed.id);\n        if (callback) {\n          if (parsed.error) {","sourceCodeStart":102,"sourceCodeEnd":138,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/node/polyfills/inspector.js#L102-L138","documentation":"Session.connectToMainThread() attaches a worker thread's inspector session to the main thread's inspector front-end. It is only valid inside a worker: when worker_threads.isMainThread is true it throws ERR_INSPECTOR_NOT_WORKER before any connection attempt (via op_inspector_connect with toMainThread=true).","triggerScenarios":"Calling inspector.Session.connectToMainThread() at the top level of the main module, or in a shared debug-utility module that both the main thread and workers import.","commonSituations":"A debug helper imported by both main and worker code; worker bootstrap code copied into the main entrypoint; frameworks that reuse one setup routine in every thread.","solutions":["Branch on worker context: if (isMainThread) session.connect(); else session.connectToMainThread();","Move worker-specific inspector setup into the worker entry file only.","On the main thread always use plain connect()."],"exampleFix":"// before\nconst session = new inspector.Session();\nsession.connectToMainThread(); // throws ERR_INSPECTOR_NOT_WORKER on main thread\n\n// after\nimport { isMainThread } from 'node:worker_threads';\nconst session = new inspector.Session();\nif (isMainThread) session.connect();\nelse session.connectToMainThread();","handlingStrategy":"validation","validationCode":"import { isMainThread } from 'node:worker_threads';\nfunction connectSession(session) {\n  if (isMainThread) session.connect();\n  else session.connectToMainThread();\n}","typeGuard":"import { isMainThread } from 'node:worker_threads';\nfunction canConnectToMainThread() {\n  return !isMainThread;\n}","tryCatchPattern":null,"preventionTips":["Always branch on worker_threads.isMainThread around inspector connection setup.","Keep worker-specific bootstrap code in the worker entry file only."],"tags":["inspector","worker-threads","node-compat"],"backgroundTag":"worker-thread-context-error","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-08-28T11:17:15.048Z"}