Background:
CouchbaseLite uses NSURLSessionStreamTask to implement WebSocket. Occasionally NSURLSessionStreamTask could crash in 'com.apple.CFNetwork.Connection' queue due to mutating an mutable array while it was being enumerated as below. What could be the cause that triggers the issue to happen? Is there a workaround to prevent the crash?
Exception:
*** Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NSArrayM: 0x1d144ca80> was mutated while being enumerated.'
Crash:
* thread #23, queue = 'com.apple.CFNetwork.Connection', stop reason = signal SIGABRT
* frame #0: 0x00000001847642e8 libsystem_kernel.dylib`__pthread_kill + 8
frame #1: 0x000000018487d748 libsystem_pthread.dylib`pthread_kill$VARIANT$armv81 + 360
frame #2: 0x00000001846d2fbc libsystem_c.dylib`abort + 140
frame #3: 0x0000000183e70068 libc++abi.dylib`abort_message + 132
frame #4: 0x0000000183e70210 libc++abi.dylib`default_terminate_handler() + 304
frame #5: 0x0000000183e98810 libobjc.A.dylib`_objc_terminate() + 124
frame #6: 0x0000000183e8854c libc++abi.dylib`std::__terminate(void (*)()) + 16
frame #7: 0x0000000183e87ea8 libc++abi.dylib`__cxa_throw + 136
frame #8: 0x0000000183e9865c libobjc.A.dylib`objc_exception_throw + 364
frame #9: 0x0000000184c519f0 CoreFoundation`__NSFastEnumerationMutationHandler + 128
frame #10: 0x00000001852c7a1c CFNetwork`-[__NSCFTCPIOStreamTask _onqueue_ioTick] + 460
frame #11: 0x00000001853616e4 CFNetwork`TCPIOConnection::writeCompleted(unsigned long, int, void (CFStreamError) block_pointer) + 268
frame #12: 0x00000001096592cc libdispatch.dylib`_dispatch_call_block_and_release + 24
frame #13: 0x000000010965928c libdispatch.dylib`_dispatch_client_callout + 16
frame #14: 0x0000000109667f80 libdispatch.dylib`_dispatch_queue_serial_drain + 696
frame #15: 0x000000010965c7ec libdispatch.dylib`_dispatch_queue_invoke + 332
frame #16: 0x0000000109667d9c libdispatch.dylib`_dispatch_queue_serial_drain + 212
frame #17: 0x000000010965c7ec libdispatch.dylib`_dispatch_queue_invoke + 332
frame #18: 0x0000000109668f6c libdispatch.dylib`_dispatch_root_queue_drain_deferred_wlh + 428
frame #19: 0x0000000109670020 libdispatch.dylib`_dispatch_workloop_worker_thread + 652
frame #20: 0x0000000184876f1c libsystem_pthread.dylib`_pthread_wqthread + 932
frame #21: 0x0000000184876b6c libsystem_pthread.dylib`start_wqthread + 4