I have a fairly large Node.js project that's been working great for an extensive period of development. Recently however -- and unfortunately cannot pinpoint exactly when it changed -- my application has started exiting with program terminated only when running from within the node.js debugger.
The big question: How do I find out what is causing this error?
Breakdown of what I know:
node debug main.js)SIGINT, exit, nor uncaughtException are hitconsole.log() I've been able to determine that this happens when I require a module (more information on this below)require logic is surrounded by try/catch but it does not catchdomain's error handler to catch this by putting the require portion in the run method but nothing caught there, eitherAbout require:
My program has a "plugin" type system in which modules are require'd at runtime as needed. The offending require is not deterministic from what I can tell, however: A module that can be require'd a few times in a row with success fails other times.
Again, this is code that has been working for months. I've been making some fairly extensive changes (but not to the area of code using require mentioned above) when I started noticing this. Since I can often run for a few minutes without issue though many different code paths before seeing this, it's hard to say what is triggering this.
What else can I do here?!
For reference, the (not quite up to date as I've not pushed my changes) code is on Github, if that helps.
After a lot more digging and asking around I have some answers, so I'll answer this myself:
The Cause
When Node.js itself encounters a segfault, the program (e.g. .js) will simply stop. You will not get an exception, error handlers executed, etc. as the underlying node has died. If you're running under the debugger, the only message you'll see is program terminated.
Finding Out Why
To find out why node is terminating a couple options work well:
gdb
node-segfault-handler
node-segfault-handler is a drop in module that will give you a basic call stack when something explodes. Simply require and init it at the top of your index.js or equivalent:
var SegfaultHandler = require('segfault-handler');
SegfaultHandler.registerHandler("crash.log");
See the authors page for additional information and usage.
gdb
With gdb you can get more readable stack information and truly debug if needed. Note that a common cause for Node.js segmentation faults is native module(s) you may be using. If this is the case, it's probably a good idea to (re)build them with debug symbols so your stack trace has something useful. Native modules are generally using node-gyp. In this case, find the modules binding.gyp file's cflags member and add -g to it. Example from the sqlite3 package:
"cflags": [ "-include ../src/gcc-preinclude.h", "-g" ],
Next rebuild the module(s) you updated:
npm rebuild --build-from-source sqlite3
Finally, execute node and your program under gdb or attach it. To debug the debugger (like in my case) the steps would be something like this:
gdb --args in front, e.g. gdb --args node debug main.js
run
bt to get the stack information, or debug away!If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With