Skip to content
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

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

Open
mraleph opened this issue Nov 7, 2024 · 1 comment
Open

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

mraleph opened this issue Nov 7, 2024 · 1 comment
Labels
area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. web-dart2js

Comments

@mraleph
Copy link
Member

mraleph commented Nov 7, 2024

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(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

@mraleph mraleph added web-dart2js area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. labels Nov 7, 2024
@mnordine
Copy link
Contributor

mnordine commented Nov 7, 2024

I think your program maybe should have:

print('Caught: $e');

instead of:

print(e);

to produce the output you gave?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. web-dart2js
Projects
None yet
Development

No branches or pull requests

2 participants