1'use strict'; 2 3const common = require('../common'); 4const assert = require('assert'); 5const async_hooks = require('async_hooks'); 6 7const id_obj = {}; 8let collect = true; 9 10const hook = async_hooks.createHook({ 11 before(id) { if (collect) id_obj[id] = true; }, 12 after(id) { delete id_obj[id]; }, 13}).enable(); 14 15process.once('uncaughtException', common.mustCall((er) => { 16 assert.strictEqual(er.message, 'bye'); 17 collect = false; 18})); 19 20setImmediate(common.mustCall(() => { 21 process.nextTick(common.mustCall(() => { 22 assert.strictEqual(Object.keys(id_obj).length, 0); 23 hook.disable(); 24 })); 25 26 // Create a stack of async ids that will need to be emitted in the case of 27 // an uncaught exception. 28 const ar1 = new async_hooks.AsyncResource('Mine'); 29 ar1.runInAsyncScope(() => { 30 const ar2 = new async_hooks.AsyncResource('Mine'); 31 ar2.runInAsyncScope(() => { 32 throw new Error('bye'); 33 }); 34 }); 35 36 // TODO(trevnorris): This test shows that the after() hooks are always called 37 // correctly, but it doesn't solve where the emitDestroy() is missed because 38 // of the uncaught exception. Simple solution is to always call emitDestroy() 39 // before the emitAfter(), but how to codify this? 40})); 41