Skip to content

Commit f1a74f4

Browse files
authored
Fix the issue on macOS where, after a hot restart with multiple windows, unresponsive windows are left behind. (flutter#180287)
There is currently an issue with multiple windows on macOS: after a hot restart, new windows are created, but the original windows are not closed. You can reproduce this by running examples/multiple_windows and then performing a hot restart. ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [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. - [ ] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [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/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent 6d4aea2 commit f1a74f4

4 files changed

Lines changed: 47 additions & 12 deletions

File tree

engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,6 @@ - (void)sendUserLocales;
152152
*/
153153
- (void)engineCallbackOnPlatformMessage:(const FlutterPlatformMessage*)message;
154154

155-
/**
156-
* Invoked right before the engine is restarted.
157-
*
158-
* This should reset states to as if the application has just started. It
159-
* usually indicates a hot restart (Shift-R in Flutter CLI.)
160-
*/
161-
- (void)engineCallbackOnPreEngineRestart;
162-
163155
/**
164156
* Requests that the task be posted back the to the Flutter engine at the target time. The target
165157
* time is in the clock used by the Flutter engine.
@@ -1289,6 +1281,7 @@ - (void)engineCallbackOnPreEngineRestart {
12891281
while ((nextViewController = [viewControllerEnumerator nextObject])) {
12901282
[nextViewController onPreEngineRestart];
12911283
}
1284+
[_windowController closeAllWindows];
12921285
[_platformViewController reset];
12931286
_keyboardManager = [[FlutterKeyboardManager alloc] initWithDelegate:self];
12941287
}

engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEngine_Internal.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,13 @@ typedef NS_ENUM(NSInteger, FlutterAppExitResponse) {
268268
*/
269269
+ (nullable FlutterEngine*)engineForIdentifier:(int64_t)identifier;
270270

271+
/**
272+
* Invoked right before the engine is restarted.
273+
*
274+
* This should reset states to as if the application has just started. It
275+
* usually indicates a hot restart (Shift-R in Flutter CLI.)
276+
*/
277+
- (void)engineCallbackOnPreEngineRestart;
271278
@end
272279

273280
NS_ASSUME_NONNULL_END

engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterWindowController.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@
1616

1717
@property(nonatomic, weak) FlutterEngine* engine;
1818

19-
@end
20-
21-
@interface FlutterWindowController (Testing)
22-
2319
- (void)closeAllWindows;
2420

2521
@end

engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterWindowControllerTest.mm

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,4 +202,43 @@ void TearDown() {
202202
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.5, false);
203203
EXPECT_EQ(window.miniaturized, YES);
204204
}
205+
206+
TEST_F(FlutterWindowControllerTest, ClosesAllWindowsOnEngineRestart) {
207+
FlutterWindowCreationRequest request{
208+
.has_size = true,
209+
.size = {.width = 800, .height = 600},
210+
.on_should_close = [] {},
211+
.on_will_close = [] {},
212+
.notify_listeners = [] {},
213+
};
214+
215+
FlutterEngine* engine = GetFlutterEngine();
216+
int64_t engine_id = reinterpret_cast<int64_t>(engine);
217+
218+
IsolateScope isolate_scope(isolate());
219+
220+
// Create multiple windows
221+
int64_t handle1 = InternalFlutter_WindowController_CreateRegularWindow(engine_id, &request);
222+
int64_t handle2 = InternalFlutter_WindowController_CreateRegularWindow(engine_id, &request);
223+
int64_t handle3 = InternalFlutter_WindowController_CreateRegularWindow(engine_id, &request);
224+
225+
// Verify windows are created
226+
FlutterViewController* viewController1 = [engine viewControllerForIdentifier:handle1];
227+
FlutterViewController* viewController2 = [engine viewControllerForIdentifier:handle2];
228+
FlutterViewController* viewController3 = [engine viewControllerForIdentifier:handle3];
229+
EXPECT_NE(viewController1, nil);
230+
EXPECT_NE(viewController2, nil);
231+
EXPECT_NE(viewController3, nil);
232+
233+
// Close all windows on engine restart
234+
[engine engineCallbackOnPreEngineRestart];
235+
236+
// Verify all windows are closed and view controllers are disposed
237+
viewController1 = [engine viewControllerForIdentifier:handle1];
238+
viewController2 = [engine viewControllerForIdentifier:handle2];
239+
viewController3 = [engine viewControllerForIdentifier:handle3];
240+
EXPECT_EQ(viewController1, nil);
241+
EXPECT_EQ(viewController2, nil);
242+
EXPECT_EQ(viewController3, nil);
243+
}
205244
} // namespace flutter::testing

0 commit comments

Comments
 (0)