Reputation: 32391
It seems to be generally accepted good practice that node processes should restart when they encounter unhandled exceptions. (e.g. see this)
However, what about when an application consists of parent and child processes, (e.g. using child_process.fork()
). We can't just restart the child process in this case. So my question is:
What is considered best practice for handling an uncaught error in a node child process?
Upvotes: 2
Views: 1618
Reputation: 5518
The best practice for handling uncaught exceptions is the same for a child process as a parent (http://nodejs.org/api/process.html#process_event_uncaughtexception), from the docs:
Note that uncaughtException is a very crude mechanism for exception handling and may be removed in the future.
Don't use it, use domains instead. If you do use it, restart your application after every unhandled exception!
An unhandled exception means your application - and by extension node.js itself - is in an undefined state. Blindly resuming means anything could happen.
Think of resuming as pulling the power cord when you are upgrading your system. Nine out of ten times nothing happens - but the 10th time, your system is bust.
You have been warned.
So it seems restarting the process would be best
Upvotes: 1
Reputation: 146154
I don't think there's a generic answer here - it depends on the purpose of each process, are they both node apps you wrote and maintain, etc.
If you are saying your application itself consists of 2 processes, which I take to mean two node apps you wrote, then I think an unexpected unclean exit of the child process should be treated as if it was an uncaught exception in the main process. Log it, then exit the parent process and let the supervisor restart the parent (which will then restart the child).
My answer would be different, for example, if the child process was graphicsmagick to resize an image. Then the child crashing would not have any particular implications for the stability of the parent process and it would be fine to log the error but allow the parent process to continue running.
If the child process was just one of a cluster of workers using the node core cluster module, then no, a worker crashing would not require the main cluster supervisor to exit and restart.
I think the overwhelming majority of long-running node apps don't fit the model you describe, thus I don't think the community has a best practice for this. The common cases are: single process, single process with external/auxilliary helpers, and master/worker clusters.
Upvotes: 2