Skip to content

Commit 5358e59

Browse files
committed
iOS: Add null-check for touch rate correction task runner
`FlutterViewController.viewDidLoad` creates a vsync client to correct the rate at which touch events are delivered on ProMotion displays, and passes it `self.engine.platformTaskRunner`. That property is nullable and only set once the engine's shell is created, then cleared again in `destroyContext`. `FlutterVSyncClient` requires a non-null task runner and dereferences it. A view controller whose engine is nil would dereference null and takes the app down with it, at launch. We now bail out when there's no task runner. The argument is that losing whatever smoothing effect this has while we have no running egnine has no effect, and either way it's better than a null deref. There's the separate argument detailed in flutter#191199 that this entire client isn't really doing anything positive and should be deleted. This simply prevents the null deref until that work can land. Also worth noting that `platformTaskRunner` is declared `nullable` and the Swift-generated `initWithTaskRunner:` takes a `_Nonnull` task runner, so clang spotted this issue when I enabled `-Wnullable-to-nonnull-conversion`: ``` FlutterViewController.mm:1332:36: warning: implicit conversion from nullable pointer 'FlutterFMLTaskRunner * _Nullable' to non-nullable pointer type 'FlutterFMLTaskRunner * _Nonnull' [-Wnullable-to-nonnull-conversion] ``` I'll follow up with a patch that enables that warning, but it requires a few other cleanups first. Issue: flutter#190030
1 parent c842d69 commit 5358e59

2 files changed

Lines changed: 38 additions & 1 deletion

File tree

‎engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1324,12 +1324,22 @@ - (void)createTouchRateCorrectionVSyncClientIfNeeded {
13241324
return;
13251325
}
13261326

1327+
FlutterFMLTaskRunner* platformTaskRunner = self.engine.platformTaskRunner;
1328+
if (platformTaskRunner == nil) {
1329+
// The engine has no platform task runner until its shell is created, and the vsync client
1330+
// dereferences the task runner we hand it. Losing the smoothing the correction client provides
1331+
// is better than dereferencing null.
1332+
[FlutterLogger logWarning:@"Unable to correct touch rate: the engine has no platform task "
1333+
@"runner."];
1334+
return;
1335+
}
1336+
13271337
void (^callback)(CFTimeInterval, CFTimeInterval) =
13281338
^(CFTimeInterval startTime, CFTimeInterval targetTime) {
13291339
// Do nothing in this block. Just trigger system to callback touch events with correct rate.
13301340
};
13311341
_touchRateCorrectionVSyncClient = [[FlutterVSyncClient alloc]
1332-
initWithTaskRunner:self.engine.platformTaskRunner
1342+
initWithTaskRunner:platformTaskRunner
13331343
isVariableRefreshRateEnabled:self.displayLinkManager.maxRefreshRateEnabledOnIPhone
13341344
maxRefreshRate:self.displayLinkManager.displayRefreshRate
13351345
callback:callback];

‎engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewControllerTest.mm‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2887,6 +2887,33 @@ - (void)testCreateTouchRateCorrectionVSyncClientWillNotCreateNewVSyncClientWhenC
28872887
XCTAssertTrue(clientBefore == clientAfter);
28882888
}
28892889

2890+
// Verifies that no touch rate correction vsync client is created when the engine has no platform
2891+
// task runner. The task runner is nil before the engine's shell is created and after its context is
2892+
// destroyed, and the vsync client dereferences the task runner it is given.
2893+
//
2894+
// A view controller cannot attach to an engine that has no shell, so the engine is run and its
2895+
// context destroyed afterwards rather than simply left uninitialized.
2896+
- (void)testCreateTouchRateCorrectionVSyncClientWillNotCreateVsyncClientWithoutTaskRunner {
2897+
id mockDisplayLinkManager = OCMPartialMock([FlutterDisplayLinkManager shared]);
2898+
[self addTeardownBlock:^{
2899+
[mockDisplayLinkManager stopMocking];
2900+
}];
2901+
double maxFrameRate = 120;
2902+
(void)[[[mockDisplayLinkManager stub] andReturnValue:@(maxFrameRate)] displayRefreshRate];
2903+
2904+
FlutterEngine* engine = [[FlutterEngine alloc] init];
2905+
[engine runWithEntrypoint:nil];
2906+
FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engine
2907+
nibName:nil
2908+
bundle:nil];
2909+
[engine destroyContext];
2910+
XCTAssertNil(engine.platformTaskRunner);
2911+
2912+
// Verify the client is nil, and we don't crash.
2913+
[viewController createTouchRateCorrectionVSyncClientIfNeeded];
2914+
XCTAssertNil(viewController.touchRateCorrectionVSyncClient);
2915+
}
2916+
28902917
- (void)testCreateTouchRateCorrectionVSyncClientWillNotCreateVsyncClientWhenRefreshRateIs60HZ {
28912918
id mockDisplayLinkManager = OCMPartialMock([FlutterDisplayLinkManager shared]);
28922919
[self addTeardownBlock:^{

0 commit comments

Comments
 (0)