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

Lock-free LocationManager instantiation #352

Merged
merged 1 commit into from
Nov 8, 2022
Merged

Conversation

OdNairy
Copy link
Collaborator

@OdNairy OdNairy commented Nov 1, 2022

This PR address the deadlock issue.
Sometimes access to MMELocationManager/locationManager happens in the middle of the original locationManager getter. For example, code with access to locationManager can be scheduled for the next main RunLoop run, and then the getter would schedule its own internal initialization to the main queue and keep holding the @synchronized lock. And on the next RunLoop run when an arbitrary access code would be called that lock would stop the main queue execution without a way to break the deadlock.
Some extra context in the internal discussion.

Fixes https://mapbox.atlassian.net/browse/CORESDK-1440

Related:

@OdNairy OdNairy requested review from tarigo, evil159 and S2Ler November 1, 2022 13:10
// since the access to locationManager might happen in
// main thread after this function call but before
// mainQueue async.
dispatch_sync(dispatch_get_main_queue(), ^{
Copy link
Contributor

Choose a reason for hiding this comment

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

theoretically this can deadlock as well. If you sync on background thread from the main one while background thread will call this method.

Not sure if it is possible scenario with this lib though.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

hm, you are right. Any ideas on how to fix that? 🙂

Copy link
Contributor

Choose a reason for hiding this comment

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

this approach seem to work, take a look
Deadlock.zip

Copy link
Contributor

Choose a reason for hiding this comment

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

just not sure about run loop. it is not guaranteed to be available.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think it is acceptable risk. My point is that sync call from main thread to the background queue in not something common and should be generally avoided as a bad practice leading to the UI lags.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks for providing a workaround. It seems to have some overhead so I will avoid it at the moment. In case of any issues reports I will reconsider my position.

@OdNairy OdNairy requested a review from sarochych November 2, 2022 14:25
@OdNairy OdNairy merged commit 10e14a2 into main Nov 8, 2022
@OdNairy OdNairy deleted the bugfix/deadlock-on-main branch November 8, 2022 13:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants