Skip to content

async* stream never ends if the generator's return is caused by an exception #47342

Open
@vexcat

Description

@vexcat

Dart SDK version: 2.14.2 (stable) (Wed Sep 15 12:32:06 2021 +0200) on "linux_x64"
OS: Linux
Platforms: desktop (working), web on chrome (bug)

Code:

int idx = 0;
Future<int> getValueExceptionOnEnd() async {
  if (idx < 4) {
    return idx++;
  } else {
    throw RangeError("no more ints for you!");
  }
}

Stream<int> onlyWorksOnDesktop() async* {
  try {
    while (true) {
      yield await getValueExceptionOnEnd();
    }
  } on RangeError {
    print("all done! dart should close the stream after this.");
    return;
  }
}

Future<int?> _allPlatformsHelper() async {
  try {
    return await getValueExceptionOnEnd();
  } on RangeError {
    return null;
  }
}

Stream<int> worksOnAllPlatforms() async* {
  while (true) {
    final value = await _allPlatformsHelper();
    if (value == null) break;
    yield value;
  }
  print("all done! dart should close the stream after this.");
}

void bugDemo() async {
  idx = 0;
  await for (var value in worksOnAllPlatforms()) {
    print(value);
  }
  print("worksOnAllPlatforms()'s stream closed.");

  idx = 0;
  await for (var value in onlyWorksOnDesktop()) {
    print(value);
  }
  //This never runs when developing for the web with flutter
  //(though, interestingly, it does work with dart2js.)
  print("onlyWorksOnDesktop()'s stream closed.");
}

bugDemo is being run in a Flutter app targeting desktop & the web.

Desktop output (expected):

0
1
2
3
all done! dart should close the stream after this.
worksOnAllPlatforms()'s stream closed.
0
1
2
3
all done! dart should close the stream after this.
onlyWorksOnDesktop()'s stream closed.

Web output:

0
1
2
3
all done! dart should close the stream after this.
worksOnAllPlatforms()'s stream closed.
0
1
2
3
all done! dart should close the stream after this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2A bug or feature request we're likely to work onarea-web-jsIssues related to JavaScript support for Dart Web, including DDC, dart2js, and JS interop.dev-compiler-asyncweb-dev-compilerweb-triage-1priority assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions