From 70d6bb139c4eccf60bff9b5bc45101acd1509b39 Mon Sep 17 00:00:00 2001 From: Chengzhong Wu Date: Wed, 10 Jun 2026 14:08:29 -0400 Subject: [PATCH] inspector: fix inspector.close() documented behavior The `inspector.close()` forcefully terminates all active connections, allowing the process to exit cleanly, without waiting for the devtool client to disconnect. Signed-off-by: Chengzhong Wu --- doc/api/inspector.md | 4 +-- ...t-inspector-close-terminate-connections.js | 35 +++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 test/parallel/test-inspector-close-terminate-connections.js diff --git a/doc/api/inspector.md b/doc/api/inspector.md index 8c6f45e3d15a88..6a31799ac68e25 100644 --- a/doc/api/inspector.md +++ b/doc/api/inspector.md @@ -415,8 +415,8 @@ changes: description: The API is exposed in the worker threads. --> -Attempts to close all remaining connections, blocking the event loop until all -are closed. Once all connections are closed, deactivates the inspector. +Deactivates the inspector. If there are active connections, they are forcibly +terminated. Blocks until the inspector server has fully stopped. ### `inspector.console` diff --git a/test/parallel/test-inspector-close-terminate-connections.js b/test/parallel/test-inspector-close-terminate-connections.js new file mode 100644 index 00000000000000..ab18ad277dbb9b --- /dev/null +++ b/test/parallel/test-inspector-close-terminate-connections.js @@ -0,0 +1,35 @@ +'use strict'; +const common = require('../common'); +common.skipIfInspectorDisabled(); + +const assert = require('assert'); +const { NodeInstance } = require('../common/inspector-helper.js'); + +// Verify that inspector.close() forcibly terminates active WebSocket +// connections without waiting for the client to disconnect. + +async function test() { + const script = ` + const inspector = require('inspector'); + inspector.close(); + process.exit(42); + `; + + const instance = new NodeInstance(['--inspect-brk=0'], script); + const session = await instance.connectInspectorSession(); + + // Enable Runtime domain and wait for an event to confirm the session is live. + await session.send({ method: 'Runtime.enable' }); + await session.waitForNotification('Runtime.executionContextCreated'); + + // Resume execution so the script calls inspector.close(). + await session.send({ method: 'Runtime.runIfWaitingForDebugger' }); + + // The server should forcibly disconnect us. + await session.waitForServerDisconnect(); + + const { exitCode } = await instance.expectShutdown(); + assert.strictEqual(exitCode, 42); +} + +test().then(common.mustCall());