You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fix(ext/node): fire uncaughtExceptionMonitor with correct origin for sync top-level throws (#34048)
## Summary
`process.on('uncaughtExceptionMonitor', handler)` was firing with
`origin === 'unhandledRejection'` instead of `'uncaughtException'` for
synchronous top-level throws in CommonJS entry modules.
Root cause: in Deno, a synchronous top-level throw in a CJS entry
surfaces as a module-evaluation rejection (the entry CJS module is
loaded through an ESM wrapper generated by `node_resolver::analyze`).
Without intervention, the rejection is routed through Deno's
unhandled-rejection path, so the monitor/uncaughtException listeners see
the wrong origin.
## Fix
- `ext/node/polyfills/01_require.js`: catch the sync entry-module throw
in `Module._load` and invoke `process._fatalException` up-front so the
monitor and `uncaughtException` listeners see `origin ===
'uncaughtException'`, matching Node.js semantics. The error is marked in
a WeakSet so the unhandled-rejection fallback in `process.ts` won't
re-fire when the rejection arrives.
- `ext/node/polyfills/process.ts`:
- Track `uncaughtExceptionMonitor` listener count so the monitor still
fires when no `uncaughtException`/`unhandledRejection` listener is
registered.
- Only `preventDefault()` on the unhandled-rejection event when a
registered handler actually consumed the error (so unhandled cases still
terminate).
## Tests
New spec test at `tests/specs/node/uncaught_exception_monitor/`:
- `entry.cjs`: verifies monitor fires with `origin ===
'uncaughtException'` for a sync top-level throw.
- `rejection.cjs`: verifies monitor fires with `origin ===
'unhandledRejection'` for an actual unhandled promise rejection.
## Test plan
- [x] `cargo test -p deno --test integration_tests
specs::node::uncaught_exception_monitor`
- [x] Verified `node:test` and other existing process-event tests still
pass.
Closes denoland/orchid#54
This replaces #34040 (that PR had unrelated commits merged in from main;
this branch contains only the two fix commits).
0 commit comments