[webview_flutter_wkwebview] Tear down ProxyAPIRegistrar in applicationWillTerminate#11484
[webview_flutter_wkwebview] Tear down ProxyAPIRegistrar in applicationWillTerminate#11484bparrishMines wants to merge 19 commits intoflutter:mainfrom
applicationWillTerminate#11484Conversation
applicationWillTerminate
There was a problem hiding this comment.
Code Review
This pull request updates the webview_flutter_wkwebview plugin to tear down the proxyApiRegistrar when the application terminates on iOS, preventing message calls during shutdown. The changes include a version bump to 3.24.4, updates to the Xcode project file, and the addition of a lifecycle test. Review feedback identifies a potential crash from force-unwrapping in the teardown method, a missing file reference in the project configuration causing build failures, and a memory leak in the test suite.
| private func tearDownProxyAPIRegistrar() { | ||
| proxyApiRegistrar!.ignoreCallsToDart = true | ||
| proxyApiRegistrar!.tearDown() | ||
| proxyApiRegistrar = nil | ||
| } |
There was a problem hiding this comment.
The proxyApiRegistrar is an optional that is set to nil at the end of this method. Since tearDownProxyAPIRegistrar is now called from both detachFromEngine and applicationWillTerminate, it is possible for it to be called twice during application shutdown (for example, if the engine is detached and then the application terminates). The force-unwrapping of proxyApiRegistrar will cause a crash on the second call. It should be handled safely using a guard or optional chaining.
private func tearDownProxyAPIRegistrar() {
guard let registrar = proxyApiRegistrar else {
return
}
registrar.ignoreCallsToDart = true
registrar.tearDown()
proxyApiRegistrar = nil
}| 8F1488FF2D2DE27000191744 /* NavigationActionProxyAPITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8F1488CB2D2DE27000191744 /* NavigationActionProxyAPITests.swift */; }; | ||
| 8F1489012D2DE91C00191744 /* AuthenticationChallengeResponseProxyAPITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8F1489002D2DE91C00191744 /* AuthenticationChallengeResponseProxyAPITests.swift */; }; | ||
| 8F63D06B2F8812E400EC5076 /* WebViewFlutterPluginTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8F63D06A2F8812E400EC5076 /* WebViewFlutterPluginTests.swift */; }; | ||
| 8F63D06C2F8812E400EC5076 /* PlatformViewImplTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8F63D0692F8812E400EC5076 /* PlatformViewImplTests.swift */; }; |
There was a problem hiding this comment.
| // Attaches an associated object to the InstanceManager to listen for when it is deallocated. | ||
| var finalizer: TestFinalizer? = TestFinalizer() | ||
| objc_setAssociatedObject( | ||
| plugin.proxyApiRegistrar!.instanceManager, malloc(1), finalizer, .OBJC_ASSOCIATION_RETAIN) |
There was a problem hiding this comment.
InstanceManager contains the only strong reference to the finalizer delegate that makes the call to Dart when an object is deallocated. This sets the plugin to listen for
applicationWillTerminateto tear down theProxyAPIRegistrar.Potential fix for flutter/flutter#168535 . This crash is difficult to reproduce, so this is just a attempt to prevent it because most of the stacktraces in the issue include
[UIApplication _terminateWithStatus:]. It's possible this could clean up the plugin before the crash can happen.Pre-Review Checklist
[shared_preferences]///).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. Comments from the
gemini-code-assistbot 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.Footnotes
Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling. ↩ ↩2