Forcibly close an open node.js inspector

A while back, I noticed that something had changed, at least in Chromium-based browsers, not sure about others.

If you start Node-RED with node.js's --inspect flag and attach an inspector. Then if you kill the node-red process (common for testing the development of custom nodes), Node-RED no longer shuts down.

It appears to shut down, if you were running from a terminal command line, you will be back at the command prompt and Node-RED's log will tell you it stopped the flows.

But if you try to restart Node-RED, you cannot because the web port is still in use.


image

I would like to request that somewhere in the Node-RED startup process, something like the following be added:

const inspector = require('node:inspector');

/** Close the Node.js inspector (and any attached debugger client)
 * so that process shutdown isn't held up waiting for the client to detach.
 */
function closeInspector() {
    // inspector.url() returns undefined when the inspector isn't active
    if ( inspector.url() === undefined ) return

    try {
        inspector.close()
    } catch (err) {
        console.error('[inspector-closer] Failed to close inspector:', err)
    }
}

// Forcably close any open inspector on process termination - otherwise, Node-RED does not cleanly close
process.once('SIGINT', closeInspector);
process.once('SIGTERM', closeInspector);

As far as I can tell, this has no downsides. Instead it makes the development workflow a lot smoother.