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

3

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?

Winser answered 31/7, 2013 at 2:9 Comment(1)
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. ;)Berdichev
P
96

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

Pseudonymous answered 31/7, 2013 at 2:51 Comment(10)
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.Winser
I couldn't remember names as well - used node repl, typed process._ + tab completion :)Pseudonymous
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.Winser
@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.Coranto
@Coranto I thought there were several. How can you see downvotes/upvotes? Anyway, yeah, of course you are correct :DWinser
@dimadima You can see vote counts by clicking the total once you reach 1000 rep. stackoverflow.com/help/privilegesCoranto
@djechlin: Oh, great. Looking forward. Thank you.Winser
is there any official documentation on these functions?Berdichev
no, they are "private" and undocumented. Use at your own riskPseudonymous
Thank you. We had been facing a performance problem in node for a couple of months now and 'why-is-node-running' gave us the answer, telling what has keeping the event loop busyFakieh
P
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.

Plutonium answered 20/11, 2015 at 2:48 Comment(1)
Does process._getActiveHandles().length gives a total of all the processes or only for the process where it is calledHypothecate
S
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.

Salema answered 14/3, 2021 at 19:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.