mirror of
https://github.com/nodejs/node.git
synced 2025-05-03 00:27:26 +00:00

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>
34 lines
1.1 KiB
JavaScript
34 lines
1.1 KiB
JavaScript
'use strict';
|
|
|
|
const common = require('../common');
|
|
const domain = require('domain');
|
|
|
|
/*
|
|
* Make sure that the domains stack is cleared after a top-level domain
|
|
* error handler exited gracefully.
|
|
*/
|
|
const d = domain.create();
|
|
|
|
d.on('error', common.mustCall(() => {
|
|
// Scheduling a callback with process.nextTick _could_ enter a _new_ domain,
|
|
// but domain's error handlers are called outside of their domain's context.
|
|
// So there should _no_ domain on the domains stack if the domains stack was
|
|
// cleared properly when the domain error handler was called.
|
|
process.nextTick(() => {
|
|
if (domain._stack.length !== 0) {
|
|
// Do not use assert to perform this test: this callback runs in a
|
|
// different callstack as the original process._fatalException that
|
|
// handled the original error, thus throwing here would trigger another
|
|
// call to process._fatalException, and so on recursively and
|
|
// indefinitely.
|
|
console.error('domains stack length should be 0, but instead is:',
|
|
domain._stack.length);
|
|
process.exit(1);
|
|
}
|
|
});
|
|
}));
|
|
|
|
d.run(() => {
|
|
throw new Error('Error from domain');
|
|
});
|