fix: require transactional query context for createOrGetExistingAsync utility method#628
Draft
wschurman wants to merge 1 commit into
Conversation
Member
Author
This stack of pull requests is managed by Graphite. Learn more about stacking. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #628 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 109 109
Lines 17743 17733 -10
Branches 1539 1522 -17
=========================================
- Hits 17743 17733 -10
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
This file contains hidden or 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
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.

Why
One class of issue in entity is concurrent calls to load/create methods caching stale data in dataloaders/redis when done outside a transaction (otherwise dataloaders are scoped to transaction and cache adapter is skipped). This can happen when multiple calls to blocks of code like
createOrGetExistingAsynchappen in parallel without an explicit transactional query context, within which it would negatively cache in one call and then incorrectly retrieve that negative cache from another call depending on race condition ordering.Concretely, one such issue with
createOrGetExistingAsyncis reproducible by calling it with the same set of args:This will throw due to the ordering: both calls will miss initially, both will create, one will hit unique constraint error (handled), that one will try to re-load, but will see the negatively-cached result in dataloader.
How
While the class of bug can happen with really any non-transactional sequence of loads/mutations, entity as a library should do its best to prevent it from occurring in blessed utility methods. The only way to make this deterministic is via requiring a transactional query context be supplied to
createOrGetExistingAsync.Reasoning for why this is likely the right call path. Most creations in applications should already be within transactions, and those that aren't automatically start a transaction in the mutator to run the triggers and create within the same transaction. This change makes the semantics of this method more explicit without much loss of usability and protects against the race that is likely.
Test Plan
ToDo