Skip to content

Commit 0cb3b7e

Browse files
kyungilchoellietteloic-sharma
authored
Skip removed observers during app exit requests (flutter#190343)
`WidgetsBinding.handleRequestAppExit` iterates over a snapshot of registered observers so callbacks can add or remove observers without causing concurrent modification. When an earlier asynchronous observer callback causes a later `AppLifecycleListener` to be disposed, the disposed listener remains in that snapshot and is invoked. In debug mode this reports a use-after-dispose assertion; in release mode it runs the stale callback. This change checks that each observer is still registered immediately before invoking it. It preserves snapshot iteration for reentrancy safety while skipping observers removed during an earlier asynchronous callback. The regression test pauses the first exit callback, disposes a second listener while the first callback is pending, resumes dispatch, and verifies that the disposed listener is not called. Fixes flutter#189472. ## Tests - `./bin/flutter analyze packages/flutter/lib/src/widgets/binding.dart` - `./bin/flutter analyze packages/flutter/test/widgets/app_lifecycle_listener_test.dart` - `./bin/flutter test --reporter compact packages/flutter/test/widgets/app_lifecycle_listener_test.dart packages/flutter/test/widgets/binding_test.dart` ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md --------- Co-authored-by: Elliott Brooks <[email protected]> Co-authored-by: Loïc Sharma <[email protected]>
1 parent 6ec64b3 commit 0cb3b7e

2 files changed

Lines changed: 37 additions & 0 deletions

File tree

‎packages/flutter/lib/src/widgets/binding.dart‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -896,6 +896,9 @@ mixin WidgetsBinding
896896
Future<AppExitResponse> handleRequestAppExit() async {
897897
var didCancel = false;
898898
for (final observer in List<WidgetsBindingObserver>.of(_observers)) {
899+
if (!_observers.contains(observer)) {
900+
continue;
901+
}
899902
try {
900903
if ((await observer.didRequestAppExit()) == AppExitResponse.cancel) {
901904
didCancel = true;

‎packages/flutter/test/widgets/app_lifecycle_listener_test.dart‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
import 'dart:async';
56
import 'dart:ui';
67

78
import 'package:flutter/services.dart';
@@ -203,6 +204,39 @@ void main() {
203204
expect(exitRequested, isTrue);
204205
});
205206

207+
testWidgets('Does not notify listeners disposed while handling an exit request', (
208+
WidgetTester tester,
209+
) async {
210+
final firstRequestStarted = Completer<void>();
211+
final finishFirstRequest = Completer<void>();
212+
var disposedListenerCalled = false;
213+
final firstListener = TestAppLifecycleListener(
214+
binding: WidgetsBinding.instance,
215+
onExitRequested: () async {
216+
firstRequestStarted.complete();
217+
await finishFirstRequest.future;
218+
return AppExitResponse.exit;
219+
},
220+
);
221+
addTearDown(firstListener.dispose);
222+
listener = TestAppLifecycleListener(
223+
binding: WidgetsBinding.instance,
224+
onExitRequested: () async {
225+
disposedListenerCalled = true;
226+
return AppExitResponse.exit;
227+
},
228+
);
229+
230+
final Future<void> exitRequest = sendAppExitRequest();
231+
await firstRequestStarted.future;
232+
listener!.dispose();
233+
listener = null;
234+
finishFirstRequest.complete();
235+
await exitRequest;
236+
237+
expect(disposedListenerCalled, isFalse);
238+
});
239+
206240
test('AppLifecycleListener dispatches memory events', () async {
207241
await expectLater(
208242
await memoryEvents(

0 commit comments

Comments
 (0)