Skip to content

Commit 0c4eb09

Browse files
author
Till Friebe
committed
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 9f03424 commit 0c4eb09

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
@@ -79,8 +80,15 @@ public class ConnectivityPlusPlugin: NSObject, FlutterPlugin, FlutterStreamHandl
7980
}
8081

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

0 commit comments

Comments
 (0)