Skip to content

Commit 700f766

Browse files
apapirovskiBethGriggs
authored andcommitted
process: ignore asyncId 0 in exception handler
Today, the global uncaught exception handler is the only place where asyncId 0 is not ignored and we still proceed to call emitAfter. This would've already failed one of our correctness tests in async_hooks if not for some other code meant to handle a different edge case. Fixes: #22982 PR-URL: #41424 Reviewed-By: Gerhard Stöbich <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 8681c83 commit 700f766

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

lib/internal/process/execution.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ const {
2020
clearAsyncIdStack,
2121
hasAsyncIdStack,
2222
afterHooksExist,
23-
emitAfter
23+
emitAfter,
24+
popAsyncContext,
2425
} = require('internal/async_hooks');
2526

2627
// shouldAbortOnUncaughtToggle is a typed array for faster
@@ -183,7 +184,11 @@ function createOnGlobalUncaughtException() {
183184
// Emit the after() hooks now that the exception has been handled.
184185
if (afterHooksExist()) {
185186
do {
186-
emitAfter(executionAsyncId());
187+
const asyncId = executionAsyncId();
188+
if (asyncId === 0)
189+
popAsyncContext(0);
190+
else
191+
emitAfter(asyncId);
187192
} while (hasAsyncIdStack());
188193
}
189194
// And completely empty the id stack, including anything that may be

test/async-hooks/init-hooks.js

-3
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,6 @@ class ActivityCollector {
168168
}
169169
const err = new Error(`Found a handle whose ${hook}` +
170170
' hook was invoked but not its init hook');
171-
// Don't throw if we see invocations due to an assertion in a test
172-
// failing since we want to list the assertion failure instead
173-
if (/process\._fatalException/.test(err.stack)) return null;
174171
throw err;
175172
}
176173
return h;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const initHooks = require('./init-hooks');
5+
6+
const hooks = initHooks();
7+
hooks.enable();
8+
9+
setImmediate(() => {
10+
throw new Error();
11+
});
12+
13+
setTimeout(() => {
14+
throw new Error();
15+
}, 1);
16+
17+
process.on('uncaughtException', common.mustCall(2));

0 commit comments

Comments
 (0)