denoland/deno · error · Error
ERR_INSPECTOR_ALREADY_CONNECTED
ERR_INSPECTOR_ALREADY_CONNECTED
Error message
The inspector session is already connected
What it means
node:inspector's Session holds exactly one connection in a private #connection field. connect() throws ERR_INSPECTOR_ALREADY_CONNECTED when a connection already exists — from a previous connect() or connectToMainThread() that was not followed by disconnect().
Source
Thrown at ext/node/polyfills/inspector.js:110
TypedArrayPrototypeGetByteLength(view),
);
}
throw new TypeError(
"Expected data to be a string, Buffer, Uint8Array, or ArrayBuffer",
);
}
class Session extends EventEmitter {
#connection = null;
#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),
);View on GitHub (pinned to 9ad36f7a2c)
Solutions
- Call session.disconnect() before reconnecting — disconnect also resets message queues, callback state, and counters.
- Create a fresh inspector.Session per connection lifecycle instead of reusing one instance.
- Wrap session lifecycle in a small manager that tracks the connected flag.
Example fix
// before
session.connect();
try { session.connect(); } catch (e) {} // ERR_INSPECTOR_ALREADY_CONNECTED
// after
session.disconnect();
session.connect(); Defensive patterns
Strategy: try-catch
Validate before calling
class InspectorSessionManager {
#connected = false;
connect(session) {
if (this.#connected) session.disconnect();
session.connect();
this.#connected = true;
}
disconnect(session) {
session.disconnect();
this.#connected = false;
}
} Try / catch
try {
session.connect();
} catch (e) {
if (e.code === 'ERR_INSPECTOR_ALREADY_CONNECTED') {
session.disconnect();
session.connect();
} else throw e;
} Prevention
- Treat connect() and disconnect() as a strict pair — one connect per disconnect.
- Do not cache Session singletons across test/setup re-runs.
- Call disconnect() unconditionally in teardown/reset flows.
When it happens
Trigger: session.connect(); session.connect(); — a second connect() with no disconnect() in between; reconnect logic that calls connect() again after a transport drop; two init paths sharing one Session instance that both connect.
Common situations: Reconnect logic that calls connect() again after a transport error without disconnect(); hot-reload or test frameworks re-running setup code against a cached Session singleton; two modules sharing one Session and both connecting at init.
Related errors
- ERR_INSPECTOR_NOT_CONNECTED
- Expected data to be a string, Buffer, Uint8Array, or ArrayBu
- ERR_INSPECTOR_NOT_WORKER
- ERR_INSPECTOR_ALREADY_ACTIVATED
- ERR_INSPECTOR_NOT_ACTIVE
AI-assisted analysis of denoland/deno@9ad36f7a2c (2026-08-20).
Data as JSON: /api/errors/4124d25feb006c43.
Report an issue: GitHub.