Skip to content

Commit 814e1eb

Browse files
sammy-SCfacebook-github-bot
authored andcommitted
avoid sync dispatch with locked mutex in RCTKeyWindowValuesProxy (facebook#49605)
Summary: Pull Request resolved: facebook#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
1 parent a003be0 commit 814e1eb

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

packages/react-native/React/Base/UIKitProxies/RCTKeyWindowValuesProxy.mm

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,28 @@ - (instancetype)init
4444

4545
- (void)startObservingWindowSizeIfNecessary
4646
{
47-
std::lock_guard<std::mutex> lock(_mutex);
48-
if (!_isObserving) {
47+
// Accesing _isObserving must be done under the lock to avoid a race condition.
48+
// We can't hold the lock while calling RCTUnsafeExecuteOnMainQueueSync.
49+
// Therefore, reading/writing _isObserving is kept separate from calling RCTUnsafeExecuteOnMainQueueSync.
50+
{
51+
std::lock_guard<std::mutex> lock(_mutex);
52+
if (_isObserving) {
53+
return;
54+
}
4955
_isObserving = YES;
50-
// For backwards compatibility, we register for notifications from the main thread only.
51-
// On the new architecture, we are already on the main thread and RCTUnsafeExecuteOnMainQueueSync will simply call
52-
// the block.
53-
RCTUnsafeExecuteOnMainQueueSync(^{
54-
[RCTKeyWindow() addObserver:self forKeyPath:kFrameKeyPath options:NSKeyValueObservingOptionNew context:nil];
55-
});
56-
57-
[[NSNotificationCenter defaultCenter] addObserver:self
58-
selector:@selector(_interfaceOrientationDidChange)
59-
name:UIApplicationDidBecomeActiveNotification
60-
object:nil];
6156
}
57+
58+
// For backwards compatibility, we register for notifications from the main thread only.
59+
// On the new architecture, we are already on the main thread and RCTUnsafeExecuteOnMainQueueSync will simply call
60+
// the block.
61+
RCTUnsafeExecuteOnMainQueueSync(^{
62+
[RCTKeyWindow() addObserver:self forKeyPath:kFrameKeyPath options:NSKeyValueObservingOptionNew context:nil];
63+
});
64+
65+
[[NSNotificationCenter defaultCenter] addObserver:self
66+
selector:@selector(_interfaceOrientationDidChange)
67+
name:UIApplicationDidBecomeActiveNotification
68+
object:nil];
6269
}
6370

6471
- (void)observeValueForKeyPath:(NSString *)keyPath

0 commit comments

Comments
 (0)