How can I get a list of callbacks in the Node work queue? (or, Why won't Node exit?)

node.js

node.js Problem Overview


It says on the Node.js about page:

> Node exits the event loop when there are no more callbacks to perform.

Is there a way to find out which callbacks are keeping Node from exiting?

node.js Solutions


Solution 1 - node.js

You can use process._getActiveHandles() and process._getActiveRequests()

See this discussion in node.js mailing list.

update: there is a good package for this - https://github.com/mafintosh/why-is-node-running

Solution 2 - node.js

There is a npm module wtfnode to show what keep the nodejs app running when you sends SIGINT (ctrl-c) to it.

It internal uses process._getActiveHandles() as mentioned in @andrey-sidrov's answer. The benefit of using wtfnode is that it provides easy-to-read output.

Solution 3 - node.js

If you are interested to find out which open connections are still open:

While the process hangs, you can run in mac and linux: netstat -a to search for open ports. It's a good clue that helps me from time to time when it comes to why Jest is hanged.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDmitry MinkovskyView Question on Stackoverflow
Solution 1 - node.jsAndrey SidorovView Answer on Stackoverflow
Solution 2 - node.jsaleungView Answer on Stackoverflow
Solution 3 - node.jsStav AlfiView Answer on Stackoverflow