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());