Skip to content

Add guards around nativeProps usage to prevent race conditions - #52646

Closed
OrfeasZ wants to merge 2 commits into
react:mainfrom
OrfeasZ:fix/clone-prop-update-race-condition
Closed

OrfeasZ wants to merge 2 commits into
react:mainfrom
OrfeasZ:fix/clone-prop-update-race-condition

Conversation

@OrfeasZ

@OrfeasZ OrfeasZ commented Jul 16, 2025

Copy link
Copy Markdown
Contributor

Summary:

Some third-party libraries, like react-native-reanimated, can clone nodes in a different thread while react-native is calling setNativeProps_DEPRECATED. This results in a race condition, where a stale pointer to nativeProps_DEPRECATED can be accessed, resulting in a crash. This usually manifests as a EXC_BAD_ACCESS crash on iOS. On Android it seems more rare. We've added a lock around accesses to nativeProps_DEPRECATED, but alternative options of fixing this can be considered too.

For more information see software-mansion/react-native-reanimated#7666

Changelog:

[INTERNAL] [FIXED] - Fixed crashes caused by race conditions when third-party libraries clone the shadow dom from a different thread

Test Plan:

Due to this being a race condition that only manifests in rare circumstances, it's very difficult to create a reliable reproduction case. The issue mentioned above contains ThreadSanitizer logs that demonstrate this issue. TSan no longer complains with this patch applied, and we've not seen any additional issues from it after deploying it in production over the past week.

@meta-cla

meta-cla Bot commented Jul 16, 2025

Copy link
Copy Markdown

Hi @OrfeasZ!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@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 Jul 16, 2025
@meta-cla

meta-cla Bot commented Jul 16, 2025

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@facebook-github-bot facebook-github-bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Jul 16, 2025
@OrfeasZ
OrfeasZ force-pushed the fix/clone-prop-update-race-condition branch from 67241d7 to b350148 Compare September 2, 2025 00:36
@renyufei8023

Copy link
Copy Markdown

rn0.81.1 still has this issue, when will we merge it?

@bartlomiejbloniarz

Copy link
Copy Markdown
Contributor

@cipolleschi Hi! Could we have someone look at this? I think it could also be a problem for c++ animated.

@ismaelsousa

ismaelsousa commented Sep 20, 2025 •

Copy link
Copy Markdown

Hello, have any of you had any problems with the patch so far?

[Edited - 22 Sep]
I will release it today for my users

@ismaelsousa

ismaelsousa commented Sep 24, 2025 •

Copy link
Copy Markdown

I patched locally and released it to my users so far, so great, no more crashes 🎉
Someone from meta really needs to look at this. I was having a 23% crash rate in the users' sections

* architecture and will be removed in the future.
*/
mutable std::unique_ptr<folly::dynamic> nativeProps_DEPRECATED;
mutable std::recursive_mutex nativePropsMutex;

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.

Any reason this needs to be a recursive mutex instead of a plain one?

Does this need to be ShadowNodeFamily scoped, or would a single global mutex on UIManager be sufficient?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I don't know enough about the internals of react native, so took the safe approach of using a recursive one to prevent deadlocks in the case the critical path ends up trying to acquire the lock again recursively. That said, if that can't happen, it could be replaced with a regular mutex.

As for scoping, I'm not sure (again due to lack of internals knowledge). The main issue this tries to address is concurrent writes / reads to the nativeProps_DEPRECATED field, which is why I added it there, trying to keep the locking exactly to where it's needed.

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.

Let's stick with a normal mutex here - I don't see any way this could be re-entrant.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@javache Switched to a regular mutex.

@OrfeasZ

OrfeasZ commented Feb 3, 2026

Copy link
Copy Markdown
Contributor Author

@javache Could we get an update on this? Is there anything I should change so we can get this merged?

@michael-tugby

Copy link
Copy Markdown

@javache Could we get an update on this? We need this fix for our project.

OrfeasZ added 2 commits June 29, 2026 22:38
Some third-party libraries, like react-native-reanimated, can clone nodes
in a different thread while react-native is calling setNativeProps_DEPRECATED.
This results in a race condition, where a stale pointer to nativeProps_DEPRECATED
can be accessed, resulting in a crash.
@OrfeasZ
OrfeasZ force-pushed the fix/clone-prop-update-race-condition branch from b350148 to 5b4d368 Compare June 29, 2026 19:41
@meta-codesync

meta-codesync Bot commented Jun 30, 2026

Copy link
Copy Markdown

@javache has imported this pull request. If you are a Meta employee, you can view this in D110169424.

@meta-codesync meta-codesync Bot closed this in 923e7dd Jul 1, 2026
@meta-codesync meta-codesync Bot added the Merged This PR has been merged. label Jul 1, 2026
@meta-codesync

meta-codesync Bot commented Jul 1, 2026

Copy link
Copy Markdown

@javache merged this pull request in 923e7dd.

gabrieldonadel pushed a commit that referenced this pull request Jul 23, 2026
Summary:
Some third-party libraries, like react-native-reanimated, can clone nodes in a different thread while react-native is calling `setNativeProps_DEPRECATED`. This results in a race condition, where a stale pointer to `nativeProps_DEPRECATED` can be accessed, resulting in a crash. This usually manifests as a `EXC_BAD_ACCESS` crash on iOS. On Android it seems more rare. We've added a lock around accesses to nativeProps_DEPRECATED, but alternative options of fixing this can be considered too.

For more information see software-mansion/react-native-reanimated#7666

[INTERNAL] [FIXED] - Fixed crashes caused by race conditions when third-party libraries clone the shadow dom from a different thread

Pull Request resolved: #52646

Test Plan:
Due to this being a race condition that only manifests in rare circumstances, it's very difficult to create a reliable reproduction case. The issue mentioned above contains ThreadSanitizer logs that demonstrate this issue. TSan no longer complains with this patch applied, and we've not seen any additional issues from it after deploying it in production over the past week.

Added unit test covering the `nativeProps_DEPRECATED` merge logic in `UIManager::cloneNode` and `ShadowNode::clone`:

```
buck2 test //xplat/js/react-native-github/packages/react-native/ReactCommon/react/renderer/uimanager:tests -- --regex FabricUIManagerTest
```

Reviewed By: zeyap

Differential Revision: D110169424

Pulled By: javache

fbshipit-source-id: 6139253dcc2c33348a0c1a3bd01e695d15aa83bc
Flewp pushed a commit to discord/react-native that referenced this pull request Sep 3, 2026
…#52646)

Summary:
Some third-party libraries, like react-native-reanimated, can clone nodes in a different thread while react-native is calling `setNativeProps_DEPRECATED`. This results in a race condition, where a stale pointer to `nativeProps_DEPRECATED` can be accessed, resulting in a crash. This usually manifests as a `EXC_BAD_ACCESS` crash on iOS. On Android it seems more rare. We've added a lock around accesses to nativeProps_DEPRECATED, but alternative options of fixing this can be considered too.

For more information see software-mansion/react-native-reanimated#7666

[INTERNAL] [FIXED] - Fixed crashes caused by race conditions when third-party libraries clone the shadow dom from a different thread

Pull Request resolved: react#52646

Test Plan:
Due to this being a race condition that only manifests in rare circumstances, it's very difficult to create a reliable reproduction case. The issue mentioned above contains ThreadSanitizer logs that demonstrate this issue. TSan no longer complains with this patch applied, and we've not seen any additional issues from it after deploying it in production over the past week.

Added unit test covering the `nativeProps_DEPRECATED` merge logic in `UIManager::cloneNode` and `ShadowNode::clone`:

```
buck2 test //xplat/js/react-native-github/packages/react-native/ReactCommon/react/renderer/uimanager:tests -- --regex FabricUIManagerTest
```

Reviewed By: zeyap

Differential Revision: D110169424

Pulled By: javache

fbshipit-source-id: 6139253dcc2c33348a0c1a3bd01e695d15aa83bc
(cherry picked from commit 0b941bc)
heavygweit added a commit to officialunofficial/react-native-macos that referenced this pull request Sep 8, 2026
Brings the fork from RN 0.86.0 to 0.86.2. The important change is
upstream react#52646, "Add guards around nativeProps usage to prevent race
conditions": ShadowNodeFamily::nativeProps_DEPRECATED was read by the
main thread's layout commit while the JS thread replaced it from
setNativeProps, which crashed the desktop app during window resizes.
Also picks up react#57244 (display: contents hasNewLayout) and Hermes V1
250829098.0.16.

Conflicts were version-number and lockfile only; no source conflicts.
Published versions become 0.86.2-rc0.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
JasonVMo added a commit to microsoft/fluentui-react-native that referenced this pull request Nov 13, 2026
Minimal backport of MIT-licensed upstream React Native code by OrfeasZ from 923e7ddcaf4d2de45ca7dedafbb84c6b87dbffc4 (react/react-native#52646). Original source copyright and license remain intact.

F16 defect 1's oversized-preview qualification exposed the renderer race in the unchanged PGB-006 NativeSemantics window-resize callback. Both Fabric full lanes lost the app; a clean native rebuild still failed. The owned ASan report fabric-progress-asan-report.86816 records BUS while ShadowNode::clone copies nativeProps_DEPRECATED during AppKit layout. Guard both writers and the cloning reader with the upstream family mutex and preserve props precedence.

Validated after fresh Pods and a clean Fabric native build with ASan retained: forced PGB-006 1/1, run 61d2dce4-5980-49b9-a727-412da47fca71; Apple module and Office-style bridging-header probe 3/3. Evidence: artifacts/desktop/f16-macos-physical/native-props-clean-build-green.log. FluentTester and other native app hosts were not built locally.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
JasonVMo added a commit to microsoft/fluentui-react-native that referenced this pull request Nov 13, 2026
…-macos 0.81.9 (#4499)

* Backport upstream Fabric native-props synchronization

Minimal backport of MIT-licensed upstream React Native code by OrfeasZ from 923e7ddcaf4d2de45ca7dedafbb84c6b87dbffc4 (react/react-native#52646). Original source copyright and license remain intact.

F16 defect 1's oversized-preview qualification exposed the renderer race in the unchanged PGB-006 NativeSemantics window-resize callback. Both Fabric full lanes lost the app; a clean native rebuild still failed. The owned ASan report fabric-progress-asan-report.86816 records BUS while ShadowNode::clone copies nativeProps_DEPRECATED during AppKit layout. Guard both writers and the cloning reader with the upstream family mutex and preserve props precedence.

Validated after fresh Pods and a clean Fabric native build with ASan retained: forced PGB-006 1/1, run 61d2dce4-5980-49b9-a727-412da47fca71; Apple module and Office-style bridging-header probe 3/3. Evidence: artifacts/desktop/f16-macos-physical/native-props-clean-build-green.log. FluentTester and other native app hosts were not built locally.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Record standalone native-props backport qualification

Document the observed failing reader, inferred concurrent writer, complete Fabric regression results, and immutable-install/fresh-native-build evidence for the independent lower branch. No E driver or runtime implementation is present in this layer.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* docs(native-core): keep backport qualification durable

Move session-only run details out of the published reference into ignored private PR evidence. Preserve upstream attribution, lock scope, renderer diagnosis, and the qualified native-host boundary.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
JasonVMo added a commit to microsoft/fluentui-react-native that referenced this pull request Nov 16, 2026
…CI lane (#4500)

* Fix macOS physical-input postconditions and add advisory CI lane

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Select macOS text without menu commands and keep previews scrollable

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Await native pointer placement without replaying physical input

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Backport upstream Fabric native-props synchronization

Minimal backport of MIT-licensed upstream React Native code by OrfeasZ from 923e7ddcaf4d2de45ca7dedafbb84c6b87dbffc4 (react/react-native#52646). Original source copyright and license remain intact.

F16 defect 1's oversized-preview qualification exposed the renderer race in the unchanged PGB-006 NativeSemantics window-resize callback. Both Fabric full lanes lost the app; a clean native rebuild still failed. The owned ASan report fabric-progress-asan-report.86816 records BUS while ShadowNode::clone copies nativeProps_DEPRECATED during AppKit layout. Guard both writers and the cloning reader with the upstream family mutex and preserve props precedence.

Validated after fresh Pods and a clean Fabric native build with ASan retained: forced PGB-006 1/1, run 61d2dce4-5980-49b9-a727-412da47fca71; Apple module and Office-style bridging-header probe 3/3. Evidence: artifacts/desktop/f16-macos-physical/native-props-clean-build-green.log. FluentTester and other native app hosts were not built locally.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Backport upstream Fabric native-props synchronization

Minimal backport of MIT-licensed upstream React Native code by OrfeasZ from 923e7ddcaf4d2de45ca7dedafbb84c6b87dbffc4 (react/react-native#52646). Original source copyright and license remain intact.

F16 defect 1's oversized-preview qualification exposed the renderer race in the unchanged PGB-006 NativeSemantics window-resize callback. Both Fabric full lanes lost the app; a clean native rebuild still failed. The owned ASan report fabric-progress-asan-report.86816 records BUS while ShadowNode::clone copies nativeProps_DEPRECATED during AppKit layout. Guard both writers and the cloning reader with the upstream family mutex and preserve props precedence.

Validated after fresh Pods and a clean Fabric native build with ASan retained: forced PGB-006 1/1, run 61d2dce4-5980-49b9-a727-412da47fca71; Apple module and Office-style bridging-header probe 3/3. Evidence: artifacts/desktop/f16-macos-physical/native-props-clean-build-green.log. FluentTester and other native app hosts were not built locally.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Record standalone native-props backport qualification

Document the observed failing reader, inferred concurrent writer, complete Fabric regression results, and immutable-install/fresh-native-build evidence for the independent lower branch. No E driver or runtime implementation is present in this layer.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Record macOS physical-input and stacked backport qualification

Part of #4441 and #4435. Record both renderers full physical/accessibility passes, real MCP receipts, strict keyboard clear, unchanged callbacks, final native contracts, standalone lower-layer evidence, and serial-mode aggregate acceptance. Preserve earlier failures and unqualified host/occlusion limits; no source assertions or deadlines were relaxed.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* docs(native-core): keep backport qualification durable

Move session-only run details out of the published reference into ignored private PR evidence. Preserve upstream attribution, lock scope, renderer diagnosis, and the qualified native-host boundary.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix(storybook): scope the scroll viewport to macOS

Restore the shared Windows decorator's original non-scrolling layout. Keep the overflow repair in a macOS platform entrypoint and add explicit macOS/Windows viewport regressions.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix(desktop-driver): refuse same-process covering windows before activation

Reserve foreign hit identities for proven different-process owners. Same-process and unproven owners retain the target's no-activation obstruction scope, preventing popup dismissal or window reordering. Add failing native owner regressions and an agent effect-none/no-input check; label the prior serial buildci run diagnostic only.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* docs(desktop-driver): record reviewed native requalification

Record the macOS-only viewport and no-activation occlusion regressions, signed helper qualification, both real-descriptor reproductions, and complete physical/access lanes without promoting the serial aggregate diagnostic to PR-gate acceptance.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* docs(desktop-driver): record plain-gate baseline comparison

Clarify the process-owner hit-test policy and retain both untouched-main comparison results without treating serial diagnostics as gate acceptance.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix(storybook): size macOS physical runs inside visible screen bounds

Prepare a verified 1024x768 window through the existing driver window-rect API before physical traversal or attached callbacks. Clamp its origin to a fitting visible screen, fail if the display or host cannot accommodate it, and preserve accessible/compatibility geometry and native occlusion checks.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Center macOS physical preset inside visible screen bounds

A real off-screen-window qualification showed the boundary-aligned resize being adjusted to 1024x767. Center the fixed preset within the fitting visible screen, preserving strict geometry verification and physical-only scope.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Record hosted physical-window qualification and console limit

Retain the complete Fabric physical and forced-small-window passes, Paper native-build success and locked-console qualification gap, and the unchanged WDIO deadline failure in the plain aggregate. No hosted success or Paper GUI pass is inferred.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Resolve environment-selected smoke lane before window setup

Reuse the existing wdio input resolver for traversal so DESKTOP_INPUT=physical receives the same macOS window preparation as an explicit flag. Regress the env-only path and preserve explicit accessible precedence; leave geometry policy and native assertions unchanged.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Record portable qualification of environment-selected smoke sizing

Keep prior explicit-input Fabric native evidence attributed to cc96a3a, record the red/green env-only regression and owning-package results, and preserve the known plain-aggregate fixture failure and locked-console native gap.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Clamp physical Storybook window to visible screen

Co-authored with the Copilot CLI.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix(desktop-driver): omit WebDriver error stacks

Co-authored-by: JasonVMo <35609178+JasonVMo@users.noreply.github.com>

* docs(desktop-driver): format merged status ledger

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
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. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants