node/test/parallel/test-domain-thrown-error-handler-stack.js
Julien Gilli 43a5170858 domain: error handler runs outside of its domain
Before this change, domains' error handlers would run with the
corresponding domain as the active domain. This creates the
possibility for domains' error handlers to call themselves recursively
if an event emitter created in the error handler emits an error, or if
the error handler throws an error.

This change sets the active domain to be the domain's parent (or null
if the domain for which the error handler is called has no parent) to
prevent that from happening.

Fixes: https://github.com/nodejs/node/issues/26086
PR-URL: https://github.com/nodejs/node/pull/26211
Reviewed-By: Vladimir de Turckheim <vlad2t@hotmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Rich Trott <rtrott@gmail.com>
2019-06-12 22:01:07 -07:00

47 lines
1.1 KiB
JavaScript

'use strict';
const common = require('../common');
const domain = require('domain');
/*
* Make sure that when an erorr is thrown from a nested domain, its error
* handler runs outside of that domain, but within the context of any parent
* domain.
*/
const d = domain.create();
const d2 = domain.create();
d2.on('error', common.mustCall((err) => {
if (domain._stack.length !== 1) {
console.error('domains stack length should be 1 but is %d',
domain._stack.length);
process.exit(1);
}
if (process.domain !== d) {
console.error('active domain should be %j but is %j', d, process.domain);
process.exit(1);
}
process.nextTick(() => {
if (domain._stack.length !== 1) {
console.error('domains stack length should be 1 but is %d',
domain._stack.length);
process.exit(1);
}
if (process.domain !== d) {
console.error('active domain should be %j but is %j', d,
process.domain);
process.exit(1);
}
});
}));
d.run(() => {
d2.run(() => {
throw new Error('oops');
});
});