Skip to content

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: https://github.com/nodejs/node/issues/22982

Tests failing before the fix:

=== release test-timers.setInterval ===                                       
Path: async-hooks/test-timers.setInterval
Error: Found a handle whose after hook was invoked but not its init hook
    at ActivityCollector._getActivity (/Users/apapirovski/Web/nodejs/test/async-hooks/init-hooks.js:169:19)
    at ActivityCollector._after (/Users/apapirovski/Web/nodejs/test/async-hooks/init-hooks.js:200:20)
    at emitHook (node:internal/async_hooks:233:38)
    at emitAfterScript (node:internal/async_hooks:520:5)
    at process._fatalException (node:internal/process/execution:192:9)
node:assert:123
  throw new AssertionError(obj);
  ^

AssertionError [ERR_ASSERTION]: Checking invocations at stage "t1: when process exits":
     Called "before" 1 time(s), but expected 2 invocation(s).
    at checkHook (/Users/apapirovski/Web/nodejs/test/async-hooks/hook-checks.js:51:14)
    at Array.forEach (<anonymous>)
    at checkInvocations (/Users/apapirovski/Web/nodejs/test/async-hooks/hook-checks.js:28:62)
    at process.<anonymous> (/Users/apapirovski/Web/nodejs/test/async-hooks/test-timers.setInterval.js:44:3)
    at process.emit (node:events:532:35)
    at process.exit (node:internal/process/per_thread:183:15)
    at fatalError (node:internal/async_hooks:174:11)
    at emitHook (node:internal/async_hooks:237:5)
    at emitAfterScript (node:internal/async_hooks:520:5)
    at process._fatalException (node:internal/process/execution:192:9) {
  generatedMessage: false,
  code: 'ERR_ASSERTION',
  actual: 1,
  expected: 2,
  operator: 'strictEqual'
}

Node.js v18.0.0-pre
Command: out/Release/node /Users/apapirovski/Web/nodejs/test/async-hooks/test-timers.setInterval.js
=== release test-unhandled-exception-valid-ids ===                    
Path: async-hooks/test-unhandled-exception-valid-ids
Error: Found a handle whose after hook was invoked but not its init hook
    at ActivityCollector._getActivity (/Users/apapirovski/Web/nodejs/test/async-hooks/init-hooks.js:169:19)
    at ActivityCollector._after (/Users/apapirovski/Web/nodejs/test/async-hooks/init-hooks.js:200:20)
    at emitHook (node:internal/async_hooks:233:38)
    at emitAfterScript (node:internal/async_hooks:520:5)
    at process._fatalException (node:internal/process/execution:192:9)
Command: out/Release/node /Users/apapirovski/Web/nodejs/test/async-hooks/test-unhandled-exception-valid-ids.js

Merge request reports

Loading