Inspector session was closed
Production Risk
Low — inspector sessions are for development/profiling only.
Thrown when a method is called on an inspector.Session that has already been closed or disconnected. Once a session is disconnected, its methods (such as post()) can no longer be used.
- 1Calling session.post() after session.disconnect() has been called
- 2Async callback that uses the session after it has been closed
Triggered when any Session method is called after the session has been closed.
const inspector = require('inspector');
const session = new inspector.Session();
session.connect();
session.disconnect();
session.post('Runtime.enable'); // session is closed — throwsexpected output
Error [ERR_INSPECTOR_CLOSED]: Session was closed
Fix
Stop using the session after disconnecting
WHEN In async code that may outlive the session
let connected = true;
session.on('inspectorNotification', (msg) => { /* handle */ });
session.connect();
function cleanUp() {
connected = false;
session.disconnect();
}
function sendCommand(method, params) {
if (!connected) return;
session.post(method, params);
}Why this works
A connected flag prevents post() calls after disconnect().
const inspector = require('inspector');
const session = new inspector.Session();
session.connect();
session.disconnect();
session.post('Runtime.enable'); // session is closed — throws // this triggers ERR_INSPECTOR_CLOSEDtry {
// operation that may throw ERR_INSPECTOR_CLOSED
riskyOperation()
} catch (err) {
if (err.code === 'ERR_INSPECTOR_CLOSED') {
console.error('ERR_INSPECTOR_CLOSED:', err.message)
} else {
throw err
}
}// Validate inputs before calling the operation
function safe_err_inspector_closed(...args) {
// validate args here
return performOperation(...args)
}✕ Send inspector commands after disconnecting the session
A closed session has no active channel; commands cannot be delivered.
Node.js Error Codes Documentation
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev