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

Conversation

zhongwuzw
Copy link
Contributor

Summary

Add lock for observers when do enumeration.
cc. @shergin .

Changelog

[iOS] [Fixed] - Add lock for surface presenter observers

Test Plan

N/A.

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Mar 20, 2019
@@ -331,7 +331,10 @@ - (void)mountingManager:(RCTMountingManager *)mountingManager willMountComponent
{
RCTAssertMainQueue();

for (id<RCTSurfacePresenterObserver> observer in _observers) {
std::unique_lock<std::mutex> lock(_observerListMutex);
Copy link
Contributor

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).

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, but seems may not give some benefits? 🤔 because reader seems always from main queue which mean only one reader. 😂

@shergin
Copy link
Contributor

shergin commented Mar 20, 2019

The concert is totally valid. We must fix it.

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!

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a 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.

@react-native-bot
Copy link
Collaborator

This pull request was successfully merged by @zhongwuzw in 946f1a6.

When will my fix make it into a release? | Upcoming Releases

@react-native-bot react-native-bot added the Merged This PR has been merged. label Mar 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged This PR has been merged.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants