Skip to content

Drain ResizeObserver callback microtasks before rendering (#60436) - #60436

Merged
meta-codesync[bot] merged 1 commit into
react:mainfrom
rubennorte:export-D135358311
Dec 2, 2026
Merged

meta-codesync[bot] merged 1 commit into
react:mainfrom
rubennorte:export-D135358311

Conversation

@rubennorte

@rubennorte rubennorte commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

Summary:

Context

ResizeObserver callbacks run during the rendering phase, after the task's normal microtask checkpoint. Jobs queued by those callbacks were left pending until another task arrived, so a later observer or native rendering could run before those jobs.

On the Web, broadcast active resize observations, §3.4.5 step 2.4 invokes each observer's callback individually using Web IDL's callback invocation algorithm. Its step 13.2 performs HTML script cleanup, which triggers a microtask checkpoint when the JavaScript execution-context stack becomes empty. For top-level rendering-phase delivery, that checkpoint drains queued jobs before the invocation returns and the broadcast continues. HTML's rendering algorithm places the ResizeObserver loop before the final UI update.

Changes

Deliver each observer's native notification separately through a scheduler-owned callback invoker. The invoker calls the notification, performs callback cleanup (including its microtask checkpoint), and then returns to the manager. The manager has no raw microtask-draining capability. Snapshot entries and root their JavaScript identities for the whole broadcast so observer removal and garbage collection cannot invalidate later deliveries.

The task checkpoint keeps its original error-handling policy. Uncaught observer checkpoint and notification errors unwind the observer machinery before clearing queued work and invoking the host fatal handler, then stop the rendering step. This prevents pending callbacks from accessing delegates destroyed during teardown. Handled observer exceptions continue normally. Keep the existing microtask-checkpoint reentry protection, without adding task/rendering-phase state or redundant entry guards.

Add public-behavior integration coverage for FIFO promise/await/nested jobs, inter-observer ordering, pre-mount execution, deeper-target observations, nested measure callbacks, cancellation and error recovery. Update existing state-update tests to distinguish a real later rendering task from jobs that were previously stuck until an arbitrary wake.

Changelog:
[General][Fixed] - Run ResizeObserver callback microtasks before later observers and native rendering.

Reviewed By: christophpurrer

Differential Revision: D135358311

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Dec 1, 2026
@facebook-github-tools facebook-github-tools Bot added p: Facebook Partner: Facebook Partner labels Dec 1, 2026
@meta-codesync

meta-codesync Bot commented Dec 1, 2026

Copy link
Copy Markdown

@rubennorte has exported this pull request. If you are a Meta employee, you can view the originating Diff in D135358311.

@christophpurrer christophpurrer left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Review automatically exported from Phabricator review in Meta.

@meta-codesync meta-codesync Bot changed the title Drain ResizeObserver callback microtasks before rendering Drain ResizeObserver callback microtasks before rendering (#60436) Dec 2, 2026
rubennorte added a commit to rubennorte/react-native that referenced this pull request Dec 2, 2026
)

Summary:

## Context
`ResizeObserver` callbacks run during the rendering phase, after the task's normal microtask checkpoint. Jobs queued by those callbacks were left pending until another task arrived, so a later observer or native rendering could run before those jobs.

On the Web, [broadcast active resize observations, §3.4.5 step 2.4](https://drafts.csswg.org/resize-observer/#broadcast-active-resize-observations) invokes each observer's callback individually using [Web IDL's callback invocation algorithm](https://webidl.spec.whatwg.org/#invoke-a-callback-function). Its step 13.2 performs [HTML script cleanup](https://html.spec.whatwg.org/multipage/webappapis.html#clean-up-after-running-script), which triggers a [microtask checkpoint](https://html.spec.whatwg.org/multipage/webappapis.html#perform-a-microtask-checkpoint) when the JavaScript execution-context stack becomes empty. For top-level rendering-phase delivery, that checkpoint drains queued jobs before the invocation returns and the broadcast continues. [HTML's rendering algorithm](https://html.spec.whatwg.org/multipage/webappapis.html#update-the-rendering) places the ResizeObserver loop before the final UI update.

## Changes
Deliver each observer's native notification separately through a scheduler-owned callback invoker. The invoker calls the notification, performs callback cleanup (including its microtask checkpoint), and then returns to the manager. The manager has no raw microtask-draining capability. Snapshot entries and root their JavaScript identities for the whole broadcast so observer removal and garbage collection cannot invalidate later deliveries.

The task checkpoint keeps its original error-handling policy. Uncaught observer checkpoint and notification errors unwind the observer machinery before clearing queued work and invoking the host fatal handler, then stop the rendering step. This prevents pending callbacks from accessing delegates destroyed during teardown. Handled observer exceptions continue normally. Keep the existing microtask-checkpoint reentry protection, without adding task/rendering-phase state or redundant entry guards.

Add public-behavior integration coverage for FIFO promise/await/nested jobs, inter-observer ordering, pre-mount execution, deeper-target observations, nested measure callbacks, cancellation and error recovery. Update existing state-update tests to distinguish a real later rendering task from jobs that were previously stuck until an arbitrary wake.

Changelog:
[General][Fixed] - Run ResizeObserver callback microtasks before later observers and native rendering.

Reviewed By: christophpurrer

Differential Revision: D135358311
)

Summary:

## Context
`ResizeObserver` callbacks run during the rendering phase, after the task's normal microtask checkpoint. Jobs queued by those callbacks were left pending until another task arrived, so a later observer or native rendering could run before those jobs.

On the Web, [broadcast active resize observations, §3.4.5 step 2.4](https://drafts.csswg.org/resize-observer/#broadcast-active-resize-observations) invokes each observer's callback individually using [Web IDL's callback invocation algorithm](https://webidl.spec.whatwg.org/#invoke-a-callback-function). Its step 13.2 performs [HTML script cleanup](https://html.spec.whatwg.org/multipage/webappapis.html#clean-up-after-running-script), which triggers a [microtask checkpoint](https://html.spec.whatwg.org/multipage/webappapis.html#perform-a-microtask-checkpoint) when the JavaScript execution-context stack becomes empty. For top-level rendering-phase delivery, that checkpoint drains queued jobs before the invocation returns and the broadcast continues. [HTML's rendering algorithm](https://html.spec.whatwg.org/multipage/webappapis.html#update-the-rendering) places the ResizeObserver loop before the final UI update.

## Changes
Deliver each observer's native notification separately through a scheduler-owned callback invoker. The invoker calls the notification, performs callback cleanup (including its microtask checkpoint), and then returns to the manager. The manager has no raw microtask-draining capability. Snapshot entries and root their JavaScript identities for the whole broadcast so observer removal and garbage collection cannot invalidate later deliveries.

The task checkpoint keeps its original error-handling policy. Uncaught observer checkpoint and notification errors unwind the observer machinery before clearing queued work and invoking the host fatal handler, then stop the rendering step. This prevents pending callbacks from accessing delegates destroyed during teardown. Handled observer exceptions continue normally. Keep the existing microtask-checkpoint reentry protection, without adding task/rendering-phase state or redundant entry guards.

Add public-behavior integration coverage for FIFO promise/await/nested jobs, inter-observer ordering, pre-mount execution, deeper-target observations, nested measure callbacks, cancellation and error recovery. Update existing state-update tests to distinguish a real later rendering task from jobs that were previously stuck until an arbitrary wake.

Changelog:
[General][Fixed] - Run ResizeObserver callback microtasks before later observers and native rendering.

Reviewed By: christophpurrer

Differential Revision: D135358311
@meta-codesync
meta-codesync Bot merged commit 97b3c62 into react:main Dec 2, 2026
101 checks passed
@react-native-bot react-native-bot added the Merged This PR has been merged. label Dec 2, 2026
@react-native-bot

Copy link
Copy Markdown
Collaborator

This pull request was successfully merged by @rubennorte in 97b3c62

When will my fix make it into a release? | How to file a pick request?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged This PR has been merged. meta-exported p: Facebook Partner: Facebook Partner

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants