Await in-flight batch in IndexedDbFileSystem.flush() - #409
Closed
DenisovAV wants to merge 1 commit into
Closed
Conversation
11be8ac made flush() return _startWorkingIfNeeded directly, which is a genuine fence when the worker is idle but takes no branch while a batch is already being written, so flush completes before that batch reaches IndexedDB. Keep the idle fast path and queue a no-op marker only when busy. _FunctionWorkItem does not override insertInto, so the marker always lands at the tail of _pendingWork and resolves after the work ahead of it is written. Fixes simolus3#408.
simolus3
reviewed
Sep 12, 2026
Owner
There was a problem hiding this comment.
Thanks for the PR. I agree the current state is buggy, but we can likely clean up the fix a bit.
Instead of adding a separate branch here, can we make flush() return the future associated with a prior work item? The easiest way to do that would be to replace bool _isWorking with a Future<void>? _currentWork.
In flush(), we then await _currentWork or _startWorkingIfNeeded if it's unset. This avoids scheduling a bogus work item, which also starts an IndexedDB transaction (so it's not free).
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.
Fixes #408.
11be8acbmadeflush()return_startWorkingIfNeeded(isImplicit: false)directly. That is a genuine fence when the worker is idle —_startWorkingIfNeedednow awaits_performWrites— and it removed the marker's overhead. But while a batch is already being written,_startWorkingIfNeededtakes no branch and its future completes immediately, soflush()returns before that batch reaches IndexedDB. WithwriteAutomatically: true,_isWorkingis set synchronously by the implicit start in_submitWork, so this is the ordinary state right after any write.This keeps the idle fast path and restores the marker only when busy:
Why the marker is sound:
_FunctionWorkItemdoes not overrideinsertInto, so it always lands at the tail of_pendingWorkand is never merged or skipped. When the running batch finishes,whenCompleterestarts work, and the next batch carries every write submitted before the flush plus the marker last. WithwriteAutomatically: falsethe running batch can only have been started explicitly, so its restart also usesisImplicit: falseand proceeds. This is the same shapeclose()already uses.On testing
I haven't added a test, and want to be upfront about why rather than add one that passes for the wrong reason. The obvious check — flush, then read the data back through a fresh
IndexedDbFileSystemorAsynchronousIndexedDbFileSystem— doesn't discriminate: an IndexedDB transaction that overlaps a pending readwrite transaction on the same store waits for it, so the read sees the data whether or notflush()actually awaited. A test that asserts on the number of event-loop turns beforeflush()resolves would catch it (it reads 0 on 3.5.2 and 7 on 3.3.3 in headless Chrome), but that is fragile. If you have a preferred way to observe work-queue state from a test, I'm happy to add one.Verified:
dart analyzeclean on the changed file,dart formatunchanged.