Restart Node.js servers on domain errors: Sensible FUD #
The Node.js documentation about the “domain” error handling module has a strongly worded warning imploring you to shut down the process when a domain catches an error.
I was recently asked for a more detailed explanation of why this is, and answered with the following gist.
This principle is not unique to JavaScript, of course. Any language with state, side effects, and exceptions that abandon local context without a way to resume at the throw site, will basically guarantee that unexpected exceptions cannot ever be handled in a way that lets the program proceed normally. (This also applies, for example, to Ruby, Python, PHP, C++, and many other languages, though of course they’re going to have their own set of caveats and workarounds.)
This is not so bad if it’s a program that isn’t long-lived. PHP scripts typically run to completion and then get cleaned up, so a missed exception is pretty meh. Same with command line scripts.
But in a server or some other sort of long-lived daemon, it’s completely unacceptable, and will nearly always lead to undesirable system behavior.
Finish up what you’re doing, close the active connections or whatever, and GTFO. Let the system start a new process.
It’s the only responsible option.