Skip to content

[go_router] Fix assertion failure when onException fires on initial navigation - #12216

Open
davidmigloz wants to merge 2 commits into
flutter:mainfrom
davidmigloz:upstream-onexception-initial-nav-fix
Open

davidmigloz wants to merge 2 commits into
flutter:mainfrom
davidmigloz:upstream-onexception-initial-nav-fix

Conversation

@davidmigloz

@davidmigloz davidmigloz commented Jul 16, 2026 •

Copy link
Copy Markdown
Contributor

When onException is provided and fires during the initial navigation (e.g. a deep-linked route blocked by onEnter returning Block.stop()), parserExceptionHandler in router.dart returns routerDelegate.currentConfiguration. On the initial navigation nothing has committed yet, so that configuration is empty, and GoRouterDelegate.setNewRoutePath crashes on assert(configuration.isNotEmpty || configuration.isError).

Crash sequence:

  1. App starts with initialLocation pointing at a route.
  2. onEnter returns Block.stop() for it.
  3. The parser builds an error RouteMatchList containing BlockedInitialNavigationException (isError == true) and invokes onException.
  4. parserExceptionHandler synchronously returns routerDelegate.currentConfiguration — still empty.
  5. setNewRoutePath asserts configuration.isNotEmpty || configuration.isError — neither holds — crash.

The fix has two parts:

  1. router.dart: fall back to the error match list when the current configuration is invalid — neither non-empty nor isError — satisfying the assertion's isError branch.
  2. parser.dart: in the onCanNotEnter no-prior-route branch, defer the onParserException call to a microtask (the same pattern as the existing deferral for the onEnter then callback nearby). Without this, a recovery navigation made synchronously inside onException — e.g. router.go('/fallback'), as in the issue's repro — is silently discarded by the Router's intent-token churn, leaving the app parked on the error state. Deferring past the in-flight parse lets synchronous recovery work the way onException normally does.

Behavior is unchanged everywhere else: non-initial navigations return the non-empty currentConfiguration as before, the plain unmatched-initial-route path keeps its fully synchronous onException call (existing redirect tests depend on it, and that path never drops the re-entrant navigation), and without onException neither code path is reached.

Tests in exception_handling_test.dart:

  • One test calls router.go('/fallback') synchronously from inside onException and asserts full recovery (fallback rendered, protected absent, router.state.uri.path == '/fallback'); a second drives the app-side deferred-via-scheduleMicrotask pattern. Verified red/green for each half: without the router.dart fallback the delegate assertion fires at delegate.dart:221; without the parser.dart deferral the synchronous recovery is silently dropped and the fallback never renders.
  • The router.dart fallback is also covered independently of the deferred path. An unmatched initial navigation with a non-navigating onException now renders the default error screen; before the fix the app just stayed blank, because the empty current configuration hit setNewRoutePath's equality early-return. And a second unmatched navigation no longer replaces the first installed error configuration: the fallback preserves any valid current configuration, whether non-empty or isError.

Validated after rebasing onto current main (go_router 18.0.2):

  • flutter test test/exception_handling_test.dart (10 tests)
  • Package and example test suites on the VM, Chrome, and Chrome/Wasm (456 package tests and 17 example tests per platform; 2 existing package tests skipped)
  • Repository format, analyze, validate, and publish dry-run checks

Fixes flutter/flutter#189582

Pre-Review Checklist

Footnotes

  1. Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling. ↩ ↩2

@github-actions github-actions Bot added p: go_router triage-framework Should be looked at in framework triage labels Jul 16, 2026
davidmigloz added a commit to davidmigloz/flutter_packages that referenced this pull request Aug 3, 2026
…avigation

When onEnter blocks the very first navigation there is no prior route to
restore, and onException handling returned an empty configuration that
tripped the setNewRoutePath assertion. Falls back to the error match list
when the current configuration is empty, defers onParserException via a
microtask in the no-prior-route branch only, and preserves valid error
configurations in parserExceptionHandler.

Squash of the two commits on the upstream PR branch:
flutter#12216
@tarrinneal

Copy link
Copy Markdown
Contributor

From traige: Is this pr still being worked on?

@tarrinneal tarrinneal added the waiting for response The Flutter team cannot make further progress on this PR until the author responds label Oct 6, 2026
…avigation

When onException fires during the initial navigation (e.g., an initial
deep link blocked by onEnter returning Block.stop()),
routerDelegate.currentConfiguration is empty. Returning it directly from
parserExceptionHandler causes GoRouterDelegate.setNewRoutePath to hit
`assert(configuration.isNotEmpty || configuration.isError)`.

Fall back to the error match list (isError=true) when the current
configuration is empty, satisfying the assertion while any recovery
navigation queued from onException is processed.

Additionally, defer the onParserException call itself to a microtask on
that same initial-navigation, blocked-with-no-prior-route path. Calling
it synchronously let the app crash-prevention fix land, but a
router.go() made synchronously from onException was silently discarded:
Flutter's Router mints a new intent token for the re-entrant parse while
the initial parse is still in-flight, dropping the outer result. The
deferral is scoped to that one parser.dart call site so ordinary initial
navigation exceptions (e.g. a plain unmatched route) keep calling
onException synchronously, as existing tests rely on.
@davidmigloz
davidmigloz force-pushed the upstream-onexception-initial-nav-fix branch from 00aeed1 to 3303cf8 Compare October 6, 2026 21:40
@davidmigloz

davidmigloz commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor Author

Yes, thanks for the ping. I have rebased this onto current main (go_router 18.0.2), resolved the intervening test-import migration, rerun the package and example suites on the VM, Chrome, and Chrome/Wasm, and refreshed the description. I am marking it ready for review now.

@davidmigloz
davidmigloz marked this pull request as ready for review October 6, 2026 21:41
@github-actions github-actions Bot removed the waiting for response The Flutter team cannot make further progress on this PR until the author responds label Oct 6, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request defers the execution of onParserException using a microtask in GoRouteInformationParser to prevent recovery navigations from being discarded during an initial parse. Additionally, it updates parserExceptionHandler in GoRouter to fall back to the error match list when the current configuration is empty and not an error, preventing an assertion failure in GoRouterDelegate.setNewRoutePath. Relevant unit tests and a changelog entry have been added. There are no review comments, so no further feedback is provided.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

p: go_router triage-framework Should be looked at in framework triage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[go_router] Assertion failure in setNewRoutePath when onException fires on the initial navigation

2 participants