Skip to content

Wrong behavior of async* function after pause-resume-pause #49465

Closed as not planned
@sgrekhov

Description

@sgrekhov

Generator function marked as async* performs excessive generation cicle after resume()

Example:

import "dart:async";

List<int> readyToSent = [];
List<int> sent = [];

Stream<int> generator() async* {
  for (int i = 1; i <= 5; i++) {
    readyToSent.add(i);
    yield i;
    sent.add(i);
  }
}

main() async {
  List received = [];
  Stream<int> s = generator();
  late StreamSubscription<int> ss;
  ss = s.listen((int i) async {
    received.add(i);
    if (i == 2) {
      print(sent); // [1]
      print(readyToSent); // [1, 2]
      ss.pause();
      await Future.delayed(Duration(milliseconds: 100));
      print(sent); // [1]
      print(readyToSent); // [1,2]
      ss.resume();
      print(sent); // [1]
      print(readyToSent); // [1, 2]
      ss.pause();
      await Future.delayed(Duration(milliseconds: 100));
      print(sent); // [1, 2] Why?
      print(readyToSent); // [1, 2, 3]
      await ss.cancel();
    }
  });
  await Future.delayed(Duration(seconds: 1));
  print("F1: $received"); // [1, 2]
  print("F2: $sent");     // [1, 2]
  print("F3: $readyToSent");  // [1, 2, 3]
}

Tested on the edge version of SDK on Linux

Cc @lrhn

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-vmUse area-vm for VM related issues, including code coverage, and the AOT and JIT backends.closed-as-intendedClosed as the reported issue is expected behaviortype-bugIncorrect behavior (everything from a crash to more subtle misbehavior)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions