Skip to content

Commit 85e1638

Browse files
fix: guard detachFromEngine to only clear channel owned by detaching engine (FSSDK-12503) (#105)
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>
1 parent 9da6774 commit 85e1638

2 files changed

Lines changed: 17 additions & 3 deletions

File tree

android/src/main/java/com/optimizely/optimizely_flutter_sdk/OptimizelyFlutterSdkPlugin.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import io.flutter.embedding.engine.plugins.FlutterPlugin;
2121
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
22+
import io.flutter.plugin.common.BinaryMessenger;
2223
import io.flutter.plugin.common.MethodCall;
2324
import io.flutter.plugin.common.MethodChannel;
2425
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
@@ -47,6 +48,7 @@
4748
public class OptimizelyFlutterSdkPlugin extends OptimizelyFlutterClient implements FlutterPlugin, ActivityAware, MethodCallHandler {
4849

4950
public static MethodChannel channel;
51+
private static BinaryMessenger attachedMessenger;
5052
private Appender<ILoggingEvent> flutterLogbackAppender;
5153

5254
/**
@@ -215,7 +217,8 @@ public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) {
215217
if (channel != null) {
216218
return;
217219
}
218-
channel = new MethodChannel(binding.getBinaryMessenger(), "optimizely_flutter_sdk");
220+
attachedMessenger = binding.getBinaryMessenger();
221+
channel = new MethodChannel(attachedMessenger, "optimizely_flutter_sdk");
219222
channel.setMethodCallHandler(this);
220223
context = binding.getApplicationContext();
221224

@@ -234,8 +237,12 @@ public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) {
234237

235238
@Override
236239
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
240+
if (binding.getBinaryMessenger() != attachedMessenger) {
241+
return;
242+
}
237243
channel.setMethodCallHandler(null);
238244
channel = null;
245+
attachedMessenger = null;
239246
// Stop and detach the appender
240247
if (flutterLogbackAppender != null) {
241248
Logger rootLogger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);

ios/Classes/SwiftOptimizelyFlutterSdkPlugin.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ public class SwiftOptimizelyFlutterSdkPlugin: NSObject, FlutterPlugin {
3030

3131
// to communicate with optimizely flutter sdk
3232
static var channel: FlutterMethodChannel!
33-
33+
private static weak var attachedMessenger: FlutterBinaryMessenger?
34+
3435
// to track each unique userContext
3536
var uuid: String {
3637
return UUID().uuidString
@@ -41,7 +42,9 @@ public class SwiftOptimizelyFlutterSdkPlugin: NSObject, FlutterPlugin {
4142
if channel != nil {
4243
return
4344
}
44-
channel = FlutterMethodChannel(name: "optimizely_flutter_sdk", binaryMessenger: registrar.messenger())
45+
let messenger = registrar.messenger()
46+
attachedMessenger = messenger
47+
channel = FlutterMethodChannel(name: "optimizely_flutter_sdk", binaryMessenger: messenger)
4548
let instance = SwiftOptimizelyFlutterSdkPlugin()
4649
registrar.addMethodCallDelegate(instance, channel: channel)
4750

@@ -55,8 +58,12 @@ public class SwiftOptimizelyFlutterSdkPlugin: NSObject, FlutterPlugin {
5558
}
5659

5760
public func detachFromEngine(for registrar: FlutterPluginRegistrar) {
61+
guard registrar.messenger() === Self.attachedMessenger else {
62+
return
63+
}
5864
Self.channel?.setMethodCallHandler(nil)
5965
Self.channel = nil
66+
Self.attachedMessenger = nil
6067
OptimizelyFlutterLogger.clearChannel()
6168
}
6269

0 commit comments

Comments
 (0)