Skip to content

Commit 1e29576

Browse files
Till Friebevbuberen
authored andcommitted
fix(connectivity_plus): guard eventSink against FlutterEngine teardown on iOS
NWPathMonitor can fire a connectivity update while the app is in the background, after the FlutterEngine's shell has been torn down. The DispatchQueue.main.async block then invokes the stale FlutterEventSink, which calls -[FlutterEngine sendOnChannel:] and hits an NSAssertion at FlutterEngine.mm:1315, aborting the process with SIGABRT. Guard the main-queue emission with [weak self], a local eventSink copy, and a UIApplication.applicationState != .background check so that pending updates are dropped instead of propagated into a dead engine. The next onListen / check call after foregrounding will resync state.
1 parent 4c623ae commit 1e29576

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

packages/connectivity_plus/connectivity_plus/ios/connectivity_plus/Sources/connectivity_plus/ConnectivityPlusPlugin.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// be found in the LICENSE file.
44

55
import Flutter
6+
import UIKit
67

78
public class ConnectivityPlusPlugin: NSObject, FlutterPlugin, FlutterStreamHandler {
89
private let connectivityProvider: ConnectivityProvider
@@ -81,8 +82,15 @@ public class ConnectivityPlusPlugin: NSObject, FlutterPlugin, FlutterStreamHandl
8182
}
8283

8384
private func connectivityUpdateHandler(connectivityTypes: [ConnectivityType]) {
84-
DispatchQueue.main.async {
85-
self.eventSink?(self.statusFrom(connectivityTypes: connectivityTypes))
85+
DispatchQueue.main.async { [weak self] in
86+
guard let self = self, let eventSink = self.eventSink else { return }
87+
// NWPathMonitor can fire while the app is in the background, after the
88+
// FlutterEngine's shell has been torn down. Calling eventSink in that
89+
// state triggers an NSAssertion in -[FlutterEngine sendOnChannel:] and
90+
// crashes the app with SIGABRT. Skip the emission until the app is
91+
// foregrounded again; the next onListen / check call will resync state.
92+
guard UIApplication.shared.applicationState != .background else { return }
93+
eventSink(self.statusFrom(connectivityTypes: connectivityTypes))
8694
}
8795
}
8896

0 commit comments

Comments
 (0)