Skip to content

Incorrect behavior of try/catch within finally within async* method #57046

@mraleph

Description

@mraleph

The following code shows different behavior on the Web and on the VM. It seems in dart2js case exception thrown from (*) is clobbering the exception which is currently in flight.

Future<void> something(int v) async {
  await Future.delayed(Duration(milliseconds: 10));
  throw StateError('error $v');
}

Stream<int> _readLoop() async* {
  try {
    while (true) {
      yield 1;
      await something(0);
    }
  } catch (e) {
    throw StateError('converted');
  } finally {
    try {
      await something(1);  // (*)
    } catch (e) {
      print('Ignored: $e');
    }
  }
}

void main() async {
  try {
    await for (var _ in _readLoop()) {
    }
  } catch (e) {
    print('Caught: $e');
  }
}
$ dart --version
Dart SDK version: 3.7.0-112.0.dev (dev) (Wed Nov 6 08:02:51 2024 -0800) on "macos_arm64"
$ dart test.dart
Ignored: Bad state: error 1
Caught: Bad state: converted
$ dart compile js test.dart
$ ~/.jsvu/v8 /Users/vegorov/src/flutter/flutter/bin/cache/dart-sdk/lib/_internal/js_runtime/lib/preambles/d8.js out.js
Ignored: Bad state: error 1
Caught: Bad state: error 1

DDC might also have a problem with desugarring but of a different nature because in DartPad I get unhandled StateError.

/cc @natebiggs

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-web-jsIssues related to JavaScript support for Dart Web, including DDC, dart2js, and JS interop.web-dart2js

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions