Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When a Node.js process can exit() directly without triggering other signal events (uncaughtException, SIGINT, SIGTERM...)

Tags:

node.js

exit

I work on an application with a REDIS data store. To maintain the data integrity I would like to cleanup the store on process shutdown.

I created a function handler and bind it (with process.on()) to signal events: uncaughtException, SIGINT, SIGTERM, SIGQUIT and it works well (CTRL+C, process killing, exception, ...).

But I've read that in certain conditions the process can exit directly without triggering the other signal events.

The problem in this particular case is that process.on('exit') handler can only process synchronous tasks.

I made different test to try to kill the process in different ways. And (except with SIGTERM on Windows) I wasn't able to identify the case where process.on('exit') is triggered directly without SIGINT, SIGTERM or other event firing.

So my question is (on Linux system), under what conditions the process can exit directly without firing on of this event: http://nodejs.org/api/all.html#all_signal_events ?

like image 481
damien Avatar asked Sep 25 '14 09:09

damien


People also ask

What is process exit in node js?

The process. exit() method is used to end the process which is running at the same time with an exit code in NodeJS. Parameter: This function accepts single parameter as mentioned above and described below: Code: It can be either 0 or 1.

What is sigint NodeJS?

SIGINT: Quit from keyboard (Ctrl + C). SIGQUIT: Quit from keyboard (Ctrl + ). It also produce a core dump file. SIGTERM: Quit from operating system (using kill command for example).

Which of the following is an exit code of node js?

Node normally exits with code 0 when no more async operations are pending.


1 Answers

As of now, reading the documentation and doing some research, it seems there is only four way a node.js app exit:

  • process.exit(), which is handled by process.on('exit')
  • Receiving a *nix signal, which can be handled by process.on('signal_name')`
  • Having a exception going back to the event loop, handled by process.on('UncaughtException')
  • The computer being plugged out, destroyed, the node.js binary blowing up, or a SIGKILL/kill -9, and there is no handling to that.

It usually happen someone don t understand the error message from a uncaughtException, and mistakenly believe it is "something else" that killed node.js.

like image 177
DrakaSAN Avatar answered Oct 16 '22 08:10

DrakaSAN