Application crashes in libsystem_kernel.dylib while calling socket close

I develop an application which communicates with an external device over TCP. Occassionally the app crashes when user kills the app (by swipping it up). I have no clue why it crashes and to me it seems to be an OS bug. In the callback aplicationWillTerminate TCP connection is closed.


The method looks as follows:


-(void)stop {
    @synchronized(self) {
        [self closeClientConnection];
        if([FileUtility isFileDescriptorValid: serverSocketDescriptor] && close(serverSocketDescriptor) == ERROR) {
            [logger error:@"Cannot close server socket connection"
                filename:@__FILE__
                    line:__LINE__
                function:[[NSString alloc]initWithUTF8String:__FUNCTION__]];
        } else {
            [logger info:@"Server socket closed"
                filename:@__FILE__
                    line:__LINE__
                function:[[NSString alloc]initWithUTF8String:__FUNCTION__]];
        }
        [serverThread cancel];
    }
}


Stack trace can be seen below: 0x104334d44 CLSHandler + 4329868612

# Organization: none
# Platform: ios
# Version: 1.0 (20)
# Date: 2019-11-06T18:26:00Z
# OS Version: 13.2.0 (17B84)
# Device: iPhone 7
# RAM Free: 5.4%
# Disk Free: 14%


#0. Crashed: com.apple.main-thread
0  libsystem_kernel.dylib         0x197aeee8c close + 8
1  MyAppAppStore                  0x1042f4960 -[TCPServer stop] + 258 (TCPServer.m:258)
2  MyAppAppStore                  0x10431a8bc @objc AppDelegate.applicationWillTerminate(_:) (<compiler-generated>)
3  UIKitCore                      0x19bd5349c -[UIApplication _deactivateForReason:notify:] + 760
4  UIKitCore                      0x19b52b1fc -[_UISceneLifecycleMultiplexer _performBlock:withApplicationOfDeactivationReasons:fromReasons:] + 264
5  UIKitCore                      0x19b52b5f0 -[_UISceneLifecycleMultiplexer _evalTransitionToSettings:fromSettings:forceExit:withTransitionStore:] + 736
6  UIKitCore                      0x19b52aeac -[_UISceneLifecycleMultiplexer uiScene:transitionedFromState:withTransitionContext:] + 336
7  UIKitCore                      0x19b52f3c8 __186-[_UIWindowSceneFBSSceneTransitionContextDrivenLifecycleSettingsDiffAction _performActionsForUIScene:withUpdatedFBSScene:settingsDiff:fromSettings:transitionContext:lifecycleActionType:]_block_invoke_2 + 188
8  UIKitCore                      0x19b8f26a4 +[BSAnimationSettings(UIKit) tryAnimatingWithSettings:actions:completion:] + 824
9  UIKitCore                      0x19b9d95b0 _UISceneSettingsDiffActionPerformChangesWithTransitionContext + 244
10 UIKitCore                      0x19b52f100 __186-[_UIWindowSceneFBSSceneTransitionContextDrivenLifecycleSettingsDiffAction _performActionsForUIScene:withUpdatedFBSScene:settingsDiff:fromSettings:transitionContext:lifecycleActionType:]_block_invoke + 136
11 UIKitCore                      0x19b9d94ac _UISceneSettingsDiffActionPerformActionsWithDelayForTransitionContext + 100
12 UIKitCore                      0x19b52ef6c -[_UIWindowSceneFBSSceneTransitionContextDrivenLifecycleSettingsDiffAction _performActionsForUIScene:withUpdatedFBSScene:settingsDiff:fromSettings:transitionContext:lifecycleActionType:] + 376
13 UIKitCore                      0x19b3a0ba4 __64-[UIScene scene:didUpdateWithDiff:transitionContext:completion:]_block_invoke + 636
14 UIKitCore                      0x19b39f70c -[UIScene _emitSceneSettingsUpdateResponseForCompletion:afterSceneUpdateWork:] + 248
15 UIKitCore                      0x19b3a08dc -[UIScene scene:didUpdateWithDiff:transitionContext:completion:] + 220
16 UIKitCore                      0x19b91350c -[UIApplicationSceneClientAgent scene:handleEvent:withCompletion:] + 464
17 FrontBoardServices             0x19cdfb2f8 -[FBSSceneImpl updater:didUpdateSettings:withDiff:transitionContext:completion:] + 544
18 FrontBoardServices             0x19ce1fdd8 __88-[FBSWorkspaceScenesClient sceneID:updateWithSettingsDiff:transitionContext:completion:]_block_invoke_2 + 120
19 FrontBoardServices             0x19ce04fb4 -[FBSWorkspace _calloutQueue_executeCalloutFromSource:withBlock:] + 232
20 FrontBoardServices             0x19ce1fd0c __88-[FBSWorkspaceScenesClient sceneID:updateWithSettingsDiff:transitionContext:completion:]_block_invoke + 184
21 libdispatch.dylib              0x1979c3184 _dispatch_client_callout + 16
22 libdispatch.dylib              0x19796bfd8 _dispatch_block_invoke_direct$VARIANT$mp + 224
23 FrontBoardServices             0x19ce444c8 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 40
24 FrontBoardServices             0x19ce44194 -[FBSSerialQueue _queue_performNextIfPossible] + 404
25 FrontBoardServices             0x19ce446bc -[FBSSerialQueue _performNextFromRunLoopSource] + 28
26 CoreFoundation                 0x197c737c4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 24
27 CoreFoundation                 0x197c7371c __CFRunLoopDoSource0 + 80
28 CoreFoundation                 0x197c72eb4 __CFRunLoopDoSources0 + 180
29 CoreFoundation                 0x197c6e000 __CFRunLoopRun + 1080
30 CoreFoundation                 0x197c6d8a0 CFRunLoopRunSpecific + 464
31 GraphicsServices               0x1a1bc5328 GSEventRunModal + 104
32 UIKitCore                      0x19bd5e768 UIApplicationMain + 1936
33 MyAppAppStore                  0x1042f7b0c main + 15 (AppDelegate.swift:15)
34 libdyld.dylib                  0x197af8360 start + 4


--


#0. Crashed: com.apple.main-thread
0  libsystem_kernel.dylib         0x197aeee8c close + 8
1  MyAppAppStore                  0x1042f4960 -[TCPServer stop] + 258 (TCPServer.m:258)
2  MyAppAppStore                  0x10431a8bc @objc AppDelegate.applicationWillTerminate(_:) (<compiler-generated>)
3  UIKitCore                      0x19bd5349c -[UIApplication _deactivateForReason:notify:] + 760
4  UIKitCore                      0x19b52b1fc -[_UISceneLifecycleMultiplexer _performBlock:withApplicationOfDeactivationReasons:fromReasons:] + 264
5  UIKitCore                      0x19b52b5f0 -[_UISceneLifecycleMultiplexer _evalTransitionToSettings:fromSettings:forceExit:withTransitionStore:] + 736
6  UIKitCore                      0x19b52aeac -[_UISceneLifecycleMultiplexer uiScene:transitionedFromState:withTransitionContext:] + 336
7  UIKitCore                      0x19b52f3c8 __186-[_UIWindowSceneFBSSceneTransitionContextDrivenLifecycleSettingsDiffAction _performActionsForUIScene:withUpdatedFBSScene:settingsDiff:fromSettings:transitionContext:lifecycleActionType:]_block_invoke_2 + 188
8  UIKitCore                      0x19b8f26a4 +[BSAnimationSettings(UIKit) tryAnimatingWithSettings:actions:completion:] + 824
9  UIKitCore                      0x19b9d95b0 _UISceneSettingsDiffActionPerformChangesWithTransitionContext + 244
10 UIKitCore                      0x19b52f100 __186-[_UIWindowSceneFBSSceneTransitionContextDrivenLifecycleSettingsDiffAction _performActionsForUIScene:withUpdatedFBSScene:settingsDiff:fromSettings:transitionContext:lifecycleActionType:]_block_invoke + 136
11 UIKitCore                      0x19b9d94ac _UISceneSettingsDiffActionPerformActionsWithDelayForTransitionContext + 100
12 UIKitCore                      0x19b52ef6c -[_UIWindowSceneFBSSceneTransitionContextDrivenLifecycleSettingsDiffAction _performActionsForUIScene:withUpdatedFBSScene:settingsDiff:fromSettings:transitionContext:lifecycleActionType:] + 376
13 UIKitCore                      0x19b3a0ba4 __64-[UIScene scene:didUpdateWithDiff:transitionContext:completion:]_block_invoke + 636
14 UIKitCore                      0x19b39f70c -[UIScene _emitSceneSettingsUpdateResponseForCompletion:afterSceneUpdateWork:] + 248
15 UIKitCore                      0x19b3a08dc -[UIScene scene:didUpdateWithDiff:transitionContext:completion:] + 220
16 UIKitCore                      0x19b91350c -[UIApplicationSceneClientAgent scene:handleEvent:withCompletion:] + 464
17 FrontBoardServices             0x19cdfb2f8 -[FBSSceneImpl updater:didUpdateSettings:withDiff:transitionContext:completion:] + 544
18 FrontBoardServices             0x19ce1fdd8 __88-[FBSWorkspaceScenesClient sceneID:updateWithSettingsDiff:transitionContext:completion:]_block_invoke_2 + 120
19 FrontBoardServices             0x19ce04fb4 -[FBSWorkspace _calloutQueue_executeCalloutFromSource:withBlock:] + 232
20 FrontBoardServices             0x19ce1fd0c __88-[FBSWorkspaceScenesClient sceneID:updateWithSettingsDiff:transitionContext:completion:]_block_invoke + 184
21 libdispatch.dylib              0x1979c3184 _dispatch_client_callout + 16
22 libdispatch.dylib              0x19796bfd8 _dispatch_block_invoke_direct$VARIANT$mp + 224
23 FrontBoardServices             0x19ce444c8 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 40
24 FrontBoardServices             0x19ce44194 -[FBSSerialQueue _queue_performNextIfPossible] + 404
25 FrontBoardServices             0x19ce446bc -[FBSSerialQueue _performNextFromRunLoopSource] + 28
26 CoreFoundation                 0x197c737c4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 24
27 CoreFoundation                 0x197c7371c __CFRunLoopDoSource0 + 80
28 CoreFoundation                 0x197c72eb4 __CFRunLoopDoSources0 + 180
29 CoreFoundation                 0x197c6e000 __CFRunLoopRun + 1080
30 CoreFoundation                 0x197c6d8a0 CFRunLoopRunSpecific + 464
31 GraphicsServices               0x1a1bc5328 GSEventRunModal + 104
32 UIKitCore                      0x19bd5e768 UIApplicationMain + 1936
33 MyAppAppStore                  0x1042f7b0c main + 15 (AppDelegate.swift:15)
34 libdyld.dylib                  0x197af8360 start + 4


#1. Thread
0  libsystem_kernel.dylib         0x197aeea7c __workq_kernreturn + 8
1  libsystem_pthread.dylib        0x197a12fd4 _pthread_wqthread + 352
2  libsystem_pthread.dylib        0x197a15ad4 start_wqthread + 8


#2. Thread
0  libsystem_kernel.dylib         0x197aeea7c __workq_kernreturn + 8
1  libsystem_pthread.dylib        0x197a12fd4 _pthread_wqthread + 352
2  libsystem_pthread.dylib        0x197a15ad4 start_wqthread + 8


#3. com.apple.uikit.eventfetch-thread
0  libsystem_kernel.dylib         0x197acc634 mach_msg_trap + 8
1  libsystem_kernel.dylib         0x197acbaa0 mach_msg + 72

Accepted Reply

My best guess here is that you’re trying to close a guarded file descriptor. See the discussion of

EXC_GUARD
in Technote 2151 Understanding and Analyzing iOS Application Crash Reports.

It’s hard to confirm this without an Apple crash report. I recommend that you remove your third-party crash reporter, wait for the problem to reproduce, grab the crash report, and then check it against the symptoms described in TN2151.

Share and Enjoy

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

let myEmail = "eskimo" + "1" + "@apple.com"

Replies

Does your project have Objective-C Exception handling enabled?

Yes, it does

It looks like the close call is being done on the main thread probably causing a block on everything else running on the main thread.

  1. #0. Crashed: com.apple.main-thread
  2. 0 libsystem_kernel.dylib 0x197aeee8c close + 8
  3. 1 MyAppAppStore 0x1042f4960 -[TCPServer stop] + 258 (TCPServer.m:258)


If you move the close off the main thread do you still see the same behavior?

My best guess here is that you’re trying to close a guarded file descriptor. See the discussion of

EXC_GUARD
in Technote 2151 Understanding and Analyzing iOS Application Crash Reports.

It’s hard to confirm this without an Apple crash report. I recommend that you remove your third-party crash reporter, wait for the problem to reproduce, grab the crash report, and then check it against the symptoms described in TN2151.

Share and Enjoy

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

let myEmail = "eskimo" + "1" + "@apple.com"