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

Core Data: Make persistent container thread-safe #14534

Merged
merged 11 commits into from
Nov 29, 2024

Conversation

itsmeichigo
Copy link
Contributor

@itsmeichigo itsmeichigo commented Nov 27, 2024

Closes: #14533

Description

When checking an issue with screenshot testing [p1732623939631369-slack-C03L1NF1EA3], @hichamboushaba found that the way we instantiate persistentContainer is not thread-safe as we can create it when accessing both viewStorage and writerDerivedStorage, which are in different threads.

This PR updates CoreDataManager to be more thread-safe by making all lazy variables constants.

Steps to reproduce

It's not easy to reproduce the race condition of persistent container. The most straightforward way is to run the screenshot test and verify that it is consistently able to display the stats on the dashboard.

Additionally, please do some quick smoke tests on the critical flows: Dashboard, Orders, Products, and Payments to ensure everything works as expected. You can follow the testing plan in pe5sF9-3e2-p2 as a guidance.

Testing information

Tested on simulator iPhone 16 Pro iOS 18 and confirmed that:

  • The screenshot test now works correctly and consistently.
  • Smoked testing features following the testing plan pe5sF9-3e2-p2 and confirmed that everything works as before (payment features were skipped, would appreciate your help here @jaclync @staskus 🙏)

Screenshots

N/A


  • I have considered if this change warrants user-facing release notes and have added them to RELEASE-NOTES.txt if necessary.

Reviewer (or Author, in the case of optional code reviews):

Please make sure these conditions are met before approving the PR, or request changes if the PR needs improvement:

  • The PR is small and has a clear, single focus, or a valid explanation is provided in the description. If needed, please request to split it into smaller PRs.
  • Ensure Adequate Unit Test Coverage: The changes are reasonably covered by unit tests or an explanation is provided in the PR description.
  • Manual Testing: The author listed all the tests they ran, including smoke tests when needed (e.g., for refactorings). The reviewer confirmed that the PR works as expected on all devices (phone/tablet) and no regressions are added.

@itsmeichigo itsmeichigo added type: bug A confirmed bug. category: architecture Related to architecture such as the database, FluxC, Networking, Core Data, etc. feature: core Core work. See "category: tooling" and "category: architecture" labels Nov 27, 2024
@wpmobilebot
Copy link
Collaborator

wpmobilebot commented Nov 27, 2024

WooCommerce iOS📲 You can test the changes from this Pull Request in WooCommerce iOS by scanning the QR code below to install the corresponding build.

App NameWooCommerce iOS WooCommerce iOS
Build Numberpr14534-4ba484c
Version21.2
Bundle IDcom.automattic.alpha.woocommerce
Commit4ba484c
App Center BuildWooCommerce - Prototype Builds #11859
Automatticians: You can use our internal self-serve MC tool to give yourself access to App Center if needed.

@itsmeichigo itsmeichigo added this to the 21.3 milestone Nov 28, 2024
@itsmeichigo itsmeichigo marked this pull request as ready for review November 28, 2024 02:51
@itsmeichigo itsmeichigo marked this pull request as draft November 28, 2024 02:54
@itsmeichigo itsmeichigo marked this pull request as ready for review November 28, 2024 06:59
@staskus staskus self-assigned this Nov 28, 2024
Copy link
Collaborator

@staskus staskus left a comment

Choose a reason for hiding this comment

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

Thank you for the improvement! I haven't found flaws in the code logic. As I understand, the only downside we could theoretically expect are longer load times for the app, since CoreDataManager will be loaded all at once together with the persistent container.

We can observe in the subsequent releases if there are any dramatic changes in average (50th percentile) and longest (90th percentile) times in Xcode Organizer.

Launch time Wooo
  • I reproduced the issue with WooScreenshots not loading the stats data, and I can confirm that these changes help avoid the issue
  • I tried stress testing the solution with some concurrent local unit tests and they succeeded
  • I also performed smoke testing for login, logout, dashboard, orders, products, payments.

I haven't reproduced any new issues.


Do you remember why we had a lazy persistent container in the first place? Were there any scenarios that we tried to cover and now we need to make sure they continue working as expected?

@itsmeichigo
Copy link
Contributor Author

Thanks @staskus for the thorough reviews!

Do you remember why we had a lazy persistent container in the first place? Were there any scenarios that we tried to cover and now we need to make sure they continue working as expected?

Honestly this code exists a few years before I joined, and I could not find any documentation about why the Core Data stack was built that way. My best guess is to optimize app start time as you pointed out.

@itsmeichigo itsmeichigo merged commit 734dfa5 into trunk Nov 29, 2024
14 checks passed
@itsmeichigo itsmeichigo deleted the fix/14533-threadsafe-persistent-container branch November 29, 2024 11:06
Copy link

sentry-io bot commented Dec 6, 2024

Suspect Issues

This pull request was deployed and Sentry observed the following issues:

  • ‼️ Error Domain=NSPOSIXErrorDomain Code=28 "No space left on device" `` View Issue

Did you find this useful? React with a 👍 or 👎

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
category: architecture Related to architecture such as the database, FluxC, Networking, Core Data, etc. feature: core Core work. See "category: tooling" and "category: architecture" type: bug A confirmed bug.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make CoreDataManager thread-safe
3 participants