1'use strict'; 2 3const common = require('../common'); 4const domain = require('domain'); 5 6/* 7 * Make sure that the domains stack is cleared after a top-level domain 8 * error handler exited gracefully. 9 */ 10const d = domain.create(); 11 12d.on('error', common.mustCall(() => { 13 process.nextTick(() => { 14 // Scheduling a callback with process.nextTick will enter a _new_ domain, 15 // and the callback will be called after the domain that handled the error 16 // was exited. So there should be only one domain on the domains stack if 17 // the domains stack was cleared properly when the domain error handler 18 // returned. 19 if (domain._stack.length !== 1) { 20 // Do not use assert to perform this test: this callback runs in a 21 // different callstack as the original process._fatalException that 22 // handled the original error, thus throwing here would trigger another 23 // call to process._fatalException, and so on recursively and 24 // indefinitely. 25 console.error('domains stack length should be 1, but instead is:', 26 domain._stack.length); 27 process.exit(1); 28 } 29 }); 30})); 31 32d.run(() => { 33 throw new Error('Error from domain'); 34}); 35