Skip to content

Inconsistent handling of an error thrown in an error handler on the VM #45617

Open
@natebosch

Description

@natebosch

This is similar to #45616 but behavior is a little different on the VM.

Reproduction case:

import 'dart:async';

void main() {
  runZoned(() {
    somethingAsync();
  }, zoneSpecification: ZoneSpecification(handleUncaughtError: handleError));
}

void handleError(
    Zone _, ZoneDelegate __, Zone ___, Object error, StackTrace stackTrace) {
  print('I see the error: $error');
  throw 'extra error';
}

void somethingAsync() async {
  await Future.delayed(const Duration(seconds: 1));
  throw 'sad';
}

I would expect either the extra error to never surface back to the handleError callback, or for it to show up in an infinite loop.

What happens in practice it that it surfaces more than 1, but less than ∞ times. The number of times varies by Dart release.

In Dart 2.0.0 and 2.1.0 the output shows up 3 times:

I see the error: sad
I see the error: extra error
I see the error: extra error
I see the error: extra error
Unhandled exception:
extra error

Starting in Dart 2.2.0 the output shows up only 2 times:

I see the error: sad
I see the error: extra error
I see the error: extra error
Unhandled exception:
extra error

There is a similar version disparity in the behavior of an implementation without using async.

import 'dart:async';

void main() {
  runZoned(() {
    somethingAsync();
  }, zoneSpecification: ZoneSpecification(handleUncaughtError: handleError));
}

void handleError(
    Zone _, ZoneDelegate __, Zone ___, Object error, StackTrace stackTrace) {
  print('I see the error: $error');
  throw 'extra error';
}

void somethingAsync() {
  Future.delayed(const Duration(seconds: 1)).then((_) {
    throw 'sad';
  });
}

Here the output shows up 1 fewer times:

Dart 2.0.0 and 2.1.0:

I see the error: sad
I see the error: extra error
I see the error: extra error
Unhandled exception:
extra error

Dart 2.2.0 and above:

I see the error: sad
I see the error: extra error
Unhandled exception:
extra error

I think that that the non-async version in recent Dart versions has the best behavior. @lrhn - is this specified somewhere? Should we look at adding any tests for behavior of error handlers which throw themselves?

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-vmUse area-vm for VM related issues, including code coverage, and the AOT and JIT backends.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions