From 7b088fa0b1d45d371fab9cd3c85e49c1fb968695 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 75c963a3a99..8c7d51fd81f 100644 --- a/packages/react-native/Libraries/AppDelegate/RCTReactNativeFactory.mm +++ b/packages/react-native/Libraries/AppDelegate/RCTReactNativeFactory.mm @@ -255,9 +255,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