86

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?

Dmitry Minkovsky
  • 36,185
  • 26
  • 116
  • 160
  • 2
    this is a great question... same one I started asking when studying the event loop... how do I see the que? not sure why people would vote this down. ;) – tbarbe Sep 08 '13 at 07:56

3 Answers3

94

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

Andrey Sidorov
  • 24,905
  • 4
  • 62
  • 75
  • 2
    Thank you so much, this is what I was looking for. Earlier in #Node.js on Freenode someone was trying to remember these calls and coun't quite get there: `"totally spacing on the name, it's like "process._getOpenHandles()" or something equally hard to remember"`. I was Googling around trying to find the actual call and couldn't. Thanks again. – Dmitry Minkovsky Jul 31 '13 at 02:56
  • 2
    I couldn't remember names as well - used node repl, typed process._ + tab completion :) – Andrey Sidorov Jul 31 '13 at 02:59
  • Hah, I hadn't thought to REPL autocomplete, which of course is the best way to have done it. I had looked in `node_globals.js` and didn't find it. Now I am grepping the node codebase, knowing what to look for, and find that they're defined in node.cc. process._getActiveHandles() is `src/node.cc:2345: NODE_SET_METHOD(process, "_getActiveHandles", GetActiveHandles);` Hah! Excellent. – Dmitry Minkovsky Jul 31 '13 at 03:08
  • @dimadima there's one downvote between question and answer right now. So firstly, it's not "people," it's "a person." And StackOverflow is one of the top 100 most visited websites in the world, you really shouldn't take it so hard that someone, somewhere, wasn't very fond of this question. – djechlin Aug 16 '13 at 12:06
  • @djechlin I thought there were several. How can you see downvotes/upvotes? Anyway, yeah, of course you are correct :D – Dmitry Minkovsky Aug 17 '13 at 23:56
  • @dimadima You can see vote counts by clicking the total once you reach 1000 rep. http://stackoverflow.com/help/privileges – djechlin Aug 18 '13 at 10:14
  • @djechlin: Oh, great. Looking forward. Thank you. – Dmitry Minkovsky Aug 18 '13 at 16:16
  • 2
    is there any official documentation on these functions? – tbarbe Sep 08 '13 at 07:59
  • no, they are "private" and undocumented. Use at your own risk – Andrey Sidorov Sep 08 '13 at 13:02
11

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.

aleung
  • 9,848
  • 3
  • 55
  • 69
0

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.

Stav Alfi
  • 13,139
  • 23
  • 99
  • 171