Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

process: ignore asyncId 0 in exception handler #41424

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions lib/internal/process/execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ const {
clearAsyncIdStack,
hasAsyncIdStack,
afterHooksExist,
emitAfter
emitAfter,
popAsyncContext,
} = require('internal/async_hooks');

// shouldAbortOnUncaughtToggle is a typed array for faster
Expand Down Expand Up @@ -183,7 +184,11 @@ function createOnGlobalUncaughtException() {
// Emit the after() hooks now that the exception has been handled.
if (afterHooksExist()) {
do {
emitAfter(executionAsyncId());
const asyncId = executionAsyncId();
if (asyncId === 0)
popAsyncContext(0);
else
emitAfter(asyncId);
} while (hasAsyncIdStack());
}
// And completely empty the id stack, including anything that may be
Expand Down
3 changes: 0 additions & 3 deletions test/async-hooks/init-hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,6 @@ class ActivityCollector {
}
const err = new Error(`Found a handle whose ${hook}` +
' hook was invoked but not its init hook');
// Don't throw if we see invocations due to an assertion in a test
// failing since we want to list the assertion failure instead
if (/process\._fatalException/.test(err.stack)) return null;
throw err;
}
return h;
Expand Down
17 changes: 17 additions & 0 deletions test/async-hooks/test-unhandled-exception-valid-ids.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

const common = require('../common');
const initHooks = require('./init-hooks');

const hooks = initHooks();
hooks.enable();

setImmediate(() => {
throw new Error();
});

setTimeout(() => {
throw new Error();
}, 1);

process.on('uncaughtException', common.mustCall(2));