stack_trace icon indicating copy to clipboard operation
stack_trace copied to clipboard

StateError deep ind stack_trace code: Future already completed.

Open escamoteur opened this issue 1 year ago • 9 comments

I currently get these here on a regular basis in Sentry:

StateError: Bad state: Future already completed
  File "future_impl.dart", line 18, in _Completer.completeError
  File "zone.dart", line 1666, in _RootZone.runBinary
  File "stack_zone_specification.dart", line 158, in StackZoneSpecification._handleUncaughtError
  File "zone.dart", line 1081, in _Zone._processUncaughtError
  File "zone.dart", line 1285, in _CustomZone.handleUncaughtError
  File "future_impl.dart", line 754, in Future._propagateToListeners
  File "future_impl.dart", line 649, in Future._completeError
  File "stack_zone_specification.dart", line 135, in StackZoneSpecification._registerBinaryCallback.<fn>.<fn>
  File "stack_zone_specification.dart", line 204, in StackZoneSpecification._run
  File "stack_zone_specification.dart", line 135, in StackZoneSpecification._registerBinaryCallback.<fn>
  File "zone.dart", line 1423, in _rootRunBinary
  File "zone.dart", line 1315, in _CustomZone.runBinary
  File "future_impl.dart", line 171, in _FutureListener.handleError
  File "future_impl.dart", line 852, in Future._propagateToListeners.handleError
  File "future_impl.dart", line 873, in Future._propagateToListeners
  File "future_impl.dart", line 649, in Future._completeError
  File "stack_zone_specification.dart", line 135, in StackZoneSpecification._registerBinaryCallback.<fn>.<fn>
  File "stack_zone_specification.dart", line 204, in StackZoneSpecification._run
  File "stack_zone_specification.dart", line 135, in StackZoneSpecification._registerBinaryCallback.<fn>
  File "zone.dart", line 1423, in _rootRunBinary
  File "zone.dart", line 1315, in _CustomZone.runBinary
  File "future_impl.dart", line 171, in _FutureListener.handleError
  File "future_impl.dart", line 852, in Future._propagateToListeners.handleError
  File "future_impl.dart", line 873, in Future._propagateToListeners
  File "future_impl.dart", line 649, in Future._completeError
  File "future_impl.dart", line 550, in Future._chainForeignFuture.<fn>
  File "stack_zone_specification.dart", line 204, in StackZoneSpecification._run
  File "stack_zone_specification.dart", line 114, in StackZoneSpecification._registerCallback.<fn>
  File "zone.dart", line 1399, in _rootRun
  File "zone.dart", line 1301, in _CustomZone.run
  File "zone.dart", line 1209, in _CustomZone.runGuarded
  File "zone.dart", line 1249, in _CustomZone.bindCallbackGuarded.<fn>
  File "schedule_microtask.dart", line 40, in _microtaskLoop
  File "schedule_microtask.dart", line 49, in _startMicrotaskLoop

This is how I use Chain.capture to get better stacktraces when en exception happens in the wrapped async function of a flutter_command:

        final completer = Completer<TResult>();
        Chain.capture(
          () => _func!(param as TParam).then(completer.complete),
          onError: completer.completeError,
        );
        result = await completer.future;

The use of the completer this way was the only way I could get back a meaningful stack_trace from Chain.capture

escamoteur avatar Sep 19 '23 12:09 escamoteur

@mraleph any help would be awesome to improve my package

escamoteur avatar Sep 19 '23 15:09 escamoteur

I can take a look next week

mraleph avatar Sep 19 '23 17:09 mraleph

That would be awesome Am 19. Sept. 2023, 19:40 +0200 schrieb Slava Egorov @.***>:

I can take a look next week — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you authored the thread.Message ID: @.***>

escamoteur avatar Sep 19 '23 18:09 escamoteur

hi @mraleph would be great if you could look into this

escamoteur avatar Oct 04 '23 10:10 escamoteur

Do you have some specific issue for me to look at? e.g. do you have an example code I could run which prints meaningless stack-trace? I am just unsure what I am supposed to help with.

mraleph avatar Oct 04 '23 13:10 mraleph

When I use the Chain.capture like in the above I get the exception Future already completed. Is it at all the right approach that I do above to call a passed function inside a Capture to get better stacktraces?

escamoteur avatar Oct 04 '23 13:10 escamoteur

@escamoteur The documentation for Chain.capture says:

Note that if callback produces multiple unhandled errors, onError may be called more than once.

That's what you are seeing here.

TBH, I am not sure why you need package:stack_trace for this to begin with. Native VM stacks should be good enough for asynchronous code.

mraleph avatar Oct 26 '23 10:10 mraleph

How can the wrapped call back produce more than one unhandled error? Is the way I try to use capture the right way at all? Am 26. Okt. 2023, 12:53 +0200 schrieb Slava Egorov @.***>:

@escamoteur The documentation for Chain.capture says:

Note that if callback produces multiple unhandled errors, onError may be called more than once. That's what you are seeing here. — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were mentioned.Message ID: @.***>

escamoteur avatar Oct 26 '23 11:10 escamoteur

It can spawn multiple asynchronous operations in parallel and not listen to either, e.g.

void foo() {
  Future.delayed(const Duration(milliseconds: 10), () => throw 'One');
  Future.delayed(const Duration(milliseconds: 10), () => throw 'Two');
}

This function will produce two unhandled errors. It's up to you to decide how to handle it, e.g. you can just ignore all errors after the first one.

mraleph avatar Oct 26 '23 11:10 mraleph