EXC_GUARD Exception (guarded with 0x08fd4dbfade2dead)

Incident Identifier: D4F32778-CE21-48E0-9231-36FC0A811CAB
CrashReporter Key:   a188a40a14ea3da6d9032375d7e431a5968aa082
Hardware Model:      iPhone6,2
Process:             xxxx [263]
Path:                /var/containers/Bundle/Application/E321495A-7A67-4D46-9CF0-B0EC5EEF86A9/xxx
Identifier:          com.xxx.xxx
Version:             170731001 (3.0.0RC)
Code Type:           ARM-64 (Native)
Role:                Non UI
Parent Process:      launchd [1]
Coalition:           <none> [0]




Date/Time:           2017-08-01 15:09:50.0910 +0800
Launch Time:         2017-08-01 15:08:19.2621 +0800
OS Version:          iPhone OS 10.1.1 (14B100)
Report Version:      104


Exception Type:  EXC_GUARD
Exception Subtype: GUARD_TYPE_FD
Exception Message: CLOSE on file descriptor 0 (guarded with 0x08fd4dbfade2dead)
Triggered by Thread:  4

...

Thread 4 name:  CCP Socket Thead
Thread 4 Crashed:
0   libsystem_kernel.dylib         0x000000018ab51554 close + 8
1   CoreFoundation                 0x000000018baa4ee0 CFSocketInvalidate + 560
2   CFNetwork                     0x000000018c29c3e0 SocketStream::close(void const*) + 444
3   CoreFoundation                 0x000000018ba91934 _CFStreamClose + 108
4   xxxxx                        0x00000001006032a8 -[CCPDecoder close] (CCPDecoder.m:33)
5   xxxx                         0x00000001005fa360 -[MPASConnectTask closeCCPChannelWithRetryFlag:] (MPASConnectTask.m:544)
6   xxxx                         0x00000001005fa200 -[MPASConnectTask error:error:] (MPASConnectTask.m:512)
7   xxxx                         0x00000001005f9328 -[MPASConnectTask decoderHandleErrorEvent:handleEventCode:error:] (MPASConnectTask.m:290)
8   xxxx                         0x000000010060360c -[CCPDecoder stream:handleEvent:] (CCPDecoder.m:134)
9   CoreFoundation                 0x000000018bae8258 _signalEventSync + 212
10  CoreFoundation                 0x000000018bae8164 _cfstream_shared_signalEventSync + 440
11  CoreFoundation                 0x000000018bb4c278 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 24
12  CoreFoundation                 0x000000018bb4bbc0 __CFRunLoopDoSources0 + 524
13  CoreFoundation                 0x000000018bb497c0 __CFRunLoopRun + 804
14  CoreFoundation                 0x000000018ba78048 CFRunLoopRunSpecific + 444
15  Foundation                     0x000000018c589a8c -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 304
16  Foundation                     0x000000018c5de210 -[NSRunLoop(NSRunLoop) run] + 88
17  xxxx                         0x00000001005f8b70 -[MPASConnectTask networkRequestThreadEntryPoint:] (MPASConnectTask.m:179)
18  Foundation                     0x000000018c68747c __NSThread__start__ + 1024
19  libsystem_pthread.dylib       0x000000018ac34850 _pthread_body + 240
20  libsystem_pthread.dylib       0x000000018ac34760 _pthread_body + 0
21  libsystem_pthread.dylib       0x000000018ac31dac thread_start + 4


When i execute the close() of NSInputStream, app crashed and above is the crash report.


NSInputStream *_stream = xxx;


...



- (void)close {
    if (_stream) {
        [_stream close];
        [_stream removeFromRunLoop:self.runLoop forMode:self.runLoopMode];
        [_stream setDelegate:nil];
        _stream = nil;
    }
}


I'm certain that this code just run only once. But i got the EXC_GUARD crash. I have already searched for long time. People said, the _stream may be already released or uninitized, the file descriptor it owned was set to default value 0. The magic value is `0x08fd4fade2dea`, indicates that the guard was applied by SQLite.


It's strange that the code above just run once. Now i can't reproduce it. Can anyone help? What situations will lead this? How to avoid it?

Technote 2151 Understanding and Analyzing iOS Application Crash Reports has some background on

EXC_GUARD
, so you should start by reading that.

I'm certain that this code just run only once.

Right. However, you don’t need to run this code twice in order to get this error. Here’s how this might have panned out:

  1. SQLite opens file descriptor N and applies a guard to it

  2. Some other code in your app accidentally closes file descriptor N

  3. Your app starts some networking, and the networking stack allocates a socket which uses file descriptor N

  4. Your app stops that networking, which closes file descriptor N

At this point the

EXC_GUARD
check kicks in and your crash as shown.

My #1 tip for avoiding problems like this is to check the error you get back from

close
. Most folks write code like this:
int fd = open(…);
if (fd < 0) {
    … handle the error …
} else {
    … work with fd …
    close();
}

I always write the

close
as:
int junk = close();
assert(junk == 0);

That will help catch cases where I accidentally close the wrong file descriptor, because if the file descriptor is invalid the close will fail with

EBADF
.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
EXC_GUARD Exception (guarded with 0x08fd4dbfade2dead)
 
 
Q