Skip to content

Commit 51fdd75

Browse files
legendecasaddaleax
authored andcommitted
async_hooks: ensure event after been emitted on runInAsyncScope
The exception handler user-defined will not automatically emit after for the async resource. Also removes a duplicated case `test-emit-after-uncaught-exception-runInAsyncScope.js` which is identical to test-emit-after-uncaught-exception.js. Refs: #30965 PR-URL: #31784 Fixes: #31783 Reviewed-By: Vladimir de Turckheim <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent 065a32f commit 51fdd75

3 files changed

+19
-45
lines changed

lib/async_hooks.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const {
2121
executionAsyncId,
2222
triggerAsyncId,
2323
// Private API
24+
hasAsyncIdStack,
2425
getHookArrays,
2526
enableHooks,
2627
disableHooks,
@@ -172,12 +173,16 @@ class AsyncResource {
172173
const asyncId = this[async_id_symbol];
173174
emitBefore(asyncId, this[trigger_async_id_symbol]);
174175

175-
const ret = thisArg === undefined ?
176-
fn(...args) :
177-
ReflectApply(fn, thisArg, args);
176+
try {
177+
const ret = thisArg === undefined ?
178+
fn(...args) :
179+
ReflectApply(fn, thisArg, args);
178180

179-
emitAfter(asyncId);
180-
return ret;
181+
return ret;
182+
} finally {
183+
if (hasAsyncIdStack())
184+
emitAfter(asyncId);
185+
}
181186
}
182187

183188
emitDestroy() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict';
2+
3+
require('../common');
4+
const { AsyncResource } = require('async_hooks');
5+
6+
try {
7+
new AsyncResource('foo').runInAsyncScope(() => { throw new Error('bar'); });
8+
} catch {}
9+
// Should abort (fail the case) if async id is not matching.

test/parallel/test-emit-after-uncaught-exception-runInAsyncScope.js

-40
This file was deleted.

0 commit comments

Comments
 (0)