[FSSDK-12503] fix: guard detachFromEngine from multi-engine channel teardown#105
Merged
muzahidul-opti merged 1 commit intomasterfrom Apr 30, 2026
Merged
Conversation
…engine (FSSDK-12503) onDetachedFromEngine/detachFromEngine was unconditionally clearing the static MethodChannel, even when a secondary engine (e.g. Firebase background messaging) detached. This destroyed the primary engine's channel and broke all SDK calls and notification callbacks. Store the BinaryMessenger that created the channel during attach, and only clear the channel in detach if the detaching engine's messenger matches the stored one. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
raju-opti
approved these changes
Apr 30, 2026
There was a problem hiding this comment.
Pull request overview
Guards native plugin teardown so a secondary Flutter engine (e.g., background messaging) detaching doesn’t destroy the primary engine’s MethodChannel and break SDK calls/callbacks.
Changes:
- iOS: Track the messenger used to create the MethodChannel and skip
detachFromEnginecleanup when a different engine is detaching. - Android: Track the
BinaryMessengerthat created the MethodChannel and skiponDetachedFromEnginecleanup when the detaching engine uses a different messenger.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| ios/Classes/SwiftOptimizelyFlutterSdkPlugin.swift | Stores the attached messenger and gates channel cleanup in detachFromEngine based on messenger identity. |
| android/src/main/java/com/optimizely/optimizely_flutter_sdk/OptimizelyFlutterSdkPlugin.java | Stores the attached BinaryMessenger and gates channel cleanup in onDetachedFromEngine based on messenger identity. |
Comments suppressed due to low confidence (2)
ios/Classes/SwiftOptimizelyFlutterSdkPlugin.swift:56
- In
register(with:), you now storelet messenger = registrar.messenger(), but the logger channel setup still callsregistrar.messenger()again. Reuse themessengervariable (including formakeBackgroundTaskQueue) so both channels are guaranteed to use the same messenger instance and to avoid redundant lookups.
let messenger = registrar.messenger()
attachedMessenger = messenger
channel = FlutterMethodChannel(name: "optimizely_flutter_sdk", binaryMessenger: messenger)
let instance = SwiftOptimizelyFlutterSdkPlugin()
registrar.addMethodCallDelegate(instance, channel: channel)
// Separate logger channel for outgoing log calls
let taskQueue = registrar.messenger().makeBackgroundTaskQueue?()
let loggerChannel = FlutterMethodChannel(name: OptimizelyFlutterLogger.LOGGER_CHANNEL,
binaryMessenger: registrar.messenger(),
codec: FlutterStandardMethodCodec.sharedInstance(),
taskQueue: taskQueue)
android/src/main/java/com/optimizely/optimizely_flutter_sdk/OptimizelyFlutterSdkPlugin.java:226
onAttachedToEnginestores the engine messenger inattachedMessenger, but the logger channel is still created with a freshbinding.getBinaryMessenger()call. Prefer usingattachedMessengerfor the logger channel as well to ensure both channels are tied to the same messenger instance and to avoid unnecessary duplicate lookups.
attachedMessenger = binding.getBinaryMessenger();
channel = new MethodChannel(attachedMessenger, "optimizely_flutter_sdk");
channel.setMethodCallHandler(this);
context = binding.getApplicationContext();
MethodChannel loggerChannel = new MethodChannel(binding.getBinaryMessenger(), FlutterLogbackAppender.CHANNEL_NAME);
FlutterLogbackAppender.setChannel(loggerChannel);
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
onDetachedFromEnginenow checks if the detaching engine'sBinaryMessengermatches the one that created the channel — skips cleanup if it doesn't matchdetachFromEnginenow comparesregistrar.messenger()against the storedattachedMessenger— skips cleanup if they differContext
The
onAttachedToEngine/registerfix from #103 correctly guards against channel overwrite on attach (if channel != null { return }). ButonDetachedFromEngine/detachFromEngineunconditionally nils the channel — so when the secondary engine detaches, it destroys the primary engine's channel.Test plan
🤖 Generated with Claude Code