Skip to content

Commit

Permalink
async_hooks: ensure event after been emitted on runInAsyncScope
Browse files Browse the repository at this point in the history
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 <vlad2t@hotmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
  • Loading branch information
legendecas authored and addaleax committed Feb 14, 2020
1 parent 065a32f commit 51fdd75
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 45 deletions.
15 changes: 10 additions & 5 deletions lib/async_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const {
executionAsyncId,
triggerAsyncId,
// Private API
hasAsyncIdStack,
getHookArrays,
enableHooks,
disableHooks,
Expand Down Expand Up @@ -172,12 +173,16 @@ class AsyncResource {
const asyncId = this[async_id_symbol];
emitBefore(asyncId, this[trigger_async_id_symbol]);

const ret = thisArg === undefined ?
fn(...args) :
ReflectApply(fn, thisArg, args);
try {
const ret = thisArg === undefined ?
fn(...args) :
ReflectApply(fn, thisArg, args);

emitAfter(asyncId);
return ret;
return ret;
} finally {
if (hasAsyncIdStack())
emitAfter(asyncId);
}
}

emitDestroy() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

require('../common');
const { AsyncResource } = require('async_hooks');

try {
new AsyncResource('foo').runInAsyncScope(() => { throw new Error('bar'); });
} catch {}
// Should abort (fail the case) if async id is not matching.

This file was deleted.

0 comments on commit 51fdd75

Please sign in to comment.