A script with an active inspector session exits unexpectedly using `process.exit()`. The developer notices that cleanup logic after the exit call is not run. Why does this happen?
Node.js interview question for Advanced practice.
Answer
process.exit() forcefully terminates the process, preventing any subsequent code, including inspector.close(), from executing.
Explanation
The process.exit() method forces the Node.js process to terminate immediately. It does not wait for pending asynchronous operations, timers, or I/O operations to complete. As a result, any code placed after the process.exit() call, such as inspector.close(), is unreachable and will never be executed. For a graceful shutdown, the process should be allowed to exit naturally when the event loop is empty.