fix(core): add timeout to BackgroundProcessManager.close()#14719
Open
anivar wants to merge 1 commit intoaws-amplify:mainfrom
Open
fix(core): add timeout to BackgroundProcessManager.close()#14719anivar wants to merge 1 commit intoaws-amplify:mainfrom
anivar wants to merge 1 commit intoaws-amplify:mainfrom
Conversation
close() hangs forever when jobs depend on unavailable resources (e.g. network down during DataStore subscription teardown). Race _closingPromise against a 10s timeout so it always resolves.
|
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.
We've been hitting a persistent issue where DataStore gets permanently stuck in "Stopping" state after network drops. Once stuck, every save/query/start throws DataStoreStateError and only an app restart recovers. This is a real problem for our offline-first POS app where network is unreliable.
Traced it down to
BackgroundProcessManager.close()— it awaitsPromise.allSettled(jobs)with no timeout. The subscription processor never implements theonTerminatehandler (there's a TODO at subscription.ts:411), so when network is down those jobs just hang forever.close()never returns, DataStore never transitions out of Stopping.Related: #13035 #10612 #12359
The fix is small — race
_closingPromiseagainst a 10s timeout soclose()always resolves. One subtlety:close()is async and ends withreturn this._closingPromise, which in an async function adopts the promise state. So even if you raced past theawait, the return would re-block callers on the original never-settling allSettled. Fixed by reassigning_closingPromiseto the race result.After timeout, zombie jobs stay in the Set but self-clean via the existing
.then()inregisterPromisewhen they eventually resolve. This matches what the community has been doing as app-layer workarounds (see #10612).Ideally the subscription/sync processors would implement
onTerminateproperly, but that's a bigger change across multiple files. This at least unblocks the stop/start cycle.