Rethrow event listener errors synchronously for native event dispatch (#57207)#57207
Open
rubennorte wants to merge 1 commit into
Open
Rethrow event listener errors synchronously for native event dispatch (#57207)#57207rubennorte wants to merge 1 commit into
rubennorte wants to merge 1 commit into
Conversation
|
@rubennorte has exported this pull request. If you are a Meta employee, you can view the originating Diff in D108622141. |
…react#57207) Summary: Pull Request resolved: react#57207 Under the EventTarget-based event dispatch path, an error thrown by an event handler (e.g. `onPress`, `onScroll`) was deferred to a new task via `setTimeout(0)` in `reportListenerError`. This diverged from the legacy plugin path, which collected the first listener error and rethrew it synchronously at the end of the dispatch (React's `runEventsInBatch` + `rethrowCaughtError`). As a result, handler errors escaped the synchronous dispatch flow — they were no longer catchable by React error boundaries or the native event call, and instead surfaced as deferred, uncaught global errors. This restores the legacy contract for native event dispatch: - `dispatchTrustedEvent` gains an opt-in `rethrowListenerErrors` argument. `dispatch()` threads a per-dispatch error holder through `invoke`/`invokeListeners`; when the flag is set it records the first listener error and rethrows it synchronously after the event has been fully cleaned up. - The renderer's native event dispatch (`dispatchNativeEvent`) opts in, so listener errors propagate synchronously again. The responder lifecycle `rethrowCaughtError()` is moved into a `finally` so a pending responder error can never leak into a later dispatch if the normal dispatch throws. - The public `dispatchEvent` API and other `EventTarget` consumers (e.g. `XMLHttpRequest`) are unchanged: they keep the DOM contract of reporting listener errors to the global error handler without throwing. - Re-enabled the previously skipped event-dispatch error-handling integration tests so they run in both dispatch modes. Changelog: [Internal] Reviewed By: javache Differential Revision: D108622141
c3d971b to
187513f
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary:
Under the EventTarget-based event dispatch path, an error thrown by an event handler (e.g.
onPress,onScroll) was deferred to a new task viasetTimeout(0)inreportListenerError. This diverged from the legacy plugin path, which collected the first listener error and rethrew it synchronously at the end of the dispatch (React'srunEventsInBatch+rethrowCaughtError). As a result, handler errors escaped the synchronous dispatch flow — they were no longer catchable by React error boundaries or the native event call, and instead surfaced as deferred, uncaught global errors.This restores the legacy contract for native event dispatch:
dispatchTrustedEventgains an opt-inrethrowListenerErrorsargument.dispatch()threads a per-dispatch error holder throughinvoke/invokeListeners; when the flag is set it records the first listener error and rethrows it synchronously after the event has been fully cleaned up.dispatchNativeEvent) opts in, so listener errors propagate synchronously again. The responder lifecyclerethrowCaughtError()is moved into afinallyso a pending responder error can never leak into a later dispatch if the normal dispatch throws.dispatchEventAPI and otherEventTargetconsumers (e.g.XMLHttpRequest) are unchanged: they keep the DOM contract of reporting listener errors to the global error handler without throwing.Changelog: [Internal]
Reviewed By: javache
Differential Revision: D108622141