failed to set category, reason: 未能完成操作。(OSStatus错误4097。)

When using the [AVAudioSession setCategory:withOptions:error:] API, the call hangs for a long time and eventually returns an error.This issue occurs on iOS 16, and did not appear in earlier versions.

Thread 135:
0   libsystem_kernel.dylib          0x00000002478e3cd4 _mach_msg2_trap :8 (in libsystem_kernel.dylib)
1   libsystem_kernel.dylib          0x00000002478e7214 _mach_msg_overwrite :428 (in libsystem_kernel.dylib)
2   libsystem_kernel.dylib          0x00000002478e705c _mach_msg :24 (in libsystem_kernel.dylib)
3   libdispatch.dylib               0x00000001d63ffe84 __dispatch_mach_send_and_wait_for_reply :548 (in libdispatch.dylib)
4   libdispatch.dylib               0x00000001d6400224 _dispatch_mach_send_with_result_and_wait_for_reply :60 (in libdispatch.dylib)
5   libxpc.dylib                    0x00000001b2114e04 _xpc_connection_send_message_with_reply_sync :256 (in libxpc.dylib)
6   Foundation                      0x000000019b6249f0 ___NSXPCCONNECTION_IS_WAITING_FOR_A_SYNCHRONOUS_REPLY__ :16 (in Foundation)
7   Foundation                      0x000000019c06d1b4 -[NSXPCConnection _sendInvocation:orArguments:count:methodSignature:selector:withProxy:] :2100 (in Foundation)
8   CoreFoundation                  0x000000019dfcb1cc ____forwarding___ :1072 (in CoreFoundation)
9   CoreFoundation                  0x000000019dfd3200 ___forwarding_prep_0___ :96 (in CoreFoundation)
10  AudioSession                    0x00000001c77498b0 __ZN4avas6client11SessionCore10HandlePingEv :192 (in AudioSession)
11  AudioSession                    0x00000001c77497b0 ____ZN4avas6client11SessionCore12DispatchPingEv_block_invoke :52 (in AudioSession)
12  libdispatch.dylib               0x00000001d63e4adc __dispatch_call_block_and_release :32 (in libdispatch.dylib)
13  libdispatch.dylib               0x00000001d63fe7ec __dispatch_client_callout :16 (in libdispatch.dylib)
14  libdispatch.dylib               0x00000001d63ed468 __dispatch_lane_serial_drain :740 (in libdispatch.dylib)
15  libdispatch.dylib               0x00000001d63edf78 __dispatch_lane_invoke :440 (in libdispatch.dylib)
16  libdispatch.dylib               0x00000001d63f6f48 __dispatch_root_queue_drain :364 (in libdispatch.dylib)
17  libdispatch.dylib               0x00000001d63f6d08 __dispatch_worker_thread :268 (in libdispatch.dylib)
18  libsystem_pthread.dylib         0x00000001f9ff144c __pthread_start :136 (in libsystem_pthread.dylib)
19  libsystem_pthread.dylib         0x00000001f9fed8cc _thread_start :8 (in libsystem_pthread.dylib)

Thread 132:
0   libsystem_kernel.dylib          0x00000002478e3cd4 _mach_msg2_trap :8 (in libsystem_kernel.dylib)
1   libsystem_kernel.dylib          0x00000002478e7214 _mach_msg_overwrite :428 (in libsystem_kernel.dylib)
2   libsystem_kernel.dylib          0x00000002478e705c _mach_msg :24 (in libsystem_kernel.dylib)
3   libdispatch.dylib               0x00000001d63ffe84 __dispatch_mach_send_and_wait_for_reply :548 (in libdispatch.dylib)
4   libdispatch.dylib               0x00000001d6400224 _dispatch_mach_send_with_result_and_wait_for_reply :60 (in libdispatch.dylib)
5   libxpc.dylib                    0x00000001b2114e04 _xpc_connection_send_message_with_reply_sync :256 (in libxpc.dylib)
6   Foundation                      0x000000019b6249f0 ___NSXPCCONNECTION_IS_WAITING_FOR_A_SYNCHRONOUS_REPLY__ :16 (in Foundation)
7   Foundation                      0x000000019c06d1b4 -[NSXPCConnection _sendInvocation:orArguments:count:methodSignature:selector:withProxy:] :2100 (in Foundation)
8   CoreFoundation                  0x000000019dfcb1cc ____forwarding___ :1072 (in CoreFoundation)
9   CoreFoundation                  0x000000019dfd3200 ___forwarding_prep_0___ :96 (in CoreFoundation)
10  AudioSession                    0x00000001c7754198 __ZNK4avas6client11SessionCore18SetBatchPropertiesEP12NSDictionaryIP8NSStringPU25objcproto14NSSecureCoding11objc_objectEPU15__autoreleasingP7NSArrayIPS2_IS4_P8NSNumberEENS_30AVAudioSessionBatchSetStrategyEbb :548 (in AudioSession)
11  AudioSession                    0x00000001c7753e58 __ZNK4avas6client11SessionCore20SetBatchPropertiesMXEP12NSDictionaryIP8NSStringPU25objcproto14NSSecureCoding11objc_objectE :92 (in AudioSession)
12  AudioSession                    0x00000001c775179c __ZN4avas6client11SessionCore11setCategoryEP8NSStringS3_32AVAudioSessionRouteSharingPolicym :472 (in AudioSession)
13  AudioSession                    0x00000001c7768f88 -[AVAudioSession setCategory:withOptions:error:] :68 (in AudioSession)
14  AlipayWallet                    0x000000010140580c -[AVAudioSession(APMHook) apmhook_setCategory:withOptions:error:] APMHookAudioSession.m:35 (in AlipayWallet)
15  AlipayWallet                    0x00000001014001a4 -[APMAudioSessionManager resume] APMAudioSessionManager.m:718 (in AlipayWallet)
16  libdispatch.dylib               0x00000001d63e4adc __dispatch_call_block_and_release :32 (in libdispatch.dylib)
17  libdispatch.dylib               0x00000001d63fe7ec __dispatch_client_callout :16 (in libdispatch.dylib)
18  libdispatch.dylib               0x00000001d63ed468 __dispatch_lane_serial_drain :740 (in libdispatch.dylib)
19  libdispatch.dylib               0x00000001d63edf44 __dispatch_lane_invoke :388 (in libdispatch.dylib)
20  libdispatch.dylib               0x00000001d63f83ec __dispatch_root_queue_drain_deferred_wlh :292 (in libdispatch.dylib)
21  libdispatch.dylib               0x00000001d63f7ce4 __dispatch_workloop_worker_thread :692 (in libdispatch.dylib)
22  libsystem_pthread.dylib         0x00000001f9fee3b8 __pthread_wqthread :292 (in libsystem_pthread.dylib)
23  libsystem_pthread.dylib         0x00000001f9fed8c0 _start_wqthread :8 (in libsystem_pthread.dylib)

Thanks for the post and the crash file. Looks like you have received no replies. Looking at the crash file it seems like 135 threads at least. I would recommend to go ahead and create a focused sample project that reproduces this issue as I'm not aware of any open bugs on that API. Why not calling it from the main thread?

Do you get the same results with just the relevant code in a small test project? If so, please share a link to your test project. That'll help us better understand what's going on. If you're not familiar with preparing a test project, take a look at Creating a test project.

Albert Pascual
  Worldwide Developer Relations.

This issue occurs on iOS 26+, I haven't reproduced this issue yet. According to online monitoring, it occurs in a specific user environment scenario where AVPlayer is used to play background music while the user is playing a mini-game on an H5 page, which also includes in-game audio. I will attempt to reproduce the issue again.

failed to set category, reason: 未能完成操作。(OSStatus错误4097。)
 
 
Q