-
-
Notifications
You must be signed in to change notification settings - Fork 31.5k
EventTarget uncaught exception handling differs from spec/browser #46852
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
Comments
Are there WPT tests that cover this behavior? If yes, it's a matter of enabling them in test/wpt and fixing any failures. If no, then first order of business is adding them upstream (to dom/events, probably.) |
Indeed, the request here is to "somehow" both synchronously call the uncaught exception path as continuing the execution of the listeners. Note that a major difference between browsers and Node is that if an uncaught exception handler does not exist, we'll never call the other handlers - which might not be what we want. Regarding the implementation, I think that this would basically mean changing the current implementation in event_target to call |
@wnayes do you plan to work on this? |
First - let me clear up a misconception. No it doesn't as far as I know. The error in this case in spec speak is "report the exception", it is quite intentionally implementation defined . We can (for example) suppress that error and ignore it and still implement the spec correctly. Error handling is fundamentally different between browsers and server runtimes so it makes sense the spec allows this. Let's focus on how to be useful and less surprising to our users. Spec compliance isn't the issue here the fact you feel like you ran into a footgun is. I see several viable paths forward:
|
I had been trying to re-implement this tricky thing from React. It uses I opened this issue because I was surprised with the extent that EventTarget in Node differed from browser, though the use case I was focusing on is certainly obscure. I have no concrete plans to look into the EventTarget changes myself, though I am still a bit curious if there is some way to achieve the callback/debugger behavior mentioned. More recently, I looked into EventEmitter and Domains, and these all behaved similar to EventTarget as far as callback dispatch goes, and I was stumped at that point. |
That's fair, and in the past we did fire an
Sure, can you elaborate on where you're stuck in particular? You can add a |
The goal is to have an API that behaves like this: function invokeGuardedCallback(callback, onError) {
try {
callback();
} catch (e) {
onError(e);
}
} Except we don't want to use that In browsers, this can be implemented with this trick (some details omitted below): function invokeGuardedCallback(callback, onError) {
let error;
globalThis.addEventListener("error", (e) => {
error = e;
});
const target = new EventTarget();
target.addEventListener("fakeevent", () => { callback(); });
target.dispatchEvent(new Event("fakeevent"));
if (error) {
onError(error);
}
} With the current behavior in Node, the above approach doesn't functionally work, because the error event ( If it were changed to fire synchronously during dispatch, the code would start to function correctly. But since To fix the caught exceptions debugger concern, it seems like the Node internal code needs its own sort of |
@benjamingr is ^-- actionable? If not, then I'll close this out as working-as-expected, I suppose? |
It's actionable (to let users add code where the debugger doesn't break and doesn't interfere with error handling) but should likely be a separate issue |
Okay then, I'll go ahead and close this issue and @wnayes can open a new one for the debugger thingy. But please reopen if I misunderstood you. |
Version
v19.7.0
Platform
Linux 6.1.12-arch1-1 #1 SMP PREEMPT_DYNAMIC Tue, 14 Feb 2023 22:08:08 +0000 x86_64 GNU/Linux
Subsystem
No response
What steps will reproduce the bug?
This is very similar to #38122. Re-opening essentially, to consider further refining the behavior.
Here is the example from that issue:
This can be ran in both Node and browsers.
How often does it reproduce? Is there a required condition?
Consistent behavior.
What is the expected behavior?
Browsers will trigger the unhandled exception handling (window onerror) immediately, followed by the two console logs.
What do you see instead?
Node is delaying the unhandled exception handling until the "next tick."
Additional information
The behavior is due to use of
emitUncaughtException
during dispatch in event_target.js, which throws the error on the next tick.The impacts seem to be that:
It seems like a general direction towards closer behavior with browsers would be (1) throw immediately, not on next tick and (2) don't wrap dispatching with a try/catch. But if those things were all that was done, I'm not sure how execution would resume (to process subsequent handlers) after a handled exception that doesn't terminate.
The text was updated successfully, but these errors were encountered: