From 2afcc82efc326e4fb0af6250230ade124bd0c4e6 Mon Sep 17 00:00:00 2001 From: Adeel Ejaz Date: Mon, 23 Nov 2026 19:04:25 +0000 Subject: [PATCH] [iOS] Fix data race in RCTComponentViewFactory fallback registration --- .../Mounting/RCTComponentViewFactory.mm | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/packages/react-native/React/Fabric/Mounting/RCTComponentViewFactory.mm b/packages/react-native/React/Fabric/Mounting/RCTComponentViewFactory.mm index 47bd98c12a5b..650af5887cc5 100644 --- a/packages/react-native/React/Fabric/Mounting/RCTComponentViewFactory.mm +++ b/packages/react-native/React/Fabric/Mounting/RCTComponentViewFactory.mm @@ -131,14 +131,18 @@ - (RCTComponentViewClassDescriptor)_componentViewClassDescriptorFromClass:(Class - (BOOL)_wasComponentRegistered:(const std::string &)name { + std::shared_lock lock(_mutex); auto registrationResult = _registrationStatusMap.find(name); return registrationResult != _registrationStatusMap.end() && (registrationResult->second); } - (void)_registerComponentIfPossible:(const std::string &)name { - if (_registrationStatusMap.find(name) != _registrationStatusMap.end()) { - return; + { + std::shared_lock lock(_mutex); + if (_registrationStatusMap.find(name) != _registrationStatusMap.end()) { + return; + } } // Fallback 1: Call provider function for component view class. @@ -177,9 +181,13 @@ - (void)_registerComponentIfPossible:(const std::string &)name auto provider = ComponentDescriptorProvider{ .handle = componentHandle, .name = componentName, .flavor = flavor, .constructor = constructor}; + // Only the map writes take the lock: building a legacy interop descriptor can wait on the + // main queue (a view manager that requires main queue setup), which may itself be waiting + // for this lock in -createComponentViewWithComponentHandle:. _providerRegistry.add(provider); - _componentViewClasses[componentHandle] = - [self _componentViewClassDescriptorFromClass:[RCTLegacyViewManagerInteropComponentView class]]; + auto classDescriptor = [self _componentViewClassDescriptorFromClass:[RCTLegacyViewManagerInteropComponentView class]]; + std::unique_lock lock(_mutex); + _componentViewClasses[componentHandle] = classDescriptor; _registrationStatusMap.insert({provider.name, true}); return; } @@ -194,8 +202,9 @@ - (void)_registerComponentIfPossible:(const std::string &)name .handle = componentHandle, .name = componentName, .flavor = flavor, .constructor = constructor}; _providerRegistry.add(provider); - _componentViewClasses[componentHandle] = - [self _componentViewClassDescriptorFromClass:[RCTUnimplementedViewComponentView class]]; + auto classDescriptor = [self _componentViewClassDescriptorFromClass:[RCTUnimplementedViewComponentView class]]; + std::unique_lock lock(_mutex); + _componentViewClasses[componentHandle] = classDescriptor; _registrationStatusMap.insert({provider.name, false}); }