App hangs while opening from background

Hi,

My app hangs for about a second when I open the app when it's been in background phase. It doesn't hang every time, but it hangs randomly (like once in 10 times) when I open the app again. I am not able to reproduce what causes this hang.

Hang Detection provides hang logs (spindump) when hang occurs. It has something to do with MusicKit as logs mention it but I cannot understand what it is. Please help me understand these logs.

I am running iOS 17 beta 1 with MusicKit and ApplicationMusicPlayer.

Heaviest stack for the main thread of the target process:
  69  start + 2104 (dyld + 87288) [0x1b719b4f8]
  69  ??? (Timed + 32864) [0x104ae8060]
  69  ??? (SwiftUI + 980040) [0x19840a448]
  69  ??? (SwiftUI + 1071088) [0x1984207f0]
  69  ??? (SwiftUI + 1667804) [0x1984b22dc]
  69  UIApplicationMain + 340 (UIKitCore + 3740336) [0x196c602b0]
  69  -[UIApplication _run] + 888 (UIKitCore + 3741260) [0x196c6064c]
  69  GSEventRunModal + 164 (GraphicsServices + 4644) [0x1d6199224]
  69  CFRunLoopRunSpecific + 600 (CoreFoundation + 527792) [0x1947fedb0]
  68  __CFRunLoopRun + 1996 (CoreFoundation + 509348) [0x1947fa5a4]
  68  __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 16 (CoreFoundation + 626340) [0x194816ea4]
  68  _dispatch_main_queue_callback_4CF + 44 (libdispatch.dylib + 75296) [0x19c404620]
  67  _dispatch_main_queue_drain + 744 (libdispatch.dylib + 76056) [0x19c404918]
  67  swift_job_runImpl(swift::Job*, swift::ExecutorRef) + 72 (libswift_Concurrency.dylib + 274348) [0x19f58cfac]
  67  swift::runJobInEstablishedExecutorContext(swift::Job*) + 416 (libswift_Concurrency.dylib + 269688) [0x19f58bd78]
  66  ??? (MusicKit + 4857464) [0x208aabe78]
  66  ??? (MusicKit + 4856108) [0x208aab92c]
  66  -[MusicKit_SoftLinking_MPMusicPlayerController nowPlayingItem] + 24 (MusicKit + 157364) [0x2086306b4]
  66  -[MPMusicPlayerController nowPlayingItem] + 24 (MediaPlayer + 1358540) [0x1a681bacc]
  66  -[MPMusicPlayerController _nowPlaying] + 372 (MediaPlayer + 1329552) [0x1a6814990]
  66  -[MPMusicPlayerController onServer:] + 52 (MediaPlayer + 1333428) [0x1a68158b4]
  63  -[MPMusicPlayerApplicationController _establishConnectionIfNeeded] + 1768 (MediaPlayer + 1553848) [0x1a684b5b8]
  63  _NSXPCDistantObjectSimpleMessageSend1 + 60 (Foundation + 208348) [0x1937daddc]
  63  -[NSXPCConnection _sendSelector:withProxy:arg1:] + 116 (Foundation + 208548) [0x1937daea4]
  62  -[NSXPCConnection _sendInvocation:orArguments:count:methodSignature:selector:withProxy:] + 2160 (Foundation + 214664) [0x1937dc688]
  62  __NSXPCCONNECTION_IS_WAITING_FOR_A_SYNCHRONOUS_REPLY__ + 16 (Foundation + 652944) [0x193847690]
  62  xpc_connection_send_message_with_reply_sync + 264 (libxpc.dylib + 67488) [0x1fc00d7a0]
  62  dispatch_mach_send_with_result_and_wait_for_reply + 60 (libdispatch.dylib + 127760) [0x19c411310]
  62  _dispatch_mach_send_and_wait_for_reply + 540 (libdispatch.dylib + 126832) [0x19c410f70]
  62  mach_msg + 24 (libsystem_kernel.dylib + 4692) [0x1da082254]
  62  mach_msg_overwrite + 436 (libsystem_kernel.dylib + 83544) [0x1da095658]
  62  mach_msg2_trap + 8 (libsystem_kernel.dylib + 3332) [0x1da081d04]
 *62  ??? (<31E57057-A9A0-3BE5-90CB-5C08E9683B34> + 217132) [0xfffffff007e0102c]

Thank you.

CC @JoeKun

Hello, can you please submit feedback with full logs and share the FB number?

I've been seeing intermittent crashes like this for years working with applicationQueuePlayer and now ApplicationMusicPlayer. I've heard from more users seeing this issue than usual since iOS 16.5 came out. Below is one such crash log I got today. I submitted FB12464849 just now with a couple of crash logs. Unfortunately I don't have a sysdiagnose to include, but I'll try to get one from any additional users that report issues like this.

Exception Type:  EXC_CRASH (SIGKILL)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Termination Reason: FRONTBOARD 2343432205 
<RBSTerminateContext| domain:10 code:0x8BADF00D explanation:[application<com.talkingsmall.Albums>:11253] failed to terminate gracefully after 5.0s
ProcessVisibility: Background
ProcessState: Running
WatchdogEvent: process-exit
WatchdogVisibility: Background
WatchdogCPUStatistics: (
"Elapsed total CPU time (seconds): 3.530 (user 2.080, system 1.450), 11% CPU",
"Elapsed application CPU time (seconds): 0.008, 0% CPU"
) reportType:CrashLog maxTerminationResistance:Interactive>

Triggered by Thread:  0

Thread 0 Crashed:
0   libsystem_kernel.dylib        	       0x20ecd3c84 mach_msg2_trap + 8
1   libsystem_kernel.dylib        	       0x20ece6b54 mach_msg2_internal + 80
2   libsystem_kernel.dylib        	       0x20ece6e2c mach_msg_overwrite + 540
3   libsystem_kernel.dylib        	       0x20ecd41c8 mach_msg + 24
4   libdispatch.dylib             	       0x1d727a20c _dispatch_mach_send_and_wait_for_reply + 548
5   libdispatch.dylib             	       0x1d727a59c dispatch_mach_send_with_result_and_wait_for_reply + 60
6   libxpc.dylib                  	       0x22fb6f218 xpc_connection_send_message_with_reply_sync + 240
7   Foundation                    	       0x1ca0f7f38 __NSXPCCONNECTION_IS_WAITING_FOR_A_SYNCHRONOUS_REPLY__ + 16
8   Foundation                    	       0x1ca08b2e4 -[NSXPCConnection _sendInvocation:orArguments:count:methodSignature:selector:withProxy:] + 2192
9   Foundation                    	       0x1ca089ae0 -[NSXPCConnection _sendSelector:withProxy:arg1:] + 116
10  Foundation                    	       0x1ca089a18 _NSXPCDistantObjectSimpleMessageSend1 + 60
11  MediaPlayer                   	       0x1dfe52108 -[MPMusicPlayerApplicationController _establishConnectionIfNeeded] + 1768
12  MediaPlayer                   	       0x1dfe1b8dc -[MPMusicPlayerController onServer:] + 52
13  MediaPlayer                   	       0x1dfe1a9b8 -[MPMusicPlayerController _nowPlaying] + 372
14  MediaPlayer                   	       0x1dfe21adc -[MPMusicPlayerController nowPlayingItem] + 24
15  MusicKit                      	       0x23b98a7d0 -[MusicKit_SoftLinking_MPMusicPlayerController nowPlayingItem] + 24
16  MusicKit                      	       0x23bedc7f4 0x23b966000 + 5728244
17  MusicKit                      	       0x23bedce34 0x23b966000 + 5729844
18  MusicKit                      	       0x23bede139 0x23b966000 + 5734713
19  MusicKit                      	       0x23bdb629d 0x23b966000 + 4522653
20  MusicKit                      	       0x23bb12fa1 0x23b966000 + 1757089
21  libswift_Concurrency.dylib    	       0x1da155dd9 completeTaskWithClosure(swift::AsyncContext*, swift::SwiftError*) + 1  
App hangs while opening from background
 
 
Q