2

In the accepted answer to this question the guy recommends against using domains with throwing exceptions from them because, he says, it will cause memleaks and instability. But this is the way I was going to use domains. Now I'm confused. Is he right?

Community
  • 1
  • 1
Andrey Kon
  • 747
  • 2
  • 6
  • 16

2 Answers2

3

Unlike that answer asserts, there is no guarantee that it will cause memory leaks and instability. But it is hard to guarantee that you can handle the exception without causing memory leaks and instability.

I believe the core of the confusion around exception handling stems from this wording from the Node.js documentation:

By the very nature of how throw works in JavaScript, there is almost never any way to safely "pick up where you left off", without leaking references, or creating some other sort of undefined brittle state.

The safest way to respond to a thrown error is to shut down the process.

http://nodejs.org/api/domain.html

In reality, there's nothing special about JavaScript that makes exceptions particularly dangerous. The fact is, exceptions have potential to be dangerous in any language. The point is to consider carefully what side-effects your application is causing, and whether you're doing anything that could be dangerous if it were to stop half-way through (hint: you probably are).

This design guide from Joyent makes the distinction between "programmer errors" and "operational errors". In answer of your question, this guide advocates not handling programmer errors (including reading a property of undefined), and asserts that since Domains and process.on('uncaughtException') are primarily geared toward these kinds of errors, they should be avoided.

Community
  • 1
  • 1
Johann
  • 4,107
  • 3
  • 40
  • 39
  • I shall only add that the purpose of domains is to give your process a chance to **exit gracefully** when an error occurs. Since Node.js apps normally run within a long-running single process, it would be a disaster if one user doing something unexpected could affect thousands of others. Domains allow you to register the error, stop accepting new requests and allow other requests to finish, if possible. If you are looking for a error-resistant application you should use domains in combination with the `cluster` module, and every time an error occurs, just stop the worker and spawn a new one. – Robert Rossmann Jan 23 '15 at 19:11
0

i think the best way of using domains is to catch out the error. Also i recommend to restart nodejs processes at least once per few hours. And use clustering so we have a number of nodejs projects sharing and serving on the same port.

vodolaz095
  • 6,680
  • 4
  • 27
  • 42
  • I don't quite understand what you mean by catching out with domains. – Andrey Kon Nov 06 '14 at 19:38
  • domains can catch quite hard errors, that standart express-errorhandler cannot catch - like this, for example - https://github.com/vodolaz095/hunt/blob/master/examples/index.js#L219-L228 for error reported middleware this simply stops the application with 1 exit code – vodolaz095 Nov 06 '14 at 19:48