SocketStream::read(__CFReadStream*, unsigned char*, long, CFStreamError*, unsigned char*) + 548 (SocketStream.cpp:2698)

My App uses MQTT Client, but NSStream Input crashes, I don't know why. Can you tell me how to solve this problem? Attached is my full crash log:

Answered by DTS Engineer in 703944022

Consider the backtrace of your crashing thread:

Thread 0 name:
Thread 0 Crashed:
0   libsystem_kernel.dylib … mach_msg_trap + 8
1   libsystem_kernel.dylib … mach_msg + 76 (mach_msg.c:119)
2   CoreFoundation         … __CFRunLoopServiceMachPort + 372 (CFRunLoop.c:2646)
3   CoreFoundation         … __CFRunLoopRun + 1212 (CFRunLoop.c:3000)
4   CoreFoundation         … CFRunLoopRunSpecific + 600 (CFRunLoop.c:3268)
5   CFNetwork              … SocketStream::read(__CFReadStream*, unsigned char*, long, CFStreamError*, unsigned char*) …
6   CoreFoundation         … CFReadStreamRead + 456 (CFStream.c:1112)
7   Luxy                   … -[MQTTCFSocketDecoder stream:handleEvent:] + 252 (MQTTCFSocketDecoder.m:59)
8   Luxy                   … 0x100bf4000 + 10155572
9   CoreFoundation         … _signalEventSync + 216 (CFStream.c:626)

Your code (frame 7) has called CFReadStreamRead (frame 6) which has then blocked in the run loop (frame 3). CFReadStreamRead only blocks this way if you’ve set the stream up to run synchronously. Synchronous networking on the main thread isn’t safe on iOS because, if something goes wrong with the network, you end up being killed by the watchdog.

ps Normally a watchdog termination includes the 0x8badf00d event code, but there’s recently been a problem where our crash reporting infrastructure has failed to include those custom codes.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Accepted Answer

Consider the backtrace of your crashing thread:

Thread 0 name:
Thread 0 Crashed:
0   libsystem_kernel.dylib … mach_msg_trap + 8
1   libsystem_kernel.dylib … mach_msg + 76 (mach_msg.c:119)
2   CoreFoundation         … __CFRunLoopServiceMachPort + 372 (CFRunLoop.c:2646)
3   CoreFoundation         … __CFRunLoopRun + 1212 (CFRunLoop.c:3000)
4   CoreFoundation         … CFRunLoopRunSpecific + 600 (CFRunLoop.c:3268)
5   CFNetwork              … SocketStream::read(__CFReadStream*, unsigned char*, long, CFStreamError*, unsigned char*) …
6   CoreFoundation         … CFReadStreamRead + 456 (CFStream.c:1112)
7   Luxy                   … -[MQTTCFSocketDecoder stream:handleEvent:] + 252 (MQTTCFSocketDecoder.m:59)
8   Luxy                   … 0x100bf4000 + 10155572
9   CoreFoundation         … _signalEventSync + 216 (CFStream.c:626)

Your code (frame 7) has called CFReadStreamRead (frame 6) which has then blocked in the run loop (frame 3). CFReadStreamRead only blocks this way if you’ve set the stream up to run synchronously. Synchronous networking on the main thread isn’t safe on iOS because, if something goes wrong with the network, you end up being killed by the watchdog.

ps Normally a watchdog termination includes the 0x8badf00d event code, but there’s recently been a problem where our crash reporting infrastructure has failed to include those custom codes.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

The input stream is generated by CFNetwork, below is my code without modifying anything:

  CFReadStreamRef readStream;
  CFWriteStreamRef writeStream;

  CFStreamCreatePairWithSocketToHost(NULL, (__bridge CFStringRef)self.host, self.port, &readStream, &writeStream);

  CFReadStreamSetProperty(readStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
  CFWriteStreamSetProperty(writeStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);

OK, to start, be aware that CFSocketStream has been deprecated in favour of Network framework, and specifically NWConnection. Honestly, moving forward might be the best option here; that API is much nicer.

below is my code without modifying anything

That doesn’t help diagnose this issue. Rather, if you look at the backtrace above it’s clear that the stream has hit an event (frame 9), called your delegate method (frame 7), and that’s then called called CFReadStreamRead (frame 6). The most likely cause is that your delegate method has mishandled this event. What does that code look like?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

SocketStream::read(__CFReadStream*, unsigned char*, long, CFStreamError*, unsigned char*) + 548 (SocketStream.cpp:2698)
 
 
Q