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 to help prevent race condition with fresco client code #2579

Closed
wants to merge 1 commit into from

Conversation

alsutton
Copy link
Contributor

@alsutton alsutton commented Jan 20, 2021

Thanks for submitting a PR! Please read these instructions carefully:

  • Explain the motivation for making this change.
  • Provide a test plan demonstrating that the code is solid.
  • Match the code formatting of the rest of the codebase.
  • 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, 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. PRs that break tests are unlikely to be merged.

For more info, see the Contributing guide.

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

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

@wizh
Copy link
Contributor

wizh commented Jan 20, 2021

Thanks for the PR!

@alsutton
Copy link
Contributor Author

No problem. If you can share details from intern phab about what the internal problems are I'd be happy to do a cleanup commit.

@facebook-github-bot
Copy link
Contributor

@wizh merged this pull request in 00c035e.

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.

3 participants