Skip to content

[iOS][Fabric] Fix data race in RCTComponentViewFactory legacy-interop/Unimplemented fallbacks - #60243

Open
adeelejaz wants to merge 1 commit into
react:mainfrom
adeelejaz:fix-component-view-factory-race
Open

adeelejaz wants to merge 1 commit into
react:mainfrom
adeelejaz:fix-component-view-factory-race

Conversation

@adeelejaz

@adeelejaz adeelejaz commented Nov 23, 2026 •

Copy link
Copy Markdown

Summary:

-[RCTComponentViewFactory _registerComponentIfPossible:] runs on the JS thread, from the ComponentDescriptorProviderRegistry request callback. Its legacy-interop fallback and its UnimplementedView fallback write _componentViewClasses and _registrationStatusMap without taking _mutex. Its early _registrationStatusMap.find and -_wasComponentRegistered: also read without the lock.

On the main thread, -createComponentViewWithComponentHandle: takes only std::shared_lock lock(_mutex) before it calls _componentViewClasses.find(handle). So an unlocked insert, particularly one that rehashes the map, can make that lookup miss a handle registered long ago. In practice the missed handle is often the built-in Paragraph:

Any app that renders a component through the legacy interop layer is exposed: a library without Fabric codegen, RNCMaskedView, or any host name that resolves to UnimplementedView. The first render of each such name is a writer.

This PR takes the factory lock only around the map reads and writes:

  • std::shared_lock in -_wasComponentRegistered: and around the early status-map check.
  • In both fallbacks, the provider and class descriptor are built and _providerRegistry.add(provider) is called with no factory lock held. Then std::unique_lock is taken for the two map writes only.

The ordering is safe. _registerComponentIfPossible: runs synchronously inside ComponentDescriptorRegistry::at on the JS thread, and returns only after the writes, so the new name cannot be used before the maps hold it. Upstream already adds the provider before writing the maps.

The lock deliberately does not span _providerRegistry.add, unlike the fix proposed in #58299. Building a LegacyViewManagerInteropComponentDescriptor can lazily load a view manager that requires main-queue setup (RCTUnsafeExecuteOnMainQueueSync; see #53128). Holding the factory lock across that would deadlock against a main thread waiting for the lock in -createComponentViewWithComponentHandle:. With this change, nothing but map operations runs under the lock.

Fixes #60244 (re-files #58299) (closed by the bot for lack of a reproducer; the race is still present on main).

Changelog:

[IOS] [FIXED] - Fix a data race in RCTComponentViewFactory where legacy-interop and Unimplemented component registration wrote the component registry without its lock, which could crash view creation ("ComponentView … (Paragraph) not found").

Test Plan:

ThreadSanitizer, before and after. Setup:

  • React Native built from source for the iOS simulator, iOS 27.2 runtime, Xcode 27.2.
  • -fsanitize=thread added as a per-file compiler flag on RCTComponentViewFactory.mm, plus the link flag.
  • A dev-only stress screen in a bridgeless app:
    • mounts a batch of 1,500 <Text> on alternate frames, more than the 1,024-view recycle pool, so the main thread keeps creating views;
    • right after each batch commits, on the JS thread, creates detached shadow nodes under host names never seen before (nativeFabricUIManager.createNode(tag, "Probe" + n, rootTag, {}, {})). Each one forces an UnimplementedView fallback registration.

Without this change, TSan reports:

WARNING: ThreadSanitizer: data race
  Read of size 8 by main thread:
    #0-#1 std::unordered_map<long long, RCTComponentViewClassDescriptor>::find
    #2 -[RCTComponentViewFactory createComponentViewWithComponentHandle:]
    #3 -[RCTComponentViewRegistry _dequeueComponentViewWithComponentHandle:]
  Previous write of size 8 by thread T15:
    #0-#3 std::unordered_map<…RCTComponentViewClassDescriptor…> insert
    #4 -[RCTComponentViewFactory _registerComponentIfPossible:]   (UnimplementedView fallback)
    … ComponentDescriptorProviderRegistry request callback

With this change, the same build, harness and run report no data race.

Also run: the app's full Jest suite and its iOS end-to-end flows on the patched source build.

The stress harness drives only the UnimplementedView fallback, because fresh legacy view-manager names can't be minted at runtime. The legacy-interop fallback gets the same lock shape, and is covered by code reading rather than by a TSan run.

Also unlocked but not changed here: +[RCTLegacyViewManagerInteropComponentView isSupported:] writes a static NSMutableDictionary. Its only caller is the legacy-interop fallback, on the JS thread.

@meta-cla

meta-cla Bot commented Nov 23, 2026

Copy link
Copy Markdown

Hi @adeelejaz!

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 commented Nov 23, 2026

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!

@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 Nov 23, 2026
@facebook-github-tools facebook-github-tools Bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Nov 23, 2026

This branch has not been deployed

No deployments
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. 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.

[iOS][Fabric] Data race in RCTComponentViewFactory: legacy-interop/Unimplemented fallbacks write the registry without _mutex (re-files #58299)

1 participant