67

I have written a script to update my db table after reading data from db tables and solr. I am using asyn.waterfall module. The problem is that the script is not getting exited after successful completion of all operations. I have used db connection pool also thinking that may be creating the script to wait infinitly. I want to put this script in crontab and if it will not exit properly it would be creating a hell lot of instances unnecessarily.

4
  • What db module are you using? Commented Feb 17, 2014 at 15:27
  • I am using mysql module. Commented Feb 18, 2014 at 6:08
  • 2
    This is hard to debug without some code. Are you calling connection.end()? Commented Feb 18, 2014 at 16:11
  • connection.end() is deprecated so it should no longer be used. I am using connection pool and connection.release(). Will post the code also let me finish.. Commented Feb 19, 2014 at 6:02

5 Answers 5

92

I just went through this issue.

The problem with just using process.exit() is that the program I am working on was creating handles, but never destroying them.

It was processing a directory and putting data into orientdb.

so some of the things that I have come to learn is that database connections need to be closed before getting rid of the reference. And that process.exit() does not solve all cases.

When my project processed 2,000 files. It would get down to about 500 left, and the extra handles would have filled up the available working memory. Which means it would not be able to continue. Therefore never reaching the process.exit at the end.

On the other hand, if you close the items that are requesting the app to stay open, you can solve the problem at its source.

The two "Undocumented Functions" that I was able to use, were

process._getActiveHandles();
process._getActiveRequests();

I am not sure what other functions will help with debugging these types of issues, but these ones were amazing.

They return an array, and you can determine a lot about what is going on in your process by using these methods.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, this was exactly what I needed to solve this problem. Solutions like calling process.exit(); are hacks around the fact that you have some kind of active event listener, timeout, etc... These functions allow you to actually track them down and write appropriate cleanup code!
I would suggest to use an NPM module like wtfnode, why-is-node-running or active-handles to determine which piece of code is causing the issue.
Doesn't work in TS 4.4.2. Or am I doing something wrong?
@KonradGałęzowski they are undocumented, you'll get TypeScript errors but those errors are just because TypeScript doesn't know that they are there. They work just fine. If you want to squash the TypeScript errors you'll have to augment the Node.js modules with these functions but it's not necessary if you're just going to use them to debug, just don't type them incorrectly.
44

You have to tell it when you're done, by calling

process.exit();

More specifically, you'll want to call this in the callback from async.waterfall() (the second argument to that function). At that point, all your asynchronous code has executed, and your script should be ready to exit.

EDIT: As pointed out by @Aaron below, this likely has to do with something like a database connection being active, and not allowing the node process to end.

6 Comments

It is not because of asynchronous routines, as evidenced by the fact that the program setTimeout(function(){}, 1000) terminates after 1 second. Node is absolutely smart enough to tell when you're done if you do asynchronous things. The problem is more likely because the db connection (a possible source of future events) is left open.
process.exit() seems to be the most obvious thing to do. But why is it required to call it explicitly? Script should exit on its own
May be I am doing the things in a wrong way,I think I have figured out. Will update on the same let me fix it now.....
@AaronDufour is right, this happens like if you create redis node client and does not disconnect at the end.
I'm upvoting for the "edit" about the database connection being active, not the process.exit() part. I didn't realize I had to manually close my connection to mongoDB in order for my node process to end. Now it shuts down on its own as expected.
|
43

You can use the node module why-is-node-running:

  1. Run npm install -D why-is-node-running

  2. Add import whyIsNodeRunning from 'why-is-node-running'; in your code. Should be the first import in the file.

  3. When you expect your program to exit, run the module:

afterAll(async () => {
  await app.close();
  whyIsNodeRunning();
})

This will print a list of open handles with a stacktrace to find out where they originated:

There are 5 handle(s) keeping the process running
# Timeout
/home/maf/dev/node_modules/why-is-node-running/example.js:6  - setInterval(function () {}, 1000)
/home/maf/dev/node_modules/why-is-node-running/example.js:10 - createServer()
    
# TCPSERVERWRAP
/home/maf/dev/node_modules/why-is-node-running/example.js:7  - server.listen(0)
/home/maf/dev/node_modules/why-is-node-running/example.js:10 - createServer()

6 Comments

Super useful module! I had to import it as import log from 'why-is-node-running' with node --experimental-modules, but it did the job!
Alternatively it's possible to run why-is-node-running stand alone (see docs)
Should there be an additional ) at the end of the first code snippet?
If you're calling the script via node file.js, it's easier to use: why-is-node-running file.js like a debugger tool instead of requiring it in the application itself.
Found this very helpful even today. Just that there are CLI methods of calling as well.
|
10

We can quit the execution by using:

connection.destroy();

Comments

8

If you use Visual Studio code, you can attach to an already running Node script directly from it.

First, run the Debug: Attached to Node Process command:

screenshot of a command palette

When you invoke the command, VS Code will prompt you which Node.js process to attach to:

A quick pick view with Node processes listed

Your terminal should display this message:

Debugger listening on ws://127.0.0.1:9229/<...>
For help, see: https://nodejs.org/en/docs/inspector
Debugger attached.

Debugger listening on ws://127.0.0.1:9229/0f06c9ce-35c1-4b12-95a8-003882ef97dd / For help, see: https://nodejs.org/en/docs/inspector / Debugger attached.

Then, inside your debug console, you can use the code from The Lazy Coder’s answer:

process._getActiveHandles();
process._getActiveRequests();

Debug console

1 Comment

This doesn't only work in Visual Studio. You can utilize this in WebStorm within the Debugger Console.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.