@@ -5,27 +5,35 @@ const { AsyncResource } = require('async_hooks');
55const { getDefaultTriggerAsyncId } = require ( 'internal/async_hooks' ) ;
66const { enqueueMicrotask } = internalBinding ( 'util' ) ;
77
8- // declared separately for name, arrow function to prevent construction
9- const queueMicrotask = ( callback ) => {
10- if ( typeof callback !== 'function' ) {
11- throw new ERR_INVALID_ARG_TYPE ( 'callback' , 'function' , callback ) ;
12- }
8+ const setupQueueMicrotask = ( triggerFatalException ) => {
9+ const queueMicrotask = ( callback ) => {
10+ if ( typeof callback !== 'function' ) {
11+ throw new ERR_INVALID_ARG_TYPE ( 'callback' , 'function' , callback ) ;
12+ }
1313
14- const asyncResource = new AsyncResource ( 'Microtask' , {
15- triggerAsyncId : getDefaultTriggerAsyncId ( ) ,
16- requireManualDestroy : true ,
17- } ) ;
14+ const asyncResource = new AsyncResource ( 'Microtask' , {
15+ triggerAsyncId : getDefaultTriggerAsyncId ( ) ,
16+ requireManualDestroy : true ,
17+ } ) ;
1818
19- enqueueMicrotask ( ( ) => {
20- asyncResource . runInAsyncScope ( ( ) => {
21- try {
22- callback ( ) ;
23- } catch ( e ) {
24- process . emit ( 'error' , e ) ;
25- }
19+ enqueueMicrotask ( ( ) => {
20+ asyncResource . runInAsyncScope ( ( ) => {
21+ try {
22+ callback ( ) ;
23+ } catch ( error ) {
24+ // TODO(devsnek) remove this if
25+ // https://bugs.chromium.org/p/v8/issues/detail?id=8326
26+ // is resolved such that V8 triggers the fatal exception
27+ // handler for microtasks
28+ triggerFatalException ( error ) ;
29+ } finally {
30+ asyncResource . emitDestroy ( ) ;
31+ }
32+ } ) ;
2633 } ) ;
27- asyncResource . emitDestroy ( ) ;
28- } ) ;
34+ } ;
35+
36+ return queueMicrotask ;
2937} ;
3038
31- module . exports = { queueMicrotask } ;
39+ module . exports = { setupQueueMicrotask } ;
0 commit comments