avoid sync dispatch with locked mutex in RCTKeyWindowValuesProxy (#49605)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/49605

changelog: [internal]

Calling `RCTUnsafeExecuteOnMainQueueSync` while holding a lock can lead to a deadlock. In this diff, we remove it from RCTKeyWindowValuesProxy.

Reviewed By: javache

Differential Revision: D69997888

fbshipit-source-id: a09fc641c9fb2aec59aef34e4047e1ef11cdaf02
This commit is contained in:
Samuel Susla
2025-02-21 16:41:30 -08:00
committed by Facebook GitHub Bot
parent a003be0f23
commit 814e1ebff6
@@ -44,21 +44,28 @@ static NSString *const kFrameKeyPath = @"frame";
- (void)startObservingWindowSizeIfNecessary
{
std::lock_guard<std::mutex> lock(_mutex);
if (!_isObserving) {
// Accesing _isObserving must be done under the lock to avoid a race condition.
// We can't hold the lock while calling RCTUnsafeExecuteOnMainQueueSync.
// Therefore, reading/writing _isObserving is kept separate from calling RCTUnsafeExecuteOnMainQueueSync.
{
std::lock_guard<std::mutex> lock(_mutex);
if (_isObserving) {
return;
}
_isObserving = YES;
// For backwards compatibility, we register for notifications from the main thread only.
// On the new architecture, we are already on the main thread and RCTUnsafeExecuteOnMainQueueSync will simply call
// the block.
RCTUnsafeExecuteOnMainQueueSync(^{
[RCTKeyWindow() addObserver:self forKeyPath:kFrameKeyPath options:NSKeyValueObservingOptionNew context:nil];
});
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(_interfaceOrientationDidChange)
name:UIApplicationDidBecomeActiveNotification
object:nil];
}
// For backwards compatibility, we register for notifications from the main thread only.
// On the new architecture, we are already on the main thread and RCTUnsafeExecuteOnMainQueueSync will simply call
// the block.
RCTUnsafeExecuteOnMainQueueSync(^{
[RCTKeyWindow() addObserver:self forKeyPath:kFrameKeyPath options:NSKeyValueObservingOptionNew context:nil];
});
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(_interfaceOrientationDidChange)
name:UIApplicationDidBecomeActiveNotification
object:nil];
}
- (void)observeValueForKeyPath:(NSString *)keyPath