I am building a native node module with Nan/V8 that uses AsyncWorker to create an async function.
Is there a way to protect the node host application from dying if my thread crashes? The data each worker executes is purely local, so the worst case would be a memory leak, but I need to protect the host application from crashing.
CodePudding user response:
A crash is generally a per-process event. There is no way to let a single thread crash and have other threads in the same process live on.
FWIW, this is unrelated to Node/Nan/V8. It's one of the key reasons why all modern browsers use multi-process architectures.
Of course, switching from a multi-threaded application to a multi-process model comes at a cost: exchanging data with other processes (IPC, "inter-process communication") is generally more cumbersome than coordinating a couple of threads within a single process.
That said, people sometimes use the word "crash" with somewhat different meaning, so it's possible that there are other answers here. E.g. from a JavaScript perspective, an unhandled exception could be described as a "crash" -- and that would be easy to handle and recover from, not even a thread has to die for that.
Also, scenarios where applications actually have to deal with crashing processes are pretty rare (browsers are an example of that); most of the time developers' time is better spent ensuring that no crashes happen, instead of trying to handle them when they do happen. Node itself should be stable enough that it doesn't just crash; and for your own module, designing (and debugging, if needed :-) ) it so it won't crash may be more feasible/effective than devising (and testing) a scheme that lets it gracefully handle crashes.