-
Notifications
You must be signed in to change notification settings - Fork 91
fix(pool): fix forEach cleanup on error and enable WasmGC tests #2360
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kevmoo
wants to merge
14
commits into
main
Choose a base branch
from
pool_fun
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
d17736a
Evaluate and improve pool package
kevmoo 7e6292e
fun
kevmoo 9631a44
enable node!
kevmoo d9d01a4
two more fixes
kevmoo e014f80
private bits in test
kevmoo 9d7131c
add an example while we're at it!
kevmoo 00d95e7
tiny nit about iterable silly
kevmoo edcee46
Update pkgs/pool/CHANGELOG.md
kevmoo be69385
review feedback
kevmoo 744f5b8
Merge branch 'main' into pool_fun
kevmoo b325bbc
Merge remote-tracking branch 'origin/pool_fun' into pool_fun
kevmoo f37d2e0
a test and a fix
kevmoo 89eaa62
prepare release
kevmoo 5def979
rollback other changes
kevmoo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| // Copyright (c) 2026, the Dart project authors. Please see the AUTHORS file | ||
| // for details. All rights reserved. Use of this source code is governed by a | ||
| // BSD-style license that can be found in the LICENSE file. | ||
|
|
||
| import 'package:pool/pool.dart'; | ||
|
|
||
| void main() async { | ||
| // Create a pool that allows at most 3 concurrent resources. | ||
| final pool = Pool(3); | ||
|
|
||
| print('Starting tasks with pool size of 3...'); | ||
|
|
||
| // Use pool.forEach to process a list of items concurrently. | ||
| // This is useful for limiting concurrent network requests or file I/O. | ||
| final items = List.generate(10, (i) => i); | ||
|
|
||
| await for (final result in pool.forEach(items, (item) async { | ||
| print(' [Start] Processing item $item'); | ||
| // Simulate some async work like a network request. | ||
| await Future<void>.delayed(const Duration(milliseconds: 100)); | ||
| print(' [Done] Processing item $item'); | ||
| return 'Result for $item'; | ||
| })) { | ||
| print('Processed: $result'); | ||
| } | ||
|
kevmoo marked this conversation as resolved.
|
||
|
|
||
| print('All tasks completed!'); | ||
|
|
||
| // Close the pool. | ||
| await pool.close(); | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,7 +27,7 @@ class Pool { | |
| /// allocated. | ||
| /// | ||
| /// See [PoolResource.allowRelease]. | ||
| final _onReleaseCallbacks = Queue<void Function()>(); | ||
| final _onReleaseCallbacks = Queue<FutureOr<void> Function()>(); | ||
|
|
||
| /// Completers that will be completed once `onRelease` callbacks are done | ||
| /// running. | ||
|
|
@@ -153,7 +153,7 @@ class Pool { | |
| Stream<T> forEach<S, T>( | ||
| Iterable<S> elements, FutureOr<T> Function(S source) action, | ||
| {bool Function(S item, Object error, StackTrace stack)? onError}) { | ||
| onError ??= (item, e, s) => true; | ||
| final errorHandler = onError ?? (item, e, s) => true; | ||
|
|
||
| var cancelPending = false; | ||
|
|
||
|
|
@@ -163,7 +163,7 @@ class Pool { | |
| late Iterator<S> iterator; | ||
|
|
||
| Future<void> run(int _) async { | ||
| while (iterator.moveNext()) { | ||
| while (!cancelPending && iterator.moveNext()) { | ||
| // caching `current` is necessary because there are async breaks | ||
| // in this code and `iterator` is shared across many workers | ||
| final current = iterator.current; | ||
|
|
@@ -182,7 +182,7 @@ class Pool { | |
| try { | ||
| value = await action(current); | ||
| } catch (e, stack) { | ||
| if (onError!(current, e, stack)) { | ||
| if (errorHandler(current, e, stack)) { | ||
| controller.addError(e, stack); | ||
| } | ||
| continue; | ||
|
|
@@ -197,11 +197,24 @@ class Pool { | |
| iterator = elements.iterator; | ||
|
|
||
| assert(doneFuture == null); | ||
| var futures = Iterable<Future<void>>.generate( | ||
| var futures = List<Future<void>>.generate( | ||
| _maxAllocatedResources, (i) => withResource(() => run(i))); | ||
| doneFuture = Future.wait(futures, eagerError: true) | ||
|
|
||
| // Eagerly forward errors to the stream and trigger cancellation. | ||
| Future.wait(futures, eagerError: true) | ||
| .onError((Object error, StackTrace stack) { | ||
| cancelPending = true; | ||
| controller.addError(error, stack); | ||
| return <void>[]; | ||
| }); | ||
|
|
||
| // Wait for all work to actually complete before closing the stream. | ||
| doneFuture = Future.wait(futures, eagerError: false) | ||
| .then<void>((_) {}) | ||
| .catchError(controller.addError); | ||
| .catchError((Object e) { | ||
| // We handle errors in the eager wait above, so we can ignore them here | ||
| // to avoid unhandled exceptions. | ||
| }); | ||
|
|
||
| doneFuture!.whenComplete(controller.close); | ||
| } | ||
|
|
@@ -210,8 +223,9 @@ class Pool { | |
| sync: true, | ||
| onListen: onListen, | ||
| onCancel: () async { | ||
| assert(!cancelPending); | ||
| cancelPending = true; | ||
| resumeCompleter?.complete(); | ||
| resumeCompleter = null; | ||
| await doneFuture; | ||
| }, | ||
|
kevmoo marked this conversation as resolved.
|
||
| onPause: () { | ||
|
|
@@ -275,7 +289,7 @@ class Pool { | |
|
|
||
| /// If there are any pending requests, this will fire the oldest one after | ||
| /// running [onRelease]. | ||
| void _onResourceReleaseAllowed(void Function() onRelease) { | ||
| void _onResourceReleaseAllowed(FutureOr<void> Function() onRelease) { | ||
| _resetTimer(); | ||
|
|
||
| if (_requestedResources.isNotEmpty) { | ||
|
|
@@ -297,15 +311,17 @@ class Pool { | |
| /// | ||
| /// Futures returned by [_runOnRelease] always complete in the order they were | ||
| /// created, even if earlier [onRelease] callbacks take longer to run. | ||
| Future<PoolResource> _runOnRelease(void Function() onRelease) { | ||
| Future<PoolResource> _runOnRelease(FutureOr<void> Function() onRelease) { | ||
| var completer = Completer<PoolResource>.sync(); | ||
| _onReleaseCompleters.add(completer); | ||
|
|
||
| Future.sync(onRelease).then((value) { | ||
| _onReleaseCompleters.removeFirst().complete(PoolResource._(this)); | ||
| }).catchError((Object error, StackTrace stackTrace) { | ||
| }).onError((Object error, StackTrace stackTrace) { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. move to the better typed version! |
||
| _onReleaseCompleters.removeFirst().completeError(error, stackTrace); | ||
| _onResourceReleased(); | ||
| }); | ||
|
kevmoo marked this conversation as resolved.
|
||
|
|
||
| var completer = Completer<PoolResource>.sync(); | ||
| _onReleaseCompleters.add(completer); | ||
| return completer.future; | ||
| } | ||
|
|
||
|
|
||
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.