From 26398e6821ae9ecafd6e967dd1d7afdd5fc03dc3 Mon Sep 17 00:00:00 2001 From: chunhtai <47866232+chunhtai@users.noreply.github.com> Date: Mon, 4 Apr 2022 12:39:04 -0700 Subject: [PATCH] Fix crashes when current route parsing transactions are discarded (#100657) * Fix crashes when current route parsing transactions are discarded * refactor * update --- packages/flutter/lib/src/widgets/router.dart | 86 ++++++------------- .../flutter/test/widgets/router_test.dart | 68 +++++++++++++++ 2 files changed, 96 insertions(+), 58 deletions(-) diff --git a/packages/flutter/lib/src/widgets/router.dart b/packages/flutter/lib/src/widgets/router.dart index 31fcf98bf7..0168e56732 100644 --- a/packages/flutter/lib/src/widgets/router.dart +++ b/packages/flutter/lib/src/widgets/router.dart @@ -466,7 +466,7 @@ class Router extends StatefulWidget { } typedef _AsyncPassthrough = Future Function(Q); -typedef _DelegateRouteSetter = Future Function(T); +typedef _RouteSetter = Future Function(T); /// The [Router]'s intention when it reports a new [RouteInformation] to the /// [RouteInformationProvider]. @@ -492,8 +492,7 @@ enum RouteInformationReportingType { } class _RouterState extends State> with RestorationMixin { - Object? _currentRouteInformationParserTransaction; - Object? _currentRouterDelegateTransaction; + Object? _currentRouterTransaction; RouteInformationReportingType? _currentIntentionToReport; final _RestorableRouteInformation _routeInformation = _RestorableRouteInformation(); @@ -593,8 +592,7 @@ class _RouterState extends State> with RestorationMixin { widget.backButtonDispatcher != oldWidget.backButtonDispatcher || widget.routeInformationParser != oldWidget.routeInformationParser || widget.routerDelegate != oldWidget.routerDelegate) { - _currentRouteInformationParserTransaction = Object(); - _currentRouterDelegateTransaction = Object(); + _currentRouterTransaction = Object(); } if (widget.routeInformationProvider != oldWidget.routeInformationProvider) { oldWidget.routeInformationProvider?.removeListener(_handleRouteInformationProviderNotification); @@ -619,20 +617,27 @@ class _RouterState extends State> with RestorationMixin { widget.routeInformationProvider?.removeListener(_handleRouteInformationProviderNotification); widget.backButtonDispatcher?.removeCallback(_handleBackButtonDispatcherNotification); widget.routerDelegate.removeListener(_handleRouterDelegateNotification); - _currentRouteInformationParserTransaction = null; - _currentRouterDelegateTransaction = null; + _currentRouterTransaction = null; super.dispose(); } - void _processRouteInformation(RouteInformation information, ValueGetter<_DelegateRouteSetter> delegateRouteSetter) { - _currentRouteInformationParserTransaction = Object(); - _currentRouterDelegateTransaction = Object(); + void _processRouteInformation(RouteInformation information, ValueGetter<_RouteSetter> delegateRouteSetter) { + _currentRouterTransaction = Object(); widget.routeInformationParser! .parseRouteInformation(information) - .then(_verifyRouteInformationParserStillCurrent(_currentRouteInformationParserTransaction, widget)) - .then(delegateRouteSetter()) - .then(_verifyRouterDelegatePushStillCurrent(_currentRouterDelegateTransaction, widget)) - .then(_rebuild); + .then(_processParsedRouteInformation(_currentRouterTransaction, delegateRouteSetter)); + } + + _RouteSetter _processParsedRouteInformation(Object? transaction, ValueGetter<_RouteSetter> delegateRouteSetter) { + return (T data) async { + if (_currentRouterTransaction != transaction) { + return; + } + await delegateRouteSetter()(data); + if (_currentRouterTransaction == transaction) { + _rebuild(); + } + }; } void _handleRouteInformationProviderNotification() { @@ -641,56 +646,21 @@ class _RouterState extends State> with RestorationMixin { } Future _handleBackButtonDispatcherNotification() { - _currentRouteInformationParserTransaction = Object(); - _currentRouterDelegateTransaction = Object(); + _currentRouterTransaction = Object(); return widget.routerDelegate .popRoute() - .then(_verifyRouterDelegatePopStillCurrent(_currentRouterDelegateTransaction, widget)) - .then((bool data) { - _rebuild(); - return SynchronousFuture(data); - }); + .then(_handleRoutePopped(_currentRouterTransaction)); } - static final Future _never = Completer().future; // won't ever complete - - _AsyncPassthrough _verifyRouteInformationParserStillCurrent(Object? transaction, Router originalWidget) { - return (T data) { - if (transaction == _currentRouteInformationParserTransaction && - widget.routeInformationProvider == originalWidget.routeInformationProvider && - widget.backButtonDispatcher == originalWidget.backButtonDispatcher && - widget.routeInformationParser == originalWidget.routeInformationParser && - widget.routerDelegate == originalWidget.routerDelegate) { - return SynchronousFuture(data); - } - return _never as Future; - }; - } - - _AsyncPassthrough _verifyRouterDelegatePushStillCurrent(Object? transaction, Router originalWidget) { - return (void data) { - if (transaction == _currentRouterDelegateTransaction && - widget.routeInformationProvider == originalWidget.routeInformationProvider && - widget.backButtonDispatcher == originalWidget.backButtonDispatcher && - widget.routeInformationParser == originalWidget.routeInformationParser && - widget.routerDelegate == originalWidget.routerDelegate) - return SynchronousFuture(data); - return _never; - }; - } - - _AsyncPassthrough _verifyRouterDelegatePopStillCurrent(Object? transaction, Router originalWidget) { + _AsyncPassthrough _handleRoutePopped(Object? transaction) { return (bool data) { - if (transaction == _currentRouterDelegateTransaction && - widget.routeInformationProvider == originalWidget.routeInformationProvider && - widget.backButtonDispatcher == originalWidget.backButtonDispatcher && - widget.routeInformationParser == originalWidget.routeInformationParser && - widget.routerDelegate == originalWidget.routerDelegate) { - return SynchronousFuture(data); + if (transaction != _currentRouterTransaction) { + // A rebuilt was trigger from a different source. Returns true to + // prevent bubbling. + return SynchronousFuture(true); } - // A rebuilt was trigger from a different source. Returns true to - // prevent bubbling. - return SynchronousFuture(true); + _rebuild(); + return SynchronousFuture(data); }; } diff --git a/packages/flutter/test/widgets/router_test.dart b/packages/flutter/test/widgets/router_test.dart index 1a11d93725..f44d2d2569 100644 --- a/packages/flutter/test/widgets/router_test.dart +++ b/packages/flutter/test/widgets/router_test.dart @@ -2,6 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +import 'dart:async'; + import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; @@ -77,6 +79,54 @@ void main() { }); }); + testWidgets('Interrupts route parsing should not crash', (WidgetTester tester) async { + final SimpleRouteInformationProvider provider = SimpleRouteInformationProvider(); + provider.value = const RouteInformation( + location: 'initial', + ); + final CompleterRouteInformationParser parser = CompleterRouteInformationParser(); + final SimpleAsyncRouterDelegate delegate = SimpleAsyncRouterDelegate( + builder: (BuildContext context, RouteInformation? information) { + if (information == null) + return const Text('waiting'); + return Text(information.location!); + }, + ); + await tester.runAsync(() async { + await tester.pumpWidget(buildBoilerPlate( + Router( + routeInformationProvider: provider, + routeInformationParser: parser, + routerDelegate: delegate, + ), + )); + // Future has not yet completed. + expect(find.text('waiting'), findsOneWidget); + + final Completer firstTransactionCompleter = parser.completer; + + // Start a new parsing transaction before the previous one complete. + provider.value = const RouteInformation( + location: 'update', + ); + await tester.pump(); + expect(find.text('waiting'), findsOneWidget); + // Completing the previous transaction does not cause an update. + firstTransactionCompleter.complete(); + await firstTransactionCompleter.future; + await tester.pump(); + expect(find.text('waiting'), findsOneWidget); + expect(tester.takeException(), isNull); + + // Make sure the new transaction can complete and update correctly. + parser.completer.complete(); + await parser.completer.future; + await delegate.setNewRouteFuture; + await tester.pump(); + expect(find.text('update'), findsOneWidget); + }); + }); + testWidgets('Router.maybeOf can be null', (WidgetTester tester) async { final GlobalKey key = GlobalKey(); await tester.pumpWidget(buildBoilerPlate( @@ -1414,6 +1464,24 @@ class SimpleAsyncRouteInformationParser extends RouteInformationParser { + CompleterRouteInformationParser(); + + late Completer completer; + + @override + Future parseRouteInformation(RouteInformation information) async { + completer = Completer(); + await completer.future; + return SynchronousFuture(information); + } + + @override + RouteInformation restoreRouteInformation(RouteInformation configuration) { + return configuration; + } +} + class SimpleAsyncRouterDelegate extends RouterDelegate with ChangeNotifier { SimpleAsyncRouterDelegate({ required this.builder,