QuartzCore encode_object_uncached crash along missing CGContext

We are seeing a crash being recorded in our production app that seem related to QuartzCore and we have no idea what is causing it nor we can reproduce it.

The stack trace of the crashing thread is bellow.

Inspecting our logs we see a pattern where this crash is happening after a call to UIGraphicsGetCurrentContext() returning nil. This nil is however handled properly does not cause the crash. The setup of the graphics context is correct as far as we know, and we have never see it failing during development or testing.

Other that this the app seems to be behaving normally until the crash happens

Stack trace:

Crashed: com.apple.main-thread
0  libsystem_kernel.dylib         0x1bf7467b0 __pthread_kill + 8
1  libsystem_pthread.dylib        0x1db8a09c0 pthread_kill + 212
2  libsystem_c.dylib              0x19cc3da44 abort + 100
3  QuartzCore                     0x197247248 CA::Render::Encoder::encode_object_uncached(CA::Render::Object const*) + 306
4  QuartzCore                     0x197114dc4 CA::Render::Layer::encode(CA::Render::Encoder*) const + 60
5  QuartzCore                     0x19724c4e0 CA::Render::encode_set_object(CA::Render::Encoder*, unsigned long, unsigned int, CA::Render::Object*, unsigned int) + 192
6  QuartzCore                     0x197190358 invocation function for block in CA::Context::commit_transaction(CA::Transaction*, double, double*) + 256
7  QuartzCore                     0x197244978 CA::Layer::commit_if_needed(CA::Transaction*, void (CA::Layer*, unsigned int, unsigned int) block_pointer) + 384
8  QuartzCore                     0x197244900 CA::Layer::commit_if_needed(CA::Transaction*, void (CA::Layer*, unsigned int, unsigned int) block_pointer) + 264
9  QuartzCore                     0x197244900 CA::Layer::commit_if_needed(CA::Transaction*, void (CA::Layer*, unsigned int, unsigned int) block_pointer) + 264
10 QuartzCore                     0x197244900 CA::Layer::commit_if_needed(CA::Transaction*, void (CA::Layer*, unsigned int, unsigned int) block_pointer) + 264
11 QuartzCore                     0x197244900 CA::Layer::commit_if_needed(CA::Transaction*, void (CA::Layer*, unsigned int, unsigned int) block_pointer) + 264
12 QuartzCore                     0x197244900 CA::Layer::commit_if_needed(CA::Transaction*, void (CA::Layer*, unsigned int, unsigned int) block_pointer) + 264
13 QuartzCore                     0x197244900 CA::Layer::commit_if_needed(CA::Transaction*, void (CA::Layer*, unsigned int, unsigned int) block_pointer) + 264
14 QuartzCore                     0x197244900 CA::Layer::commit_if_needed(CA::Transaction*, void (CA::Layer*, unsigned int, unsigned int) block_pointer) + 264
15 QuartzCore                     0x197244900 CA::Layer::commit_if_needed(CA::Transaction*, void (CA::Layer*, unsigned int, unsigned int) block_pointer) + 264
16 QuartzCore                     0x197244900 CA::Layer::commit_if_needed(CA::Transaction*, void (CA::Layer*, unsigned int, unsigned int) block_pointer) + 264
17 QuartzCore                     0x197244900 CA::Layer::commit_if_needed(CA::Transaction*, void (CA::Layer*, unsigned int, unsigned int) block_pointer) + 264
18 QuartzCore                     0x19718db88 CA::Context::commit_transaction(CA::Transaction*, double, double*) + 6148
19 QuartzCore                     0x1971b72c8 CA::Transaction::commit() + 668
20 UIKitCore                      0x1968bbb80 _UIApplicationFlushRunLoopCATransactionIfTooLate + 120
21 UIKitCore                      0x19696369c __processEventQueue + 7136
22 UIKitCore                      0x19695a6dc __eventFetcherSourceCallback + 156
23 CoreFoundation                 0x193f988f0 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 24
24 CoreFoundation                 0x193f987f0 __CFRunLoopDoSource0 + 204
25 CoreFoundation                 0x193f97b44 __CFRunLoopDoSources0 + 256
26 CoreFoundation                 0x193f92060 __CFRunLoopRun + 768
27 CoreFoundation                 0x193f91818 CFRunLoopRunSpecific + 572
28 GraphicsServices               0x1aa697570 GSEventRunModal + 160
29 UIKitCore                      0x1968bd0e8 -[UIApplication _run] + 1052
30 UIKitCore                      0x1968c2664 UIApplicationMain + 164
31 libswiftUIKit.dylib            0x1a70b087c UIApplicationMain(_:_:_:_:) + 100
32 IXLMath                        0x1027ff48c main + 4311282828 (<compiler-generated>:4311282828)
33 libdyld.dylib                  0x193c70140 start + 4

As an additional detail, we retrieve the context right after calling UIGraphicsBeginImageContextWithOptions(bounds.size, false, 0), so it is unclear why sometimes UIGraphicsGetCurrentContext() returns nil.

QuartzCore encode_object_uncached crash along missing CGContext
 
 
Q