Christopher Stevens
Christopher Stevens

Reputation: 1278

How do I prevent a segmentation fault from stopping my node.js script?

Recently I've run into a couple of separate issues (this one and this one specifically) where a segmentation fault occurs and it kills the running Node.js script. While the ultimate goal is to resolve these issues to prevent segment faults from occurring, the issues I linked to are still open. I'm looking for a solution to allow a script handle the error with potential to recover.

Example outputs (which don't give error output beyond this):

Segmentation fault (core dumped)

Segmentation fault: 11 (it's been a while since testing this one, could vary slightly)

Try/catch techniques don't seem to be effective so far. In the case of the linked modules, it's a bit tougher to test/share as extra hardware is involved (I can still post some code if it will help).

In general, is there a good technique to prevent a segmentation fault from stopping my node.js script? So far I've only run into this issue intermittently with modules that access external hardware.

Upvotes: 1

Views: 1912

Answers (1)

jfriend00
jfriend00

Reputation: 707686

In general, is there a good technique to prevent a segmentation fault from stopping my node.js script?

You can't. A seg fault is somewhere in native code and you can't catch it via Javascript. Even if you could, you have no idea what state your server is in after a seg fault so you can't really continue unless the error is caught right in the context where it occurred and that code can cleanly put things back in a good state.

The problem needs to be fixed in the native code where it occurs.

Upvotes: 2

Related Questions