Skip to content

Commit a378299

Browse files
[pigeon] Fix race in event channel handler removal integration test (#13528)
**This PR fixes the tree.** `Linux_android custom_package_tests master` is failing on `main` (all 3 attempts of [#4084](https://ci.chromium.org/p/flutter/builders/prod/Linux_android%20custom_package_tests%20master/4084)) in the Pigeon integration test `event channels handle multiple instances`: ``` The following MissingPluginException was thrown while de-activating platform stream on channel dev.flutter.pigeon.pigeon_integration_tests.EventChannelMethods.streamConsistentNumbers.1: MissingPluginException(No implementation found for method cancel on channel dev.flutter.pigeon.pigeon_integration_tests.EventChannelMethods.streamConsistentNumbers.1) ``` ### Cause The test plugin unregisters the stream handler for instance `"1"` (to exercise event channel unregistration, added in #13438) immediately after calling `endOfStream()`. On the Dart side, the end of the stream closes the `EventChannel`'s broadcast stream, which then sends a `cancel` method call back on the same channel. If the handler has already been removed by the time that `cancel` call arrives, it fails with a `MissingPluginException`, which fails the test. Whether it fails depends on timing, so it shows up as a flake. ### Fix Unregister the handler from the stream handler's `onCancel` instead, i.e. only after the `cancel` call has been received, in both the Kotlin (Android) and Swift (iOS/macOS) test plugins. The generated Pigeon stream handler wrappers already forward the platform `onCancel` to the user handler, so no generator or generated-code changes are needed. The test still exercises unregistration; it just no longer races with the channel teardown. This only changes test plugin code under `platform_tests/`, so there is no change to the published package. ### Testing On an Android emulator (Pixel 9a), running only `event channels handle multiple instances`: | | Runs | Failures (`MissingPluginException` on `cancel`) | |---|---|---| | Without this fix (`main`) | 5 | 2 | | With this fix | 10 | 0 | The full `android_kotlin_integration_tests` suite (680 tests) also passes with this fix, as do `dart_unittests` and `flutter_unittests`. The Swift change mirrors the Kotlin one and is covered by the iOS/macOS integration tests in CI. ### Related flutter/flutter#179154 tracks a different flake in the same test on macOS Swift (`Bad state: Cannot add new events after calling close`, i.e. an event sent after `endOfStream`). This PR does not address that one, and the macOS Swift skip is left in place. ## Pre-Review Checklist [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent 3f34a6f commit a378299

2 files changed

Lines changed: 20 additions & 6 deletions

File tree

  • packages/pigeon/platform_tests/test_plugin

‎packages/pigeon/platform_tests/test_plugin/android/src/main/kotlin/com/example/test_plugin/TestPlugin.kt‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class TestPlugin : FlutterPlugin, HostIntegrationCoreApi, HostCallbackCoreApi {
6666
eventChannelOne =
6767
StreamConsistentNumbersStreamHandler.register(
6868
binding.binaryMessenger, streamHandlerOne, "1")
69-
streamHandlerOne.onCompleted = { eventChannelOne?.setStreamHandler(null) }
69+
streamHandlerOne.onCancelled = { eventChannelOne?.setStreamHandler(null) }
7070
StreamConsistentNumbersStreamHandler.register(
7171
binding.binaryMessenger, SendConsistentNumbers(2), "2")
7272
}
@@ -2332,7 +2332,7 @@ object SendClass : StreamEventsStreamHandler() {
23322332
class SendConsistentNumbers(private val numberToSend: Long) :
23332333
StreamConsistentNumbersStreamHandler() {
23342334
private val handler = Handler(Looper.getMainLooper())
2335-
var onCompleted: (() -> Unit)? = null
2335+
var onCancelled: (() -> Unit)? = null
23362336

23372337
override fun onListen(p0: Any?, sink: PigeonEventSink<Long>) {
23382338
var count = 0
@@ -2341,7 +2341,6 @@ class SendConsistentNumbers(private val numberToSend: Long) :
23412341
override fun run() {
23422342
if (count >= 10) {
23432343
sink.endOfStream()
2344-
onCompleted?.invoke()
23452344
} else {
23462345
handler.post {
23472346
sink.success(numberToSend)
@@ -2353,4 +2352,12 @@ class SendConsistentNumbers(private val numberToSend: Long) :
23532352
}
23542353
handler.postDelayed(r, 10)
23552354
}
2355+
2356+
// Closing the stream causes the Dart side to send a "cancel" call, so any
2357+
// handler removal must wait until that call has been received. Removing the
2358+
// handler immediately after endOfStream races with the "cancel" call, which
2359+
// then fails with a MissingPluginException.
2360+
override fun onCancel(p0: Any?) {
2361+
onCancelled?.invoke()
2362+
}
23562363
}

‎packages/pigeon/platform_tests/test_plugin/darwin/test_plugin/Sources/test_plugin/TestPlugin.swift‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public class TestPlugin: NSObject, FlutterPlugin, HostIntegrationCoreApi, HostCa
6868
with: binaryMessenger,
6969
instanceName: "1",
7070
streamHandler: streamHandlerOne)
71-
streamHandlerOne.onCompleted = { channelOne.setStreamHandler(nil) }
71+
streamHandlerOne.onCancelled = { channelOne.setStreamHandler(nil) }
7272
StreamConsistentNumbersStreamHandler.register(
7373
with: binaryMessenger,
7474
instanceName: "2",
@@ -2266,7 +2266,7 @@ class SendEvents: StreamEventsStreamHandler {
22662266

22672267
class SendConsistentNumbers: StreamConsistentNumbersStreamHandler {
22682268
let numberToSend: Int64
2269-
var onCompleted: (() -> Void)?
2269+
var onCancelled: (() -> Void)?
22702270
init(numberToSend: Int64) {
22712271
self.numberToSend = numberToSend
22722272
}
@@ -2285,10 +2285,17 @@ class SendConsistentNumbers: StreamConsistentNumbersStreamHandler {
22852285
if count >= 10 {
22862286
sink.endOfStream()
22872287
self.timer?.invalidate()
2288-
self.onCompleted?()
22892288
}
22902289
}
22912290
}
22922291
}
22932292
}
2293+
2294+
// Closing the stream causes the Dart side to send a "cancel" call, so any
2295+
// handler removal must wait until that call has been received. Removing the
2296+
// handler immediately after endOfStream races with the "cancel" call, which
2297+
// then fails with a MissingPluginException.
2298+
override func onCancel(withArguments arguments: Any?) {
2299+
onCancelled?()
2300+
}
22942301
}

0 commit comments

Comments
 (0)