Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fabric] Add lock for surface presenter observers #24052

Closed
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions React/Fabric/RCTSurfacePresenter.mm
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ @implementation RCTSurfacePresenter {
RCTBridge *_bridge; // Unsafe. We are moving away from Bridge.
RCTBridge *_batchedBridge;
std::shared_ptr<const ReactNativeConfig> _reactNativeConfig;
std::mutex _observerListMutex;
better::shared_mutex _observerListMutex;
NSMutableArray<id<RCTSurfacePresenterObserver>> *_observers;
}

Expand Down Expand Up @@ -315,13 +315,13 @@ - (void)schedulerOptimisticallyCreateComponentViewWithComponentHandle:(Component

- (void)addObserver:(id<RCTSurfacePresenterObserver>)observer
{
std::lock_guard<std::mutex> lock(_observerListMutex);
std::unique_lock<better::shared_mutex> lock(_observerListMutex);
[self->_observers addObject:observer];
}

- (void)removeObserver:(id<RCTSurfacePresenterObserver>)observer
{
std::lock_guard<std::mutex> lock(_observerListMutex);
std::unique_lock<better::shared_mutex> lock(_observerListMutex);
[self->_observers removeObject:observer];
}

Expand All @@ -331,7 +331,12 @@ - (void)mountingManager:(RCTMountingManager *)mountingManager willMountComponent
{
RCTAssertMainQueue();

for (id<RCTSurfacePresenterObserver> observer in _observers) {
NSArray<id<RCTSurfacePresenterObserver>> *observers;
{
std::shared_lock<better::shared_mutex> lock(_observerListMutex);
observers = [_observers copy];
}
for (id<RCTSurfacePresenterObserver> observer in observers) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not iterate over _observers while the lock is acquired?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing to watch out for is deadlock if any of the observer delegate methods can add/remove observers unless better::shared_mutex is like recursive_mutex.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shergin What I concerned is the same as @ide , we have no control what delegate would do. But if this deadlock situation shouldn't happened. I can remove copy.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

willMountComponentsWithRootTag: is being called quite often and perf sensitive, so please do.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

if ([observer respondsToSelector:@selector(willMountComponentsWithRootTag:)]) {
[observer willMountComponentsWithRootTag:rootTag];
}
Expand All @@ -351,7 +356,12 @@ - (void)mountingManager:(RCTMountingManager *)mountingManager didMountComponents
surface.view.rootView = (RCTSurfaceRootView *)rootComponentView;
}
}
for (id<RCTSurfacePresenterObserver> observer in _observers) {
NSArray<id<RCTSurfacePresenterObserver>> *observers;
{
std::shared_lock<better::shared_mutex> lock(_observerListMutex);
observers = [_observers copy];
}
for (id<RCTSurfacePresenterObserver> observer in observers) {
if ([observer respondsToSelector:@selector(didMountComponentsWithRootTag:)]) {
[observer didMountComponentsWithRootTag:rootTag];
}
Expand Down