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

events: change EventTarget handler exception behavior #37237

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
3 changes: 3 additions & 0 deletions doc/api/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -1137,6 +1137,9 @@ setMaxListeners(5, target, emitter);
<!-- YAML
added: v14.5.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/37237
description: changed EventTarget error handling.
- version: v15.4.0
pr-url: https://github.com/nodejs/node/pull/35949
description: No longer experimental.
Expand Down
12 changes: 6 additions & 6 deletions lib/internal/event_target.js
Original file line number Diff line number Diff line change
Expand Up @@ -459,9 +459,9 @@ class EventTarget {
result = FunctionPrototypeCall(callback, this, arg);
}
if (result !== undefined && result !== null)
addCatch(this, result, createEvent());
addCatch(result);
} catch (err) {
emitUnhandledRejectionOrErr(this, err, createEvent());
emitUncaughtException(err);
}

handler = next;
Expand Down Expand Up @@ -624,19 +624,19 @@ function isEventTarget(obj) {
return obj?.constructor?.[kIsEventTarget];
}

function addCatch(that, promise, event) {
function addCatch(promise) {
const then = promise.then;
if (typeof then === 'function') {
FunctionPrototypeCall(then, promise, undefined, function(err) {
// The callback is called with nextTick to avoid a follow-up
// rejection from this promise.
process.nextTick(emitUnhandledRejectionOrErr, that, err, event);
emitUncaughtException(err);
});
}
}

function emitUnhandledRejectionOrErr(that, err, event) {
process.emit('error', err, event);
function emitUncaughtException(err) {
process.nextTick(() => { throw err; });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need the same stack trace enhancing machinery used in events.js here?

}

function makeEventHandler(handler) {
Expand Down
12 changes: 7 additions & 5 deletions test/parallel/test-eventtarget.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,14 +178,16 @@ let asyncTest = Promise.resolve();
}

{
const uncaughtException = common.mustCall((err, event) => {
const uncaughtException = common.mustCall((err, origin) => {
strictEqual(err.message, 'boom');
strictEqual(event.type, 'foo');
strictEqual(origin, 'uncaughtException');
}, 4);

// Whether or not the handler function is async or not, errors
// are routed to uncaughtException
process.on('error', uncaughtException);
// Make sure that we no longer call 'error' on error.
process.on('error', common.mustNotCall());
// Don't call rejection even for async handlers.
process.on('unhandledRejection', common.mustNotCall());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect we need (in a future PR) something like captureRejections from EventEmitter if there is no error between the async function throwing and uncaughtException.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

( and then possibly to remove the async behaviour entirely)

process.on('uncaughtException', uncaughtException);

const eventTarget = new EventTarget();

Expand Down
14 changes: 7 additions & 7 deletions test/parallel/test-process-uncaught-exception-monitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ process.on(
process.on('uncaughtException', common.mustCall((err, origin) => {
assert.strictEqual(origin, 'uncaughtException');
assert.strictEqual(err, theErr);
}));

process.nextTick(common.mustCall(() => {
// Test with uncaughtExceptionCaptureCallback installed
process.setUncaughtExceptionCaptureCallback(common.mustCall(
(err) => assert.strictEqual(err, theErr))
);
process.nextTick(common.mustCall(() => {
// Test with uncaughtExceptionCaptureCallback installed
process.setUncaughtExceptionCaptureCallback(common.mustCall(
(err) => assert.strictEqual(err, theErr))
);

throw theErr;
throw theErr;
}));
}));

throw theErr;