Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js: inspect what's left in the event loop that's preventing the script from exiting naturally

Tags:

node.js

Node.js script won't exit if there's callbacks left in the main event loop. While one could forcefully terminate the script by calling process.exit() or throwing exceptions, it is recommended to let the script terminate "naturally", by always doing proper cleanup. However, this sometimes can be difficult as bugs in the code may prevent proper cleanup, e.g., I may forget to remove an IntervalObject when no longer needed, etc., which eventually prevents the program from terminating.

Therefore, is there a way to debug a non-terminating script to find out what's remaining registered in the event loop? In other words, is there a way in Node.js to debug what's preventing the program from exiting?

like image 366
KFL Avatar asked Sep 26 '14 10:09

KFL


People also ask

What is an event loop in NodeJS and how does it work?

Event loop is an endless loop, which waits for tasks, executes them and then sleeps until it receives more tasks. The event loop executes tasks from the event queue only when the call stack is empty i.e. there is no ongoing task. The event loop allows us to use callbacks and promises.

How do you break an event loop?

The first way to break the event loop is to execute a task that takes too long. Take a look at this short script: var fs = require('fs'); var doSyncWork = function() { var start = new Date(); var now = new Date(); console. log('doSyncWork: start'); while (now.

Which of the following is an exit code of NodeJS?

Node normally exits with code 0 when no more async operations are pending. process. exit(1) should be used to exit with a failure code.

How the node event loop works internally?

The Event Loop takes the timer with the shortest wait time and compares it with the Event Loop's current time. If the wait time has elapsed, then the timer's callback is queued to be called once the call stack is empty. Node. js has different types of timers: setTimeout() and setInterval() .


1 Answers

You can call process._getActiveRequests() to get a list of active I/O requests and process._getActiveHandles() to get a list of open handles/file descriptors.

like image 132
mscdex Avatar answered Sep 22 '22 06:09

mscdex