fix(storage): share one in-flight provider initialization across concurrent first calls - #724
Open
Matthew-Selvam wants to merge 1 commit into
Open
Conversation
…urrent first calls getStorageProvider() checked _provider && _initialized, then awaited a dynamic import and initialize() before setting the flag. Two overlapping first requests — the page-load GET /api/storage racing a PUT /api/storage/[collection] — both fell through, each constructing and initializing a provider; the second assignment overwrote the first mid-initialize, duplicating initialize() and leaking the orphan's connections. The in-flight promise is now memoized: concurrent callers await the same build+initialize. A rejected initialize is un-memoized so the next request retries instead of awaiting a dead promise forever.
3 tasks
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
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.
Summary
getStorageProvider()had an initialization race: it checked_provider && _initialized, then awaited a dynamicimport()andinitialize()before setting the flag. Two overlapping first requests — e.g. the page-loadGET /api/storageracing aPUT /api/storage/[collection]— both fell through the check, each constructed and initialized a provider, and the second assignment overwrote the first mid-initialize. Result:initialize()ran twice, a duplicated (encrypted-wrapper) provider was created, and the orphaned provider's connections were leaked.Fix
The in-flight initialization promise is memoized: the first caller starts exactly one build+initialize, and every concurrent caller awaits the same promise. A rejected
initialize()is un-memoized so a transient DB outage doesn't become a permanent one (the next request retries from scratch instead of awaiting a dead promise forever).closeStorageProvider()also resets the memo so tests/cleanup keep working.Test plan
concurrent first calls share one provider and one initialize()—Promise.allof two first calls asserts a single instance and a singleinitialize()a failed initialize is not memoized — the next call retries from scratchtests/isolated/factory-singleton.test.tsstill pass unchangedFound during a broader code review of the storage layer; no issue existed yet for it.