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

ProviderContainer weirdly catches and handles unintended errors #1874

Closed
lucavenir opened this issue Nov 3, 2022 · 4 comments
Closed

ProviderContainer weirdly catches and handles unintended errors #1874

lucavenir opened this issue Nov 3, 2022 · 4 comments
Assignees
Labels
bug Something isn't working

Comments

@lucavenir
Copy link
Sponsor Contributor

lucavenir commented Nov 3, 2022

Describe the bug
I made a mistake on my codebase. Dang, that happens. Specifically, I gave two routes the same name.
I'm using GoRouter as a dependency; rightfully, my router will complain about this with a specific runtime exception.

The thing is, ProviderContainer kicks in, eats this exception and returns something I'm unable to understand.
I had to do a scavenge the stack trace to understand where such exception came from.

Here's what I get on my terminal:

some incomprehensible logs
══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following assertion was thrown building UncontrolledProviderScope:
Got a stack frame from package:stack_trace, where a vm or web frame was expected. This can happen if
FlutterError.demangleStackTrace was not set in an environment that propagates non-standard stack
traces to the framework, such as during tests.
'package:flutter/src/foundation/stack_frame.dart':
Failed assertion: line 192 pos 7: 'line != '===== asynchronous gap ===========================''

The relevant error-causing widget was:
  UncontrolledProviderScope
  UncontrolledProviderScope:file:///home/venir/snap/flutter/common/flutter/.pub-cache/hosted/pub.dartlang.org/flutter_riverpod-2.1.1/lib/src/framework.dart:250:12

When the exception was thrown, this was the stack:
#2      StackFrame.fromStackTraceLine (package:flutter/src/foundation/stack_frame.dart:192:7)
#3      MappedIterator.moveNext (dart:_internal/iterable.dart:391:20)
#4      WhereTypeIterator.moveNext (dart:_internal/iterable.dart:869:20)
#5      new _GrowableList._ofOther (dart:core-patch/growable_array.dart:202:26)
#6      new _GrowableList.of (dart:core-patch/growable_array.dart:152:26)
#7      new List.of (dart:core-patch/array_patch.dart:51:28)
#8      Iterable.toList (dart:core/iterable.dart:470:12)
#9      StackFrame.fromStackString (package:flutter/src/foundation/stack_frame.dart:93:10)
#10     StackFrame.fromStackTrace (package:flutter/src/foundation/stack_frame.dart:78:12)
#11     FlutterErrorDetails.debugFillProperties (package:flutter/src/foundation/assertions.dart:712:57)
#12     DiagnosticableNode.builder.<anonymous closure> (package:flutter/src/foundation/diagnostics.dart:2940:17)
#13     DiagnosticableNode.builder (package:flutter/src/foundation/diagnostics.dart:2943:8)
#14     _FlutterErrorDetailsNode.builder (package:flutter/src/foundation/assertions.dart:1289:56)
#15     DiagnosticableNode.emptyBodyDescription (package:flutter/src/foundation/diagnostics.dart:2954:77)
#16     DiagnosticsNode.toJsonMap.<anonymous closure> (package:flutter/src/foundation/diagnostics.dart:1632:13)
#17     DiagnosticsNode.toJsonMap (package:flutter/src/foundation/diagnostics.dart:1661:6)
#18     WidgetInspectorService._nodeToJson (package:flutter/src/widgets/widget_inspector.dart:1606:18)
#19     WidgetInspectorService._reportStructuredError (package:flutter/src/widgets/widget_inspector.dart:937:44)
#20     FlutterError.reportError (package:flutter/src/foundation/assertions.dart:1184:14)
#21     _debugReportException (package:flutter/src/widgets/framework.dart:6493:16)
#22     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4887:9)
#23     StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5050:11)
#24     Element.rebuild (package:flutter/src/widgets/framework.dart:4604:5)
#25     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4859:5)
#26     StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:5041:11)
#27     ComponentElement.mount (package:flutter/src/widgets/framework.dart:4853:5)
...     Normal element mounting (7 frames)
#34     _UncontrolledProviderScopeElement.mount (package:flutter_riverpod/src/framework.dart:309:11)
...     Normal element mounting (9 frames)
#43     Element.inflateWidget (package:flutter/src/widgets/framework.dart:3863:16)
#44     Element.updateChild (package:flutter/src/widgets/framework.dart:3592:18)
#45     RenderObjectToWidgetElement._rebuild (package:flutter/src/widgets/binding.dart:1195:16)
#46     RenderObjectToWidgetElement.mount (package:flutter/src/widgets/binding.dart:1164:5)
#47     RenderObjectToWidgetAdapter.attachToRenderTree.<anonymous closure> (package:flutter/src/widgets/binding.dart:1111:18)
#48     BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2605:19)
#49     RenderObjectToWidgetAdapter.attachToRenderTree (package:flutter/src/widgets/binding.dart:1110:13)
#50     WidgetsBinding.attachRootWidget (package:flutter/src/widgets/binding.dart:945:7)
#51     WidgetsBinding.scheduleAttachRootWidget.<anonymous closure> (package:flutter/src/widgets/binding.dart:925:7)
(elided 6 frames from class _AssertionError, class _RawReceivePortImpl, class _Timer, and dart:async-patch)

════════════════════════════════════════════════════════════════════════════════════════════════════

Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 4606 pos 14: 'owner!._debugCurrentBuildTarget == this': is not true.

Another exception was thrown: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 4606 pos 14: 'owner!._debugCurrentBuildTarget == this': is not true.
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: 'package:flutter/src/widgets/framework.dart': Failed assertion: line 2609 pos 20: '_debugCurrentBuildTarget == context': is not true.
     _AssertionError._doThrowNew (dart:core-patch/errors_patch.dart:51:61)
     _AssertionError._throwNew (dart:core-patch/errors_patch.dart:40:5)
     BuildOwner.buildScope.<anonymous closure> (package:flutter/src/widgets/framework.dart:2609:20)
     BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2613:12)
     RenderObjectToWidgetAdapter.attachToRenderTree (package:flutter/src/widgets/binding.dart:1110:13)
     WidgetsBinding.attachRootWidget (package:flutter/src/widgets/binding.dart:945:7)
     WidgetsBinding.scheduleAttachRootWidget.<anonymous closure> (package:flutter/src/widgets/binding.dart:925:7)
     Timer._createTimer.<anonymous closure> (dart:async-patch/timer_patch.dart:18:15)
     _Timer._runTimers (dart:isolate-patch/timer_impl.dart:398:19)
     _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:429:5)
     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:192:12)

Versus what I should get on my terminal if I hadn't used ProviderScope:

some useful logs
══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following assertion was thrown building Myapp(dirty):
duplication fullpaths for name "home":/home, /home
'package:go_router/src/configuration.dart':
Failed assertion: line 188 pos 15: '!_nameToPath.containsKey(name)'

The relevant error-causing widget was:
  Myapp Myapp:file:///home/venir/Documents/Riverpod/riverpod_bug_repro/lib/main2.dart:6:16

When the exception was thrown, this was the stack:
#2      RouteConfiguration._cacheNameToPath (package:go_router/src/configuration.dart:188:15)
#3      new RouteConfiguration (package:go_router/src/configuration.dart:24:5)
#4      new GoRouter (package:go_router/src/router.dart:65:27)
#5      globalRouter (package:riverpod_bug_repro/router/router.dart:17:22)
#6      globalRouter (package:riverpod_bug_repro/router/router.dart)
#7      Myapp.build (package:riverpod_bug_repro/main2.dart:17:21)
#8      StatelessElement.build (package:flutter/src/widgets/framework.dart:4949:49)
#9      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4878:15)
#10     Element.rebuild (package:flutter/src/widgets/framework.dart:4604:5)
#11     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4859:5)
#12     ComponentElement.mount (package:flutter/src/widgets/framework.dart:4853:5)
#13     Element.inflateWidget (package:flutter/src/widgets/framework.dart:3863:16)
#14     Element.updateChild (package:flutter/src/widgets/framework.dart:3592:18)
#15     RenderObjectToWidgetElement._rebuild (package:flutter/src/widgets/binding.dart:1195:16)
#16     RenderObjectToWidgetElement.mount (package:flutter/src/widgets/binding.dart:1164:5)
#17     RenderObjectToWidgetAdapter.attachToRenderTree.<anonymous closure> (package:flutter/src/widgets/binding.dart:1111:18)
#18     BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2605:19)
#19     RenderObjectToWidgetAdapter.attachToRenderTree (package:flutter/src/widgets/binding.dart:1110:13)
#20     WidgetsBinding.attachRootWidget (package:flutter/src/widgets/binding.dart:945:7)
#21     WidgetsBinding.scheduleAttachRootWidget.<anonymous closure> (package:flutter/src/widgets/binding.dart:925:7)
(elided 6 frames from class _AssertionError, class _RawReceivePortImpl, class _Timer, and dart:async-patch)

════════════════════════════════════════════════════════════════════════════════════════════════════

To Reproduce
I made it! You can reproduce this behavior on this repo. To reproduce, do the following:

  1. Run the application using Riverpod via flutter run -t lib/main.dart: you'll get weird errors
  2. Run the exact same application, without using Riverpod: flutter run -t lib/main2.dart: you'll get useful errors

Expected behavior
main.dart and main2.dart should show the same exception, i.e. the readable GoRouter one, as ProviderScope shouldn't absorb nor modify the nature of this exception, I guess.

EDIT. I forgot to push my commits onto the repro, sorry. Now it's updated.

@lucavenir lucavenir added bug Something isn't working needs triage labels Nov 3, 2022
@mattermoran
Copy link

mattermoran commented Nov 4, 2022

I have actually run into this issue before and I thought I was doing something very wrong. Debugging was very painful without seeing the actual error message. I've used this snippet to get readable error message though stacktrace is still messed up.

import 'package:stack_trace/stack_trace.dart';

FlutterError.demangleStackTrace = (StackTrace stack) {
  if (stack is Trace) return stack.vmTrace;
  if (stack is Chain) return stack.toTrace().vmTrace;
  return stack;
};

@lucavenir
Copy link
Sponsor Contributor Author

lucavenir commented Nov 15, 2022

I am unable to access Trace and Chain classes in my code. What am I missing?

EDIT. Sorry, I didn't know about this, they are both from stack_trace.

@mattermoran
Copy link

Ah yes, my bad, should've been more specific :)

@huynguyennovem
Copy link

I'm seeing this stack trace in my project. This issue only occurs when I use a nested route in GoRoute, for eg:

GoRoute(
  path: '/home',
  builder: (context, state) => const HomePage(),
  routes: [
    GoRoute(
      path: 'recent_contacts',
      builder: (context, state) => const RecentContactsPage(),
    ),
  ],
),

Not sure if OP also has the same route structure since his repo sample is not found now. If I remove mRecentContacts route here, the issue will not occur.

Adding demangleStackTrace as mentioned above can work around this, but hope there is another better solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants