Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate FlutterCallbackCache and FlutterKeyboardManager to ARC #51983

Merged
merged 6 commits into from
Apr 15, 2024

Conversation

jmagman
Copy link
Member

@jmagman jmagman commented Apr 9, 2024

Smart pointers support ARC as of #47612, and the unit tests were migrated in #48162.

MigrateFlutterCallbackCache and FlutterKeyboardManager from MRC to ARC. These files do not themselves import any MRC files, making them leaf nodes in the dependency graph of MRC files.

Doing a few at a time to make the dependency graph manageable, and to easily revert if this causes retain cycles or other memory management issues.

Part of flutter/flutter#137801.

@jmagman jmagman self-assigned this Apr 9, 2024
@flutter-dashboard
Copy link

It looks like this pull request may not have tests. Please make sure to add tests before merging. If you need an exemption to this rule, contact "@test-exemption-reviewer" in the #hackers channel in Chat (don't just cc them here, they won't see it! Use Discord!).

If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix?

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing.

@jmagman jmagman marked this pull request as draft April 9, 2024 05:39
@@ -89,7 +76,7 @@ - (void)handlePress:(nonnull FlutterUIPressProxy*)press
// encounter.
NSAssert([_primaryResponders count] >= 0, @"At least one primary responder must be added.");

__block auto weakSelf = [self getWeakNSObject];
__block __weak __typeof(self) weakSelf = self;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the WeakNSObjectFactory in favor of __weak where it's used before the block. I can swap __typeof(self) to FlutterKeyboardManager* I don't really have a preference.

@@ -168,7 +168,7 @@ - (void)testResize {
[binaryMessenger stopMocking];
}

- (bool)testSetWarnsOnOverflow {
- (void)testSetWarnsOnOverflow {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated, but noticed this wasn't running because the signature wasn't void.

} // namespace

// These tests were designed to run on iOS 13.4 or later.
API_AVAILABLE(ios(13.4))
Copy link
Member Author

@jmagman jmagman Apr 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Putting the availability on the class FlutterKeyboardManagerTest means it won't need to be checked in setUp or decorated on the methods.

@property(nonatomic, assign) BOOL didCallNotifyLowMemory;
@end

@interface FlutterEngine ()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dead code.

using namespace flutter::testing;

/// Sometimes we have to use a custom mock to avoid retain cycles in ocmock.
@interface FlutterEnginePartialMock : FlutterEngine
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dead code.

[super tearDown];
}

- (id)checkKeyDownEvent:(UIKeyboardHIDUsage)keyCode API_AVAILABLE(ios(13.4)) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dead code.

@interface FlutterKeyboardManagerTest : XCTestCase
@property(nonatomic, strong) id mockEngine;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved this into a local in the one remaining spot the engine is mocked.


// Verify that the nextResponder returned from mockOwnerWithPressesBeginOnlyNext()
// throws exception when pressesEnded is called.
- (bool)testNextResponderShouldThrowOnPressesEnded API_AVAILABLE(ios(13.4)) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This wasn't actually running because the return type wasn't void.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we need a custom CI check on this? It seems like anything in Test.{m,mm} that starts - (anything but void)test is probably wrong.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jmagman jmagman force-pushed the cache-keyboard-arc branch 2 times, most recently from 46815b3 to 0c8d245 Compare April 11, 2024 03:17
@jmagman jmagman marked this pull request as ready for review April 11, 2024 03:53
// It will be destroyed and invalidate its weak pointers
// before any other members are destroyed.
_weakFactory.reset();

[_primaryResponders removeAllObjects];
[_secondaryResponders removeAllObjects];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would we need to remove everything from private properties in dealloc? Is there some way we are vending outside references to these arrays?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me check.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it would be necessary, this is the only place -addPrimaryResponder and -addSecondaryResponder are called:

[self.keyboardManager addPrimaryResponder:[[[FlutterEmbedderKeyResponder alloc]
initWithSendEvent:sendEvent] autorelease]];
FlutterChannelKeyResponder* responder = [[[FlutterChannelKeyResponder alloc]
initWithChannel:self.engine.keyEventChannel] autorelease];
[self.keyboardManager addPrimaryResponder:responder];
FlutterTextInputPlugin* textInputPlugin = self.engine.textInputPlugin;
if (textInputPlugin != nil) {
[self.keyboardManager addSecondaryResponder:textInputPlugin];
}

I'll remove.

FlutterUIPressProxy* press;
FlutterAsyncKeyCallback callback;
__unsafe_unretained FlutterUIPressProxy* press;
__unsafe_unretained FlutterAsyncKeyCallback callback;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems scary. Why are we passing unsafe unretained pointers to an async block? It seems like this code was probably just wrong before?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's needed for ARC NSInvocation calls, that's the only place I've regularly seen __unsafe_unretained used

https://developer.apple.com/documentation/foundation/nsinvocation/1437830-getargument#discussion

__unsafe_unretained id tempObject = nil;
[myInvocation getArgument: &tempObject atIndex: 2];
self.myObject = tempObject;
NSLog(@"Argument 0 was: %@", self.myObject); // prints "A string"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that the next thing we do is pass the arguments into an async block, I think we are missing a critical intermediate step:

You can then copy this into another form of storage, like a strongly-held property.

I know this is pre-existing, but it seems like a very likely source of flaky test crashing, so we should fix it while we are here. We can rename these something like pressUnsafe and callbackUnsafe and then do

FlutterUIPressProxy* press = pressUnsafe;
FlutterAsyncKeyCallback callback = callbackUnsafe;

That way we won't be trying to run an async block with pointers into memory owned by an invocation that's almost certainly out of scope. I'm pretty surprised that the current code is ever working.


// Verify that the nextResponder returned from mockOwnerWithPressesBeginOnlyNext()
// throws exception when pressesEnded is called.
- (bool)testNextResponderShouldThrowOnPressesEnded API_AVAILABLE(ios(13.4)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we need a custom CI check on this? It seems like anything in Test.{m,mm} that starts - (anything but void)test is probably wrong.

Copy link
Contributor

@stuartmorgan stuartmorgan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks for the fixes!

@jmagman jmagman added the autosubmit Merge PR when tree becomes green via auto submit App label Apr 12, 2024
Copy link
Contributor

auto-submit bot commented Apr 12, 2024

auto label is removed for flutter/engine/51983, due to Pull request flutter/engine/51983 is not in a mergeable state.

@auto-submit auto-submit bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Apr 12, 2024
@jmagman jmagman force-pushed the cache-keyboard-arc branch from 92daaec to 25bfa11 Compare April 12, 2024 20:24
Copy link
Member

@cbracken cbracken left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Also nice catch on the dead code. Wonder how much else we have floating around :/

@jmagman jmagman added the autosubmit Merge PR when tree becomes green via auto submit App label Apr 13, 2024
@auto-submit auto-submit bot merged commit a504499 into flutter:main Apr 15, 2024
28 checks passed
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Apr 15, 2024
@jmagman jmagman deleted the cache-keyboard-arc branch April 15, 2024 19:24
auto-submit bot pushed a commit to flutter/flutter that referenced this pull request Apr 15, 2024
…146776)

flutter/engine@84238c4...07ae93c

2024-04-15 [email protected] Roll Dart SDK from ac31be3c8546 to 3c2376cb9850 (1 revision) (flutter/engine#52095)
2024-04-15 [email protected] Migrate FlutterCallbackCache and FlutterKeyboardManager to ARC (flutter/engine#51983)
2024-04-15 [email protected] Roll buildroot and set ios_use_simulator variable used by Skia GN scripts (flutter/engine#52101)

If this roll has caused a breakage, revert this CL and stop the roller
using the controls here:
https://autoroll.skia.org/r/flutter-engine-flutter-autoroll
Please CC [email protected],[email protected],[email protected] on the revert to ensure that a human
is aware of the problem.

To file a bug in Flutter: https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://issues.skia.org/issues/new?component=1389291&template=1850622

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
gilnobrega pushed a commit to gilnobrega/flutter that referenced this pull request Apr 22, 2024
…lutter#146776)

flutter/engine@84238c4...07ae93c

2024-04-15 [email protected] Roll Dart SDK from ac31be3c8546 to 3c2376cb9850 (1 revision) (flutter/engine#52095)
2024-04-15 [email protected] Migrate FlutterCallbackCache and FlutterKeyboardManager to ARC (flutter/engine#51983)
2024-04-15 [email protected] Roll buildroot and set ios_use_simulator variable used by Skia GN scripts (flutter/engine#52101)

If this roll has caused a breakage, revert this CL and stop the roller
using the controls here:
https://autoroll.skia.org/r/flutter-engine-flutter-autoroll
Please CC [email protected],[email protected],[email protected] on the revert to ensure that a human
is aware of the problem.

To file a bug in Flutter: https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://issues.skia.org/issues/new?component=1389291&template=1850622

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
affects: desktop autosubmit Merge PR when tree becomes green via auto submit App platform-ios platform-macos
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants