-
Notifications
You must be signed in to change notification settings - Fork 24.3k
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
[Fabric] Add lock for surface presenter observers #24052
Conversation
React/Fabric/RCTSurfacePresenter.mm
Outdated
@@ -331,7 +331,10 @@ - (void)mountingManager:(RCTMountingManager *)mountingManager willMountComponent | |||
{ | |||
RCTAssertMainQueue(); | |||
|
|||
for (id<RCTSurfacePresenterObserver> observer in _observers) { | |||
std::unique_lock<std::mutex> lock(_observerListMutex); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have to use { ... }
blocks to designate the scope covered by mutex. We must not call .unlock
manually.
We also have to use better::shared_mutex and std::shared_lock
here (and std::unique_lock
where we modify the collection).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, but seems may not give some benefits? 🤔 because reader seems always from main queue which mean only one reader. 😂
The concert is totally valid. We must fix it. |
React/Fabric/RCTSurfacePresenter.mm
Outdated
std::shared_lock<better::shared_mutex> lock(_observerListMutex); | ||
observers = [_observers copy]; | ||
} | ||
for (id<RCTSurfacePresenterObserver> observer in observers) { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@shergin is landing this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
This pull request was successfully merged by @zhongwuzw in 946f1a6. When will my fix make it into a release? | Upcoming Releases |
Summary
Add lock for observers when do enumeration.
cc. @shergin .
Changelog
[iOS] [Fixed] - Add lock for surface presenter observers
Test Plan
N/A.