Skip to content

updateRuntimeShadowNodeReferencesOnCommit Feature Flag + Enable #82

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

Closed
wants to merge 4 commits into from

Conversation

Flewp
Copy link

@Flewp Flewp commented Jul 7, 2025

This moves shadow node reference updates to the commit phase with the goal of thread safety for syncing shadow nodes and renderer state correctness. Potential fix for some state issues involving nodes/views that don't exist.

Combination of a munging of three commits:

Flewp and others added 4 commits July 7, 2025 17:43
Summary:
Pull Request resolved: facebook#50753

Runtime Shadow Node Reference Updates (RSNRU) is currently implemented through the clone method which on each internal clone updates the runtime reference to point to the new clone. This guarantees that the runtime reference always points at the latest revision of the shadow node.

This came with the constraint that RSNRU could only run from one thread at all times, otherwise the React renderer state (current fiber tree) would end up being corrupted by receiving reference updates from multiple threads cloning shadow nodes.

This change moves the reference update step to the locked scope of the commit phase. Since the runtime is blocking on the commit and the scope is locked, it is safe and correct to update the runtime references with the latest revision of the shadow node after running state progression and layout.

By moving the reference update to the commit, we can support shadow node syncing from any thread since the actual runtime references are now executing at a safe time and the renderer state will stay valid at all times.

This change is gated behind the `updateRuntimeShadowNodeReferencesOnCommit` feature flag, which enabled shadow node syncing from any thread and reference updates only during the commit.

Changelog: [Internal]

Reviewed By: rubennorte

Differential Revision: D73038439

fbshipit-source-id: d90308498f3c0625dc87158f15311d1088aad8b0
Summary:
Pull Request resolved: facebook#51843

Changelog: [Internal]

To avoid corrupting the React fiber tree when committing from multiple threads this diff only updates shadow node references within the fiber tree for commits originating from React.

This guarantees that during the update of the references no React render will start or is running, making the update of the shadow node reference safe to execute.

S527994

Reviewed By: sammy-SC

Differential Revision: D76043845

fbshipit-source-id: bfcbeaae7fc8b976a1c2db6682330cef9ca25ab8
…facebook#49981)

Summary:
Pull Request resolved: facebook#49981

Changelog: [internal]

(because MutationObserver isn't a public API yet)

## Context

`MutationObserver` is a [JavaScript API](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver) used to report mutations in the DOM tree. The mutations available on Web are: changing children, changing attributes and changing text. In React Native, only changing children is supported.

Mutation detection needs to happen synchronously when mutations happen (in ShadowTree commits) and the notification is dispatched as a microtask in JS, which means we can only report mutations happening in JavaScript.

There's an assumption that only React (in JS) can change the structure of the tree in Fabric, so we implemented `MutationObserver` considering this assummption.

Unfortunately, while the assumption is correct (we can only mutate children from React) the implementation didn't take into account that commit hooks were triggered from multiple threads, even if the structure doesn't change (e.g.: with Fabric state updates). In this case, we do the checks but we never dispatch the notifications. This can cause crashes (see T217617393) if we try to check for mutations from the main thread while we add new observers in the JS thread (because that logic wasn't thread safe).

This fixes that crash by, in MutationObserver, not only preventing notifications from commits not coming from React, but also preventing the determination altogether.

In order to do this, this modifies the signature of the commit hooks to also pass the commit options, and adds a new field in the commit options with the source of the commit (for now, just "React" or "Unknown").

In `MutationObserver`, before accessing the data structures of the observer, we check if the commit is coming from React, and return early otherwise.

Reviewed By: javache

Differential Revision: D71036705

fbshipit-source-id: 985c8f903375cbf876dce5174e04563f74d7621a
@Flewp
Copy link
Author

Flewp commented Jul 8, 2025

Closing in favor of just cherry-picking facebook#49981 straight to the .78 branch.

@Flewp Flewp closed this Jul 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants