From 444c7d4eff3d4fbe25452c94cba7ffacb3c366cc Mon Sep 17 00:00:00 2001 From: Riccardo Cipolleschi Date: Tue, 11 Feb 2025 05:54:39 -0800 Subject: [PATCH] Allow multiple RN instances to run at the same time (#49300) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/49300 With the refactor of the AppDelegate in favor ReactNativeFactory, the users can now instantiate multiple instances of react native. However, currently, if you try to run multiple instances, the app will crash with the message: ``` libc++abi: terminating due to uncaught exception of type std::runtime_error: Feature flags cannot be overridden more than once ``` This happens also when the feature flags we would like to set are the same that we already applied. This should be an allowed scenario because reapplying the excatly same features flags should have no effect on React native and that's not the use case we want to forbid. With this change, we are creating a static variable that checks whether we already apply that set of feature flags and it allows you to create multiple instances by keeping the same flags ## Changelog: [iOS][Fixed] - Allow multiple RN instances to run at the same time Reviewed By: rubennorte Differential Revision: D69398441 fbshipit-source-id: a377c6a1402d38d66d348fa8c6a65e645973aadc --- .../Libraries/AppDelegate/RCTReactNativeFactory.mm | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/packages/react-native/Libraries/AppDelegate/RCTReactNativeFactory.mm b/packages/react-native/Libraries/AppDelegate/RCTReactNativeFactory.mm index 0f9f876adc8..4f202329807 100644 --- a/packages/react-native/Libraries/AppDelegate/RCTReactNativeFactory.mm +++ b/packages/react-native/Libraries/AppDelegate/RCTReactNativeFactory.mm @@ -274,9 +274,12 @@ class RCTAppDelegateBridgelessFeatureFlags : public ReactNativeFeatureFlagsDefau - (void)_setUpFeatureFlags { - if ([self bridgelessEnabled]) { - ReactNativeFeatureFlags::override(std::make_unique()); - } + static dispatch_once_t setupFeatureFlagsToken; + dispatch_once(&setupFeatureFlagsToken, ^{ + if ([self bridgelessEnabled]) { + ReactNativeFeatureFlags::override(std::make_unique()); + } + }); } @end