-
Notifications
You must be signed in to change notification settings - Fork 177
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
Add synchronized block to protect against race condition #69
Closed
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
alsutton
added a commit
to alsutton/fresco
that referenced
this pull request
Jan 20, 2021
…nitialized, and when we try to perform .init() which means a race condition can occur (as mentioned in facebook/SoLoader#60). This change uses synchronization on the NativeLoader class, and a secondary check to verify that the initialization needs to take place just before we perform it, in order to reduce the window for a race condition to affect client code. When combined with facebook/SoLoader#69 this will remove the race condition from the code because the initialisation inside SoLoader and inside Fresco will both be synchronized on the NativeLoad.class object.
4 tasks
Thanks for the PR! |
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.
@wizh has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
facebook-github-bot
pushed a commit
to facebook/fresco
that referenced
this pull request
Jan 22, 2021
…de (#2579) Summary: Thanks for submitting a PR! Please read these instructions carefully: - [X] Explain the **motivation** for making this change. - [X] Provide a **test plan** demonstrating that the code is solid. - [X] Match the **code formatting** of the rest of the codebase. - [X] Target the `master` branch ## Motivation (required) There is a period of time between checking if NativeLoader has been initialized, and when we try to perform `.init()` which means a race condition can occur (as mentioned in facebook/SoLoader#60). This PR provides a mechanism for client code to avoid that race condition. This change uses synchronization on the `NativeLoader` class object, and a secondary check to verify that the initialization needs to take place just before we perform it in order to reduce the window in which a race condition can happen. When combined with facebook/SoLoader#69 this will remove the race condition from the code because the initialisation inside SoLoader and inside Fresco will both be synchronized on the `NativeLoader.class` object. Without that PR in SoLoader this change will still allow fresco clients to synchronize on `NativeLoader.class` in their own code around any areas which may race with this code in fresco. ## Test Plan (required) This code doesn't alter the functionality of fresco, and so existing tests should provide verification that there is no operational impact. ## Next Steps Sign the [CLA][2], if you haven't already. Small pull requests are much easier to review and more likely to get merged. Make sure the PR does only one thing, otherwise please split it. Make sure all **tests pass** on [Circle CI][4]. PRs that break tests are unlikely to be merged. For more info, see the [Contributing guide][4]. [1]: https://medium.com/martinkonicek/what-is-a-test-plan-8bfc840ec171#.y9lcuqqi9 [2]: https://code.facebook.com/cla [3]: http://circleci.com/gh/facebook/fresco [4]: /~https://github.com/facebook/fresco/blob/master/CONTRIBUTING.md Pull Request resolved: #2579 Reviewed By: oprisnik Differential Revision: D25973346 Pulled By: wizh fbshipit-source-id: 10239f7359e45ffc5deddec86d5798a838fc4bc5
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes: #60
As mentioned in issue #60 if the init method is called twice from two different threads it can result in a race condition where both threads receive false for
NativeLoader.isInitialized()
, but, whenNativeLoader.init()
is run for a second time it throws an exception.Adding a synchronized block here ensures that only one thread will be able to query the initialisation state and trigger the initialisation at a time.