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

  1. Call session.disconnect() before reconnecting — disconnect also resets message queues, callback state, and counters.
  2. Create a fresh inspector.Session per connection lifecycle instead of reusing one instance.
  3. 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

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


AI-assisted analysis of denoland/deno@9ad36f7a2c (2026-08-20). Data as JSON: /api/errors/4124d25feb006c43. Report an issue: GitHub.