Description
- Version:
v10.15.1
(almost definitely present in older versions as well though) - Platform:
Linux lyra 4.14.97 #1-NixOS SMP Thu Jan 31 07:13:48 UTC 2019 x86_64 GNU/Linux
- Subsystem: Domains
Currently, domains execute the error
event within the context of the domain that caught the error. I'm not sure if this is entirely intentional; however, this can easily lead to an infinite loop when using a stream within the error handler due to the domains stack growing at the same rate it shrinks, e.g.:
const domain = require('domain');
const http = require('http');
const aDomain = domain.create();
aDomain.on('error', () => {
console.log("Stack length: " + domain._stack.length);
http.get('http://google.com', () => { throw new Error('foobar'); });
});
aDomain.run(() => { throw new Error('fail') });
A workaround is to use aDomain.once
instead of aDomain.on
to watch the error
event, but this workaround will only work for a certain class of consumers of domains--that is, cases where the domain is no longer used after the first error occurs.
It's also possible to manually create a new domain and execute the error
event handler within that new domain to avoid this issue.
I believe that domain error handlers should not be executed within the same domain that they're handling. However, I'm not sure if that would be considered a breaking change or a bugfix.
Also I'm not sure if there are any other ways to solve this issue and avoid the infinite loop that may not be considered breaking changes?