Nodejs: why should I close the process when an error occurs?

The default behavior of Nodejs is to turn off when the error goes to the main event loop. The manual strongly recommends that you do not override this behavior (for example, through process.on('uncaughtException ).

Explanation given:

An unhandled exception means your application - and the node.js extension - is in undefined state. Blindly renewable funds can all happen.

Think about renewing when you pull the power cord when upgrading your system. Nine out of ten times nothing happens - but on the 10th time, your system breaks up.

Can someone clarify this? Chrome, which uses the same V8 engine as node, resumes its event loop after an incomprehensible default error, and AFAIK does not cause any problems. Thus, it does not seem that there is any internal reason why the V8 cannot gracefully recover from an uncaught exception. Is there something in node internals that behave differently than Chrome?

+4
source share
1 answer

The answer has nothing to do with the ability to start the engine yourself.

This is due to your own application code. If an unhandled exception occurs, then there is essentially no way to understand the state of your application. If this were so, then this would not be an unhandled exception. And, if you donโ€™t know your condition, then you cannot be sure that more unhandled exceptions will not continue, which is likely to lead to worse and worse problems as time progresses (since unexpected states cascade into more and more unexpected states )

Imagine this is code that runs on the server (since this does not apply to node.js):

 start process open two server sockets process incoming requests 

If you cannot open the second server socket without handling the exception, then the likelihood that your application will not work. Restarting the thread in the next logical step will most likely not work either. Restarting the engine could not reasonably close one socket, and it is unlikely that it will fix the cause of the second failure (most likely, the port is already in use), and if it closed the successfully opened socket, then it is better to restart the application so that it can be reopened (or worse )

This is perhaps the obvious case, but now imagine that you are a graphical application (for example, a game):

 start process load models handle state (until closing) draw screen 

If any model did not load without exception handling, the process cannot be reasonably continued, because it will simply cause more errors when drawing.

There are times when recovering from unhandled exceptions is reasonable. Most client GUIs have a registration method for unhandled exceptions that allows you to restart the event stream (GUI stream), similar to restoring Chrome V8. This is dangerous because recovery is not guaranteed; everything that caused the unhandled exception can still be in memory and ready to throw the exception again the next time it is used. However, it is also possible that a well-designed application can be small enough to completely clear data with such exceptions. The best use of such handlers (handling unhandled exceptions) is to log the exception so that the problem can be fixed.

In other words, imagine that an exception occurred that you did not handle anywhere in your application. What can you do to fix this so that it doesnโ€™t happen on the next pass of the code? To answer safely, this means that you know what caused it, which means that A) it should not be unprocessed and B) it is isolated.

The only guaranteed safe reset should start from the very beginning, which means restarting the application.

+1
source

All Articles