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

Add synchronized block to protect against race condition #69

Closed

Conversation

alsutton
Copy link
Contributor

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, when NativeLoader.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.

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.
@wizh
Copy link

wizh commented Jan 20, 2021

Thanks for the PR!

Copy link

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

@wizh has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@facebook-github-bot
Copy link

@wizh merged this pull request in 17b6163.

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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Race condition in initializer
3 participants