denoland/deno · error · Error
ERR_INSPECTOR_NOT_WORKER
ERR_INSPECTOR_NOT_WORKER
Error message
Current thread is not a worker
What it means
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).
Source
Thrown at ext/node/polyfills/inspector.js:120
#nextId = 1;
#messageCallbacks = new SafeMap();
#pendingMessages = [];
#drainScheduled = false;
#isDraining = false;
connect() {
if (this.#connection) {
throw new ERR_INSPECTOR_ALREADY_CONNECTED("The inspector session");
}
this.#connection = op_inspector_connect(
false,
(m) => this.#enqueueMessage(m),
);
}
connectToMainThread() {
if (lazyWorkerThreads().isMainThread) {
throw new ERR_INSPECTOR_NOT_WORKER();
}
if (this.#connection) {
throw new ERR_INSPECTOR_ALREADY_CONNECTED("The inspector session");
}
this.#connection = op_inspector_connect(
true,
(m) => this.#enqueueMessage(m),
);
}
#onMessage(message) {
const parsed = JSONParse(message);
try {
if (parsed.id) {
const callback = this.#messageCallbacks.get(parsed.id);
this.#messageCallbacks.delete(parsed.id);
if (callback) {
if (parsed.error) {View on GitHub (pinned to 9ad36f7a2c)
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().
Example fix
// before
const session = new inspector.Session();
session.connectToMainThread(); // throws ERR_INSPECTOR_NOT_WORKER on main thread
// after
import { isMainThread } from 'node:worker_threads';
const session = new inspector.Session();
if (isMainThread) session.connect();
else session.connectToMainThread(); Defensive patterns
Strategy: validation
Validate before calling
import { isMainThread } from 'node:worker_threads';
function connectSession(session) {
if (isMainThread) session.connect();
else session.connectToMainThread();
} Type guard
import { isMainThread } from 'node:worker_threads';
function canConnectToMainThread() {
return !isMainThread;
} Prevention
- Always branch on worker_threads.isMainThread around inspector connection setup.
- Keep worker-specific bootstrap code in the worker entry file only.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Expected data to be a string, Buffer, Uint8Array, or ArrayBu
- ERR_INSPECTOR_ALREADY_CONNECTED
- ERR_INSPECTOR_NOT_CONNECTED
- ERR_INSPECTOR_ALREADY_ACTIVATED
- ERR_INSPECTOR_NOT_ACTIVE
AI-assisted analysis of denoland/deno@9ad36f7a2c (2026-08-20).
Data as JSON: /api/errors/78b5082eb4b6b35e.
Report an issue: GitHub.