rxdart icon indicating copy to clipboard operation
rxdart copied to clipboard

Nested asynchronous loops don't work with groupBy

Open feinstein opened this issue 1 year ago • 1 comments

This code works just fine:

// prints:
// 0
// Instance of '_MultiStream<int>'
// 0
// 1
// Instance of '_MultiStream<int>'
// 0
// 1
// 2
// Instance of '_MultiStream<int>'
// 0
// 1
// 2
// 3
// flutter: Instance of '_MultiStream<int>'
await for (final item in Stream.fromIterable(List.generate(4, (index) => Stream.fromIterable(List.generate(index + 1, (index) => index))))) {
  await for (final item2 in item) {
    print(item2);
  }
  print(item);
}

Now if I use a groupBy it only runs once:

// prints:
// 0
await for (final item in Stream.fromIterable(List.generate(30, (index) => index)).groupBy((value) => value % 3)) {
    await for (final item2 in item) {
      print(item2);
    }
    print(item);
  }

If I remove the second await for I can see the stream emits 3 GroupedStream as expected.

feinstein avatar May 30 '23 17:05 feinstein

The inner Stream of groupBy completes when the outer is complete. Currently, we should not use asyncExpand((s) => s or nested await for. It causes a death lock

hoc081098 avatar Jun 20 '23 09:06 hoc081098