From ab2c47be285fde99a77d189fd04220ef8ad0933a Mon Sep 17 00:00:00 2001 From: Riccardo Cipolleschi Date: Fri, 4 Oct 2024 13:23:20 -0700 Subject: [PATCH] Fix deadlock when a new surface is getting started while another is stopped (#46841) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/46841 Some apps are crashing because surfaces are started and stopped concurrently and this can create a deadlock. This is an attempt to disentangle the deadlock by not moving synchronously to the main queue when starting a surface ## Changelog [iOS][Changed] - Do not move to the main queue synchronously when starting a new surface Reviewed By: fkgozali Differential Revision: D63899469 fbshipit-source-id: 98cb313ebd610c65f6b06a683912856a00102e44 --- .../React/Fabric/Surface/RCTFabricSurface.mm | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/packages/react-native/React/Fabric/Surface/RCTFabricSurface.mm b/packages/react-native/React/Fabric/Surface/RCTFabricSurface.mm index 0567a52fd89..7b3ce5918ce 100644 --- a/packages/react-native/React/Fabric/Surface/RCTFabricSurface.mm +++ b/packages/react-native/React/Fabric/Surface/RCTFabricSurface.mm @@ -99,15 +99,16 @@ using namespace facebook::react; // We need to register a root view component here synchronously because right after // we start a surface, it can initiate an update that can query the root component. - RCTUnsafeExecuteOnMainQueueSync(^{ + RCTExecuteOnMainQueue(^{ [self->_surfacePresenter.mountingManager attachSurfaceToView:self.view surfaceId:self->_surfaceHandler->getSurfaceId()]; + dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INTERACTIVE, 0), ^{ + self->_surfaceHandler->start(); + [self _propagateStageChange]; + + [self->_surfacePresenter setupAnimationDriverWithSurfaceHandler:*self->_surfaceHandler]; + }); }); - - _surfaceHandler->start(); - [self _propagateStageChange]; - - [_surfacePresenter setupAnimationDriverWithSurfaceHandler:*_surfaceHandler]; } - (void)stop