macOS crash on quit (2020.3.2f1 -> 2020.3.10f1) com.Metal.CompletionQueueDispatch

We are seeing a consistent crash on macOS after we’ve exited the application; either by Application.Quit() or through the menu to quit on the native OS layer.

This is holding up an important release, so we’re hoping someone at Unity can respond as whether this is a known issue or if there is a workaround.

macOS 11.4

Crashed Thread:        48  Dispatch queue: com.Metal.CompletionQueueDispatch

Exception Type:        EXC_BAD_ACCESS (SIGSEGV)
Exception Codes:       KERN_INVALID_ADDRESS at 0x00000000000000b0
Exception Note:        EXC_CORPSE_NOTIFY

Termination Signal:    Segmentation fault: 11
Termination Reason:    Namespace SIGNAL, Code 0xb
Terminating Process:   exc handler [7703]
Thread 48 Crashed:: Dispatch queue: com.Metal.CompletionQueueDispatch
0   libsystem_platform.dylib          0x00007fff20352c62 OSAtomicAdd32 + 2
1   UnityPlayer.dylib                 0x000000010ac66bad invocation function for block in metal::PrepareEndFrame() + 61
2   com.apple.Metal                   0x00007fff2828b472 MTLDispatchListApply + 34
3   com.apple.Metal                   0x00007fff2828b98d -[_MTLCommandBuffer didCompleteWithStartTime:endTime:error:] + 577
4   com.apple.Metal                   0x00007fff2828b729 -[MTLIOAccelCommandBuffer didCompleteWithStartTime:endTime:error:] + 88
5   com.apple.Metal                   0x00007fff2828b622 -[_MTLCommandQueue commandBufferDidComplete:startTime:completionTime:error:] + 161
6   com.apple.IOAccelerator           0x00007fff28263cd6 ioAccelCommandQueueBlockFenceCallback + 44
7   com.apple.framework.IOKit         0x00007fff22abc79b IODispatchCalloutFromCFMessage + 364
8   com.apple.framework.IOKit         0x00007fff22abc61c _IODispatchCalloutWithDispatch + 33
9   libdispatch.dylib                 0x00007fff2017e2f8 dispatch_mig_server + 350
10  libdispatch.dylib                 0x00007fff201657e6 _dispatch_client_callout + 8
11  libdispatch.dylib                 0x00007fff20168190 _dispatch_continuation_pop + 423
12  libdispatch.dylib                 0x00007fff20178561 _dispatch_source_invoke + 2061
13  libdispatch.dylib                 0x00007fff2016b473 _dispatch_lane_serial_drain + 263
14  libdispatch.dylib                 0x00007fff2016c08d _dispatch_lane_invoke + 366
15  libdispatch.dylib                 0x00007fff20175bed _dispatch_workloop_worker_thread + 811
16  libsystem_pthread.dylib           0x00007fff2030c45d _pthread_wqthread + 314
17  libsystem_pthread.dylib           0x00007fff2030b42f start_wqthread + 15

I have the exact same crash on quit with Unity 2020.3.12f LTS - macOS 11.4:

Crashed Thread:        30  Dispatch queue: com.Metal.CompletionQueueDispatch

Exception Type:        EXC_BAD_ACCESS (SIGSEGV)
Exception Codes:       KERN_INVALID_ADDRESS at 0x00000000000000b0
Exception Note:        EXC_CORPSE_NOTIFY

Termination Signal:    Segmentation fault: 11
Termination Reason:    Namespace SIGNAL, Code 0xb
Terminating Process:   exc handler [1557]
Thread 30 Crashed:: Dispatch queue: com.Metal.CompletionQueueDispatch
0   libsystem_platform.dylib          0x00007fff2043bc62 OSAtomicAdd32 + 2
1   UnityPlayer.dylib                 0x0000000104ca461d 0x103e43000 + 15078941
2   com.apple.Metal                   0x00007fff28391472 MTLDispatchListApply + 34
3   com.apple.Metal                   0x00007fff2839198d -[_MTLCommandBuffer didCompleteWithStartTime:endTime:error:] + 577
4   com.apple.Metal                   0x00007fff28391729 -[MTLIOAccelCommandBuffer didCompleteWithStartTime:endTime:error:] + 88
5   com.apple.Metal                   0x00007fff28391622 -[_MTLCommandQueue commandBufferDidComplete:startTime:completionTime:error:] + 161
6   com.apple.IOAccelerator           0x00007fff28369cd6 ioAccelCommandQueueBlockFenceCallback + 44
7   com.apple.framework.IOKit         0x00007fff22bae6cb IODispatchCalloutFromCFMessage + 364
8   com.apple.framework.IOKit         0x00007fff22bae54c _IODispatchCalloutWithDispatch + 33
9   libdispatch.dylib                 0x00007fff202672f8 dispatch_mig_server + 350
10  libdispatch.dylib                 0x00007fff2024e806 _dispatch_client_callout + 8
11  libdispatch.dylib                 0x00007fff202511b0 _dispatch_continuation_pop + 423
12  libdispatch.dylib                 0x00007fff20261564 _dispatch_source_invoke + 2061
13  libdispatch.dylib                 0x00007fff20254493 _dispatch_lane_serial_drain + 263
14  libdispatch.dylib                 0x00007fff202550ad _dispatch_lane_invoke + 366
15  libdispatch.dylib                 0x00007fff2025ec0d _dispatch_workloop_worker_thread + 811
16  libsystem_pthread.dylib           0x00007fff203f545d _pthread_wqthread + 314
17  libsystem_pthread.dylib           0x00007fff203f442f start_wqthread + 15

Here’s a little more info:

ibsystem_platform.dylib`OSAtomicAdd32:
0x7fff20392ce0 <+0>: movl   %edi, %eax
0x7fff20392ce2 <+2>: lock   = Thread 39: EXC_BAD_ACCESS (code=1, address=0xb0)
0x7fff20392ce3 <+3>: xaddl  %edi, (%rsi)
0x7fff20392ce6 <+6>: addl   %edi, %eax
0x7fff20392ce8 <+8>: retq
0x7fff20392ce9 <+9>: nopl   (%rax)

Avoided by delaying when the application exits
use Application.wantsToQuit
Disable the game object, wait a few seconds, and then exit the application

Nakagawa_Takashi, I think you just saved my sanity! Thank you…
I made my own graceful shutdown with a second before application quit and it shuts off nicely without any crash (so far). I didn’t know this could be an issue that I needed to take care of myself.

Could you go into a bit more detail please? I’m running into a similar issue and am having trouble figuring it out.

How did you use Application.wantsToQuit? How do you wait a few seconds before quitting? What is the cause of this error?

bump

For me, the issue ended up being Vivox. I wasn’t completely cleaning up from a Vivox session before quitting. I had to make sure I disposed of all the Vivox stuff in OnApplicationQuit() and that fixed the crash. Perhaps you guys also have some undisposed resources.