I just ran into a really nasty bug related to node's process.on('uncaughtException') handler and thought I should share my experience.
Basically you need to be very careful with handling uncaught exceptions in node. Ideally you should just send out an error email and perform a graceful restart of your service whenever you hit an uncaught exception.
Why? Well, if you think you can use "uncaughtException" to just resume your service as if nothing had happened, you may be in for a nice suprise. Consider the following example:
var fs = require('fs');
process.on('uncaughtException', function(err) {
console.log(err);
});
var file = fs.createReadStream(__filename, {encoding: 'utf8'});
file
.on('data', function(b) {
console.log('data event fired');
throw new Error('oh no');
})
.on('end', function() {
console.log('end event fired');
});
Unless you are very familar with the inner workings of fs.ReadStream, the assumed output sequence would be:
data event fired
{ message: 'oh no', stack: [Getter/Setter] }
end event fired
However, the actual output is:
data event fired
{ message: 'oh no', stack: [Getter/Setter] }
You might already be able to guess what happens. The exception caused in the callback to the 'data' event causes node to trigger the "uncaughtException" event, and drop the whole call stack after that. Unfortunately however, "fs.ReadStream" had plans to execute more code after firing this "data" callback, which doesn't get executed anymore, causing the "end" event to never fire.
This is a fairly harmless example. The bug I actually ran into caused my database queries to get out of order, so I was looking at the wrong end of my code base for quite a while.
So, unless you really know what you are doing, you should perform a graceful restart of your service after receiving an "uncaughtException" exception event. Otherwise you risk the state of your application, or that of 3rd party libraries to become inconsistent, leading to all kinds of crazy bugs.
Let me know if you have any questions, or other suggestions for dealing with uncaught exceptions!
--fg
PS: This bug was especially painful for, since I'm also to blame for "uncaughtException" being in the node core to begin with : ).
