Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit ece9858

Browse files
authored
Unblock FlutterResizeSynchronizer on engine shutdown (#24264)
1 parent 6993cb2 commit ece9858

File tree

5 files changed

+34
-5
lines changed

5 files changed

+34
-5
lines changed

shell/platform/darwin/macos/framework/Source/FlutterEngine.mm

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,10 @@ - (void)shutDownEngine {
489489
return;
490490
}
491491

492+
if (_viewController && _viewController.flutterView) {
493+
[_viewController.flutterView shutdown];
494+
}
495+
492496
FlutterEngineResult result = _embedderAPI.Deinitialize(_engine);
493497
if (result != kSuccess) {
494498
NSLog(@"Could not de-initialize the Flutter engine: error %d", result);

shell/platform/darwin/macos/framework/Source/FlutterResizeSynchronizer.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,9 @@
7575
*/
7676
- (void)requestCommit;
7777

78+
/**
79+
* Called when shutting down. Unblocks everything and prevents any further synchronization.
80+
*/
81+
- (void)shutdown;
82+
7883
@end

shell/platform/darwin/macos/framework/Source/FlutterResizeSynchronizer.mm

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ @interface FlutterResizeSynchronizer () {
3434
// Target size for resizing.
3535
CGSize _newSize;
3636

37+
// if YES prevents all synchronization
38+
BOOL _shuttingDown;
39+
3740
__weak id<FlutterResizeSynchronizerDelegate> _delegate;
3841
}
3942
@end
@@ -54,7 +57,7 @@ - (void)beginResize:(CGSize)size notify:(dispatch_block_t)notify {
5457
return;
5558
}
5659

57-
if (!_receivedFirstFrame) {
60+
if (!_receivedFirstFrame || _shuttingDown) {
5861
// No blocking until framework produces at least one frame
5962
notify();
6063
return;
@@ -77,7 +80,7 @@ - (void)beginResize:(CGSize)size notify:(dispatch_block_t)notify {
7780

7881
_waiting = YES;
7982

80-
_condBlockRequestCommit.wait(lock, [&] { return _pendingCommit; });
83+
_condBlockRequestCommit.wait(lock, [&] { return _pendingCommit || _shuttingDown; });
8184

8285
[_delegate resizeSynchronizerFlush:self];
8386
[_delegate resizeSynchronizerCommit:self];
@@ -104,7 +107,7 @@ - (BOOL)shouldEnsureSurfaceForSize:(CGSize)size {
104107

105108
- (void)requestCommit {
106109
std::unique_lock<std::mutex> lock(_mutex);
107-
if (!_acceptingCommit) {
110+
if (!_acceptingCommit || _shuttingDown) {
108111
return;
109112
}
110113

@@ -113,7 +116,7 @@ - (void)requestCommit {
113116
_pendingCommit = YES;
114117
if (_waiting) { // BeginResize is in progress, interrupt it and schedule commit call
115118
_condBlockRequestCommit.notify_all();
116-
_condBlockBeginResize.wait(lock, [&]() { return !_pendingCommit; });
119+
_condBlockBeginResize.wait(lock, [&]() { return !_pendingCommit || _shuttingDown; });
117120
} else {
118121
// No resize, schedule commit on platform thread and wait until either done
119122
// or interrupted by incoming BeginResize
@@ -128,8 +131,15 @@ - (void)requestCommit {
128131
_condBlockBeginResize.notify_all();
129132
}
130133
});
131-
_condBlockBeginResize.wait(lock, [&]() { return !_pendingCommit; });
134+
_condBlockBeginResize.wait(lock, [&]() { return !_pendingCommit || _shuttingDown; });
132135
}
133136
}
134137

138+
- (void)shutdown {
139+
std::unique_lock<std::mutex> lock(_mutex);
140+
_shuttingDown = YES;
141+
_condBlockBeginResize.notify_all();
142+
_condBlockRequestCommit.notify_all();
143+
}
144+
135145
@end

shell/platform/darwin/macos/framework/Source/FlutterView.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,10 @@
5656
*/
5757
- (nonnull FlutterRenderBackingStore*)backingStoreForSize:(CGSize)size;
5858

59+
/**
60+
* Must be called when shutting down. Unblocks raster thread and prevents any further
61+
* synchronization.
62+
*/
63+
- (void)shutdown;
64+
5965
@end

shell/platform/darwin/macos/framework/Source/FlutterView.mm

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,8 @@ - (void)viewDidChangeBackingProperties {
124124
[_reshapeListener viewDidReshape:self];
125125
}
126126

127+
- (void)shutdown {
128+
[_resizeSynchronizer shutdown];
129+
}
130+
127131
@end

0 commit comments

Comments
 (0)