emqx / CocoaMQTT

MQTT 5.0 client library for iOS and macOS written in Swift
https://www.emqx.com/en
Other
1.57k stars 411 forks source link

Priority Inversion Crash #580

Open AdamHeavens opened 3 months ago

AdamHeavens commented 3 months ago

Hello,

I have the following MqttManager class that seems to crash the process with the following error

`Thread Performance Checker: Thread running at User-interactive quality-of-service class waiting on a lower QoS thread running at Default quality-of-service class. Investigate ways to avoid priority inversions PID: 64144, TID: 3923284 Backtrace

3 Foundation 0x0000000180e0f1cc -[_NSThreadPerformInfo wait] + 40 4 Foundation 0x0000000180e1127c -[NSObject(NSThreadPerformAdditions) performSelector:onThread:withObject:waitUntilDone:modes:] + 596 5 Foundation 0x0000000180e114e4 -[NSObject(NSThreadPerformAdditions) performSelector:onThread:withObject:waitUntilDone:] + 104 6 MqttCocoaAsyncSocket 0x0000000105eeece0 43-[MGCDAsyncSocket removeStreamsFromRunLoop]_block_invoke + 96 7 libdispatch.dylib 0x000000010541573c _dispatch_client_callout + 16 8 libdispatch.dylib 0x000000010542648c _dispatch_lane_barrier_sync_invoke_and_complete + 144 9 MqttCocoaAsyncSocket 0x0000000105eeec40 -[MGCDAsyncSocket removeStreamsFromRunLoop] + 572 10 MqttCocoaAsyncSocket 0x0000000105edbd34 -[MGCDAsyncSocket closeWithError:] + 504 11 MqttCocoaAsyncSocket 0x0000000105edc694 29-[MGCDAsyncSocket disconnect]_block_invoke + 132 12 libdispatch.dylib 0x000000010541573c _dispatch_client_callout + 16 13 libdispatch.dylib 0x000000010542648c _dispatch_lane_barrier_sync_invoke_and_complete + 144 14 MqttCocoaAsyncSocket 0x0000000105edc5e4 -[MGCDAsyncSocket disconnect] + 216 15 CocoaMQTT 0x0000000105cdbca0 $s9CocoaMQTT0A10MQTTSocketC10disconnectyyF + 76 16 CocoaMQTT 0x0000000105cdc040 $s9CocoaMQTT0A10MQTTSocketCAA0aC8ProtocolA2aDP10disconnectyyFTW + 20 17 CocoaMQTT 0x0000000105c94eb0 $s9CocoaMQTTAACfD + 552 18 CocoaMQTT 0x0000000105c94f0c $s9CocoaMQTTAACfDTo + 28 19 AppName 0x00000001040de420 $s10AppName11MQTTManagerC4mqtt9CocoaMQTTAECSgvs + 80 20 PlugStream 0x00000001040de7a8 $s10AppName11MQTTManagerC7connectyyF + 388 21 PlugStream 0x00000001040f33e8 $s10AppName20ChargePointViewModelC03getC13PointsFromApiyyFys6ResultOySSs5Error_pGcfUyyScMYccfU + 1288 22 AppName 0x00000001040c5fa4 $sIeg_IeyB_TR + 48 23 libdispatch.dylib 0x0000000105413ec4 _dispatch_call_block_and_release + 24 24 libdispatch.dylib 0x000000010541573c _dispatch_client_callout + 16 25 libdispatch.dylib 0x00000001054253f8 _dispatch_main_queue_drain + 1228 26 libdispatch.dylib 0x0000000105424f1c _dispatch_main_queue_callback_4CF + 40 27 CoreFoundation 0x000000018040e9a0 CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE + 12 28 CoreFoundation 0x00000001804090b8 __CFRunLoopRun + 1936 29 CoreFoundation 0x0000000180408514 CFRunLoopRunSpecific + 572 30 GraphicsServices 0x000000018ef06ae4 GSEventRunModal + 160 31 UIKitCore 0x00000001853e8040 -[UIApplication _run] + 868 32 UIKitCore 0x00000001853ebcc8 UIApplicationMain + 124 33 SwiftUI 0x00000001cc1fcb68 OUTLINED_FUNCTION_65 + 492 34 SwiftUI 0x00000001cc1fca10 OUTLINED_FUNCTION_65 + 148 35 SwiftUI 0x00000001cbebb068 OUTLINED_FUNCTION_0 + 92 36 AppName 0x000000010412b328 $s10AppName0aB3AppV5$mainyyFZ + 40 37 AppName 0x000000010412b7d4 main + 12 `

It appears to connect as I receive the dieout will message but not sure what causes the crash?

import Foundation import SwiftUI import CocoaMQTT

class MQTTManager: ObservableObject { @Published var isConnected = false

var mqtt: CocoaMQTT!
private let serverURI = "serverurl.co.uk"

init() {
    connect()
}

func connect() {
    let clientID = "CocoaMQTT" + String(ProcessInfo().processIdentifier)
    mqtt = CocoaMQTT(clientID: clientID, host: serverURI, port: 8883)
    mqtt.username = "username"
    mqtt.password = "password"
    mqtt.keepAlive = 60
    mqtt.willMessage = CocoaMQTTMessage(topic: "/will", string: "dieout")
    mqtt.delegate = self
    mqtt.enableSSL = true // Enable SSL/TLS
    mqtt.allowUntrustCACertificate = true // Skip server certificate verification
    mqtt.connect()
}

func subscribe(serialNumber: String) {
    guard let client = self.mqtt else {
        print("Error: MQTT client not initialized.")
        return
    }

    let topic = "topic\(serialNumber)/cmd/"
    client.subscribe(topic, qos: .qos1)
}

}

extension MQTTManager: CocoaMQTTDelegate { func mqtt(_ mqtt: CocoaMQTT, didSubscribeTopics success: NSDictionary, failed: [String]) {

}

func mqtt(_ mqtt: CocoaMQTT, didUnsubscribeTopics topics: [String]) {

}

func mqtt(_ mqtt: CocoaMQTT, didConnect host: String, port: Int) {
}

func mqtt(_ mqtt: CocoaMQTT, didReceiveMessage message: CocoaMQTTMessage, id: UInt16 ) {
}

func mqtt(_ mqtt: CocoaMQTT, didReceive trust: SecTrust, completionHandler: @escaping (Bool) -> Void) {
    completionHandler(true)
}

func mqtt(_ mqtt: CocoaMQTT, didConnectAck ack: CocoaMQTTConnAck) {
}

func mqtt(_ mqtt: CocoaMQTT, didPublishMessage message: CocoaMQTTMessage, id: UInt16) {
}

func mqtt(_ mqtt: CocoaMQTT, didPublishAck id: UInt16) {
}

func mqtt(_ mqtt: CocoaMQTT, didSubscribeTopic topic: String) {
}

func mqtt(_ mqtt: CocoaMQTT, didUnsubscribeTopic topic: String) {
}

func mqttDidPing(_ mqtt: CocoaMQTT) {
}

func mqttDidReceivePong(_ mqtt: CocoaMQTT) {
}

func mqttDidDisconnect(_ mqtt: CocoaMQTT, withError err: Error?) {
   print ("Disconnected \(String(describing:err))")
}

}

alinekborges commented 2 months ago

I am having the same issue

AryasDad commented 2 weeks ago

Ditto. Been showing up for me, as well.