Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crash if server returns reserved CONNACK message #106

Closed
Decrypto opened this issue Oct 21, 2016 · 1 comment
Closed

Crash if server returns reserved CONNACK message #106

Decrypto opened this issue Oct 21, 2016 · 1 comment

Comments

@Decrypto
Copy link

Decrypto commented Oct 21, 2016

Background:
AdafruitIO has extended the MQTT protocol by adding two additional CONNACK return codes:

6: Connection Refused, throttled
7: Connection Refused, banned

https://io.adafruit.com/blog/mqtt/2016/06/28/extending-the-mqtt-protocol/

Under normal circumstances, these codes shouldn't come up. However, I have engineered my code to reconnect after a slight delay on disconnection. Due to #98 this code currently is being triggered frequently, leading to AdafruitIO throttling further connections.

Issue:
When the server returns a CONNACK message with a code other than the formally defined 0-5 (
http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718033 ), CocoaMQTT crashes in CocoaMQTT.didReceiveConnAck(reader:, connack:) at:

let ack = CocoaMQTTConnAck(rawValue: connack)!

The initializer for CocoaMQTTConnAck returns nil when passed a rawValue outside 0...5.

`fatal error: unexpectedly found nil while unwrapping an Optional value
2016-10-20 17:12:14.708461 Ahem[1782:42159] fatal error: unexpectedly found nil while unwrapping an Optional value
Current stack trace:
0    libswiftCore.dylib                 0x0000000100458580 swift_reportError + 132
1    libswiftCore.dylib                 0x0000000100475850 _swift_stdlib_reportFatalError + 61
2    libswiftCore.dylib                 0x0000000100271550 specialized specialized StaticString.withUTF8Buffer ((UnsafeBufferPointer) -> A) -> A + 355
3    libswiftCore.dylib                 0x00000001003e9600 partial apply for (_fatalErrorMessage(StaticString, StaticString, StaticString, UInt, flags : UInt32) -> Never).(closure #2) + 109
4    libswiftCore.dylib                 0x0000000100271550 specialized specialized StaticString.withUTF8Buffer ((UnsafeBufferPointer) -> A) -> A + 355
5    libswiftCore.dylib                 0x00000001003a4970 specialized _fatalErrorMessage(StaticString, StaticString, StaticString, UInt, flags : UInt32) -> Never + 96
6    CocoaMQTT                          0x00000001001b64e0 CocoaMQTT.didReceiveConnAck(CocoaMQTTReader, connack : UInt8) -> () + 149
7    CocoaMQTT                          0x00000001001b86d0 protocol witness for CocoaMQTTReaderDelegate.didReceiveConnAck(CocoaMQTTReader, connack : UInt8) -> () in conformance CocoaMQTT + 76
8    CocoaMQTT                          0x00000001001ba7c0 CocoaMQTTReader.frameReady() -> () + 440
9    CocoaMQTT                          0x00000001001ba220 CocoaMQTTReader.payloadReady(Data) -> () + 1403
10   CocoaMQTT                          0x00000001001b5250 CocoaMQTT.socket(GCDAsyncSocket, didRead : Data, withTag : Int) -> () + 4089
11   CocoaMQTT                          0x00000001001b62b0 @objc CocoaMQTT.socket(GCDAsyncSocket, didRead : Data, withTag : Int) -> () + 95
12   CocoaAsyncSocket                   0x0000000100133900 __37-[GCDAsyncSocket completeCurrentRead]_block_invoke + 83
13   libdispatch.dylib                  0x0000000100aac741 _dispatch_call_block_and_release + 12
14   libdispatch.dylib                  0x0000000100aa2fc4 _dispatch_client_callout + 8
15   libdispatch.dylib                  0x0000000100ab1a71 _dispatch_main_queue_callback_4CF + 362
16   CoreFoundation                     0x00007fff9eca9c80 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
17   CoreFoundation                     0x00007fff9ec6a810 __CFRunLoopRun + 2205
18   CoreFoundation                     0x00007fff9ec6a410 CFRunLoopRunSpecific + 420
19   HIToolbox                          0x00007fff9e20be7c RunCurrentEventLoopInMode + 240
20   HIToolbox                          0x00007fff9e20bbf1 ReceiveNextEventCommon + 432
21   HIToolbox                          0x00007fff9e20bb8f _BlockUntilNextEventMatchingListInModeWithFilter + 71
22   AppKit                             0x00007fff9c9021b0 _DPSNextEvent + 1093
23   AppKit                             0x00007fff9d012286 -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 1637
24   AppKit                             0x00007fff9c8f6c1f -[NSApplication run] + 926
25   AppKit                             0x00007fff9c8c15b5 NSApplicationMain + 1237
26   Ahem                               0x0000000100027750 main + 84
27   libdyld.dylib                      0x00007fffb3caa254 start + 1
`

It would be nice (and more future-safe as MQTT evolves) if CocoaMQTT could handle reserved values more gracefully. There may be other instances of this class of issue besides CONNACK.

@CrazyWisdom
Copy link
Member

@Decrypto
#109

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants