fix: resolve race-condition deadlock in parallelForEach using package:pool - #9551
Closed
sigurdm wants to merge 1 commit into
Closed
fix: resolve race-condition deadlock in parallelForEach using package:pool#9551sigurdm wants to merge 1 commit into
sigurdm wants to merge 1 commit into
Conversation
Member
|
I do not believe the explanations given here. I could be wrong, but when reading: while (running > 0) {
await itemDone.wait;
}yes, the
We have code like: The decrement cannot happen after |
Contributor
Author
|
I think you are right - this won't help :/ |
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.
Description
Fixes a race-condition deadlock in
StreamExtensions.parallelForEachthat caused background tasks such ascheck-datastore-integrityandsynchronize-exported-apito hang indefinitely.Root Cause of the Deadlock
The
Notifierhelper class acts as an event broadcaster rather than a counting semaphore:When multiple tasks complete close together:
Notifier.notify()marks its internal_completeras completed.notify()calls occurring beforewaitis accessed are dropped (!_completer.isCompletedis false).await itemDone.waitis subsequently called,if (_completer.isCompleted)resets_completer = Completer()and returns an uncompletedFuturefrom the new completer, waiting for a subsequent notification instead of resolving immediately.parallelForEachcompletes itsawait forstream iteration and enters thefinallyblock:waitis accessed or during a microtask turn,runningbecomes0, butwaitreturns a brand new unresolvedCompleter. Because no tasks remain running to callnotify(),parallelForEachdeadlocks indefinitely.Minimal Reproduction Example
Fix
parallelForEachto usePoolfrompackage:pool(already inpubspec.yaml).await pool.request()cleanly enforces backpressure on the stream whenmaxParalleltasks are in-flight.resource.release()immediately grants execution to the next waiting request in FIFO order.await pool.close()safely waits for all in-flight tasks to complete in thefinallyblock.parallel_foreach_test.dartto verify no race conditions or deadlocks occur under high-throughput streaming workloads.