We have an application where we have a worker thread working in parallel with the main thread. The application flow is in this way. First, the main thread calls JavascriptCore API JSObjectMake. While this is happening the worker thread also makes the JSValueUnprotect call to JavascriptCore framework. By observing the call stack we have found that main thread JSObjectMake call uses the JavaScriptCore API and locks certain block of the API and later when worker thread makes JSValueUnprotect API call it is locking JSC::JSLock::lock(long) the JavascriptCore thread. As a result, main thread waits for JS thread, but the JS thread is locked by closure thread and the closure thread is waiting for some other resource locked by main thread JSObject make call. Due to this deadlock, the application is freezing and later crashed by the watchdog.
Please find the call stack below. What to do in this case. Are we doing anything wrong? Any small insight would be very helpful.
* thread #1, queue = 'com.apple.main-thread', stop reason = signal SIGSTOP
* frame #0: 0x00000001bfcfad1c libsystem_kernel.dylib`__psynch_mutexwait + 8
frame #1: 0x00000001bfc1e868 libsystem_pthread.dylib`_pthread_mutex_firstfit_lock_wait + 92
frame #2: 0x00000001bfc1e7d8 libsystem_pthread.dylib`_pthread_mutex_firstfit_lock_slow + 260
frame #3: 0x00000001bfe8a594 CoreFoundation`CFRunLoopTimerSetNextFireDate + 344
frame #4: 0x00000001cfab3ccc JavaScriptCore`JSC::JSRunLoopTimer::Manager::scheduleTimer(JSC::JSRunLoopTimer&, ***::Seconds) + 564
frame #5: 0x00000001cfab45c4 JavaScriptCore`JSC::JSRunLoopTimer::setTimeUntilFire(***::Seconds) + 144
frame #6: 0x00000001cf6e4e28 JavaScriptCore`JSC::GCActivityCallback::didAllocate(JSC::Heap&, unsigned long) + 188
frame #7: 0x00000001cf6e71e4 JavaScriptCore`JSC::Heap::didAllocate(unsigned long) + 48
frame #8: 0x00000001cf702e88 JavaScriptCore`JSC::LocalAllocator::allocateSlowCase(JSC::GCDeferralContext*, JSC::AllocationFailureMode) + 216
frame #9: 0x00000001cf26985c JavaScriptCore`JSObjectMake + 608
*************PRIVATE CODE************* (Call to JavaScriptCore::JSObjectMake)
frame #36: 0x00000001bfe8c05c CoreFoundation`__CFRunLoopDoSource0 + 84
frame #37: 0x00000001bfe8b7c8 CoreFoundation`__CFRunLoopDoSources0 + 184
frame #38: 0x00000001bfe86694 CoreFoundation`__CFRunLoopRun + 1068
frame #39: 0x00000001bfe85f40 CoreFoundation`CFRunLoopRunSpecific + 480
frame #40: 0x00000001ca102534 GraphicsServices`GSEventRunModal + 108
frame #41: 0x00000001c3ffea60 UIKitCore`UIApplicationMain + 1940
frame #42: 0x0000000100ad5c54 main(argc=1, argv=0x000000016f3678b8) at main.m:94:9
frame #43: 0x00000001bfd04e18 libdyld.dylib`start + 4
thread #2
frame #0: 0x00000001bfcfbab4 libsystem_kernel.dylib`__workq_kernreturn + 8
thread #6, name = 'com.apple.uikit.eventfetch-thread'
frame #0: 0x00000001bfcd8c04 libsystem_kernel.dylib`mach_msg_trap + 8
frame #1: 0x00000001bfcd8020 libsystem_kernel.dylib`mach_msg + 76
frame #2: 0x00000001bfe8b964 CoreFoundation`__CFRunLoopServiceMachPort + 220
frame #3: 0x00000001bfe867fc CoreFoundation`__CFRunLoopRun + 1428
frame #4: 0x00000001bfe85f40 CoreFoundation`CFRunLoopRunSpecific + 480
frame #5: 0x00000001c01cb3b0 Foundation`-[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 232
frame #6: 0x00000001c01cb288 Foundation`-[NSRunLoop(NSRunLoop) runUntilDate:] + 92
frame #7: 0x00000001c409a23c UIKitCore`-[UIEventFetcher threadMain] + 156
frame #8: 0x00000001c01ca014 Foundation`-[NSThread main] + 40
frame #9: 0x00000001c0305a7c Foundation`__NSThread__start__ + 852
frame #10: 0x00000001bfc19840 libsystem_pthread.dylib`_pthread_start + 168
thread #7, name = 'Worker Thread'
frame #0: 0x00000001bfcfaccc libsystem_kernel.dylib`__psynch_cvwait + 8
frame #1: 0x00000001bfc1d21c libsystem_pthread.dylib`_pthread_cond_wait + 676
frame #2: 0x00000001cf021c24 JavaScriptCore`***::ThreadCondition::timedWait(***::Mutex&, ***::WallTime) + 84
frame #3: 0x00000001cf00723c JavaScriptCore`***::ParkingLot::parkConditionallyImpl(void const*, ***::ScopedLambda<bool ()> const&, ***::ScopedLambda<void ()> const&, ***::TimeWithDynamicClockType const&) + 1928
frame #4: 0x00000001ceff8f48 JavaScriptCore`***::LockAlgorithm<unsigned char, (unsigned char)1, (unsigned char)2, ***::EmptyLockHooks<unsigned char> >::lockSlow(***::Atomic<unsigned char>&) + 244
frame #5: 0x00000001cfa854c4 JavaScriptCore`JSC::JSLock::lock(long) + 256
frame #6: 0x00000001cfa85024 JavaScriptCore`JSC::JSLockHolder::JSLockHolder(JSC::ExecState*) + 60
frame #7: 0x00000001cf2714cc JavaScriptCore`JSValueUnprotect + 36
*************PRIVATE CODE************* (Call to JavaScriptCore::JSValueUnprotect)
frame #17: 0x00000001bfde7ab4 CoreFoundation`-[__NSDictionaryI dealloc] + 156
frame #18: 0x00000001bfc433f8 libobjc.A.dylib`objc_release + 136
frame #19: 0x00000001bfc449c8 libobjc.A.dylib`AutoreleasePoolPage::releaseUntil(objc_object**) + 184
frame #20: 0x00000001bfc448b4 libobjc.A.dylib`objc_autoreleasePoolPop + 232
frame #21: 0x00000001bff1d958 CoreFoundation`_CFAutoreleasePoolPop + 32
frame #22: 0x00000001bfe8bb7c CoreFoundation`__CFRunLoopDoTimers + 364
frame #23: 0x00000001bfe869d0 CoreFoundation`__CFRunLoopRun + 1896
frame #24: 0x00000001bfe85f40 CoreFoundation`CFRunLoopRunSpecific + 480
frame #25: 0x00000001c01cb3b0 Foundation`-[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 232
frame #26: *************PRIVATE CODE*************
frame #27: 0x00000001c0305a7c Foundation`__NSThread__start__ + 852
frame #28: 0x00000001bfc19840 libsystem_pthread.dylib`_pthread_start + 168
thread #8, name = 'Network Thread'
frame #0: 0x00000001bfcd8c04 libsystem_kernel.dylib`mach_msg_trap + 8
frame #1: 0x00000001bfcd8020 libsystem_kernel.dylib`mach_msg + 76
frame #2: 0x00000001bfe8b964 CoreFoundation`__CFRunLoopServiceMachPort + 220
frame #3: 0x00000001bfe867fc CoreFoundation`__CFRunLoopRun + 1428
frame #4: 0x00000001bfe85f40 CoreFoundation`CFRunLoopRunSpecific + 480
frame #5: 0x00000001c01cb3b0 Foundation`-[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 232
frame #6: *************PRIVATE CODE*************
frame #7: 0x00000001c0305a7c Foundation`__NSThread__start__ + 852
frame #8: 0x00000001bfc19840 libsystem_pthread.dylib`_pthread_start + 168
thread #9, name = 'JavaScriptCore bmalloc scavenger'
frame #0: 0x00000001bfcfaccc libsystem_kernel.dylib`__psynch_cvwait + 8
frame #1: 0x00000001bfc1d21c libsystem_pthread.dylib`_pthread_cond_wait + 676
frame #2: 0x00000001bfd4fd04 libc++.1.dylib`std::__1::condition_variable::wait(std::__1::unique_lock<std::__1::mutex>&) + 28
frame #3: 0x00000001cf05e21c JavaScriptCore`void std::__1::condition_variable_any::wait<std::__1::unique_lock<bmalloc::Mutex> >(std::__1::unique_lock<bmalloc::Mutex>&) + 104
frame #4: 0x00000001cf061c80 JavaScriptCore`bmalloc::Scavenger::threadRunLoop() + 196
frame #5: 0x00000001cf061978 JavaScriptCore`bmalloc::Scavenger::threadEntryPoint(bmalloc::Scavenger*) + 16
frame #6: 0x00000001cf062cbc JavaScriptCore`void* std::__1::__thread_proxy<std::__1::tuple<std::__1::unique_ptr<std::__1::__thread_struct, std::__1::default_delete<std::__1::__thread_struct> >, void (*)(bmalloc::Scavenger*), bmalloc::Scavenger*> >(void*) + 44
frame #7: 0x00000001bfc19840 libsystem_pthread.dylib`_pthread_start + 168
thread #15
frame #0: 0x00000001bfc219e0 libsystem_pthread.dylib`start_wqthread
thread #16
frame #0: 0x00000001bfcfbab4 libsystem_kernel.dylib`__workq_kernreturn + 8
thread #17
frame #0: 0x00000001bfcfbab4 libsystem_kernel.dylib`__workq_kernreturn + 8
thread #18
frame #0: 0x00000001bfcfbab4 libsystem_kernel.dylib`__workq_kernreturn + 8
thread #19, name = 'JSC Heap Collector Thread'
frame #0: 0x00000001bfcfaccc libsystem_kernel.dylib`__psynch_cvwait + 8
frame #1: 0x00000001bfc1d21c libsystem_pthread.dylib`_pthread_cond_wait + 676
frame #2: 0x00000001cf021c64 JavaScriptCore`***::ThreadCondition::timedWait(***::Mutex&, ***::WallTime) + 148
frame #3: 0x00000001cf00723c JavaScriptCore`***::ParkingLot::parkConditionallyImpl(void const*, ***::ScopedLambda<bool ()> const&, ***::ScopedLambda<void ()> const&, ***::TimeWithDynamicClockType const&) + 1928
frame #4: 0x00000001cefddfc4 JavaScriptCore`bool ***::Condition::waitUntil<***::Lock>(***::Lock&, ***::TimeWithDynamicClockType const&) + 192
frame #5: 0x00000001cefde334 JavaScriptCore`***::Detail::CallableWrapper<***::AutomaticThread::start(***::AbstractLocker const&)::$_0, void>::call() + 208
frame #6: 0x00000001cf01f8f8 JavaScriptCore`***::Thread::entryPoint(***::Thread::NewThreadContext*) + 260
frame #7: 0x00000001cf021444 JavaScriptCore`***::wtfThreadEntryPoint(void*) + 16
frame #8: 0x00000001bfc19840 libsystem_pthread.dylib`_pthread_start + 168
thread #20, name = 'Heap Helper Thread'
frame #0: 0x00000001bfcfaccc libsystem_kernel.dylib`__psynch_cvwait + 8
frame #1: 0x00000001bfc1d21c libsystem_pthread.dylib`_pthread_cond_wait + 676
frame #2: 0x00000001cf021c64 JavaScriptCore`***::ThreadCondition::timedWait(***::Mutex&, ***::WallTime) + 148
frame #3: 0x00000001cf00723c JavaScriptCore`***::ParkingLot::parkConditionallyImpl(void const*, ***::ScopedLambda<bool ()> const&, ***::ScopedLambda<void ()> const&, ***::TimeWithDynamicClockType const&) + 1928
frame #4: 0x00000001cefddfc4 JavaScriptCore`bool ***::Condition::waitUntil<***::Lock>(***::Lock&, ***::TimeWithDynamicClockType const&) + 192
frame #5: 0x00000001cefde334 JavaScriptCore`***::Detail::CallableWrapper<***::AutomaticThread::start(***::AbstractLocker const&)::$_0, void>::call() + 208
frame #6: 0x00000001cf01f8f8 JavaScriptCore`***::Thread::entryPoint(***::Thread::NewThreadContext*) + 260
frame #7: 0x00000001cf021444 JavaScriptCore`***::wtfThreadEntryPoint(void*) + 16
frame #8: 0x00000001bfc19840 libsystem_pthread.dylib`_pthread_start + 168
thread #21, name = 'Heap Helper Thread'
frame #0: 0x00000001bfcfaccc libsystem_kernel.dylib`__psynch_cvwait + 8
frame #1: 0x00000001bfc1d21c libsystem_pthread.dylib`_pthread_cond_wait + 676
frame #2: 0x00000001cf021c64 JavaScriptCore`***::ThreadCondition::timedWait(***::Mutex&, ***::WallTime) + 148
frame #3: 0x00000001cf00723c JavaScriptCore`***::ParkingLot::parkConditionallyImpl(void const*, ***::ScopedLambda<bool ()> const&, ***::ScopedLambda<void ()> const&, ***::TimeWithDynamicClockType const&) + 1928
frame #4: 0x00000001cefddfc4 JavaScriptCore`bool ***::Condition::waitUntil<***::Lock>(***::Lock&, ***::TimeWithDynamicClockType const&) + 192
frame #5: 0x00000001cefde334 JavaScriptCore`***::Detail::CallableWrapper<***::AutomaticThread::start(***::AbstractLocker const&)::$_0, void>::call() + 208
frame #6: 0x00000001cf01f8f8 JavaScriptCore`***::Thread::entryPoint(***::Thread::NewThreadContext*) + 260
frame #7: 0x00000001cf021444 JavaScriptCore`***::wtfThreadEntryPoint(void*) + 16
frame #8: 0x00000001bfc19840 libsystem_pthread.dylib`_pthread_start + 168
thread #22, name = 'Heap Helper Thread'
frame #0: 0x00000001bfcfaccc libsystem_kernel.dylib`__psynch_cvwait + 8
frame #1: 0x00000001bfc1d21c libsystem_pthread.dylib`_pthread_cond_wait + 676
frame #2: 0x00000001cf021c64 JavaScriptCore`***::ThreadCondition::timedWait(***::Mutex&, ***::WallTime) + 148
frame #3: 0x00000001cf00723c JavaScriptCore`***::ParkingLot::parkConditionallyImpl(void const*, ***::ScopedLambda<bool ()> const&, ***::ScopedLambda<void ()> const&, ***::TimeWithDynamicClockType const&) + 1928
frame #4: 0x00000001cefddfc4 JavaScriptCore`bool ***::Condition::waitUntil<***::Lock>(***::Lock&, ***::TimeWithDynamicClockType const&) + 192
frame #5: 0x00000001cefde334 JavaScriptCore`***::Detail::CallableWrapper<***::AutomaticThread::start(***::AbstractLocker const&)::$_0, void>::call() + 208
frame #6: 0x00000001cf01f8f8 JavaScriptCore`***::Thread::entryPoint(***::Thread::NewThreadContext*) + 260
frame #7: 0x00000001cf021444 JavaScriptCore`***::wtfThreadEntryPoint(void*) + 16
frame #8: 0x00000001bfc19840 libsystem_pthread.dylib`_pthread_start + 168
thread #23, name = 'Heap Helper Thread'
frame #0: 0x00000001bfcfaccc libsystem_kernel.dylib`__psynch_cvwait + 8
frame #1: 0x00000001bfc1d21c libsystem_pthread.dylib`_pthread_cond_wait + 676
frame #2: 0x00000001cf021c64 JavaScriptCore`***::ThreadCondition::timedWait(***::Mutex&, ***::WallTime) + 148
frame #3: 0x00000001cf00723c JavaScriptCore`***::ParkingLot::parkConditionallyImpl(void const*, ***::ScopedLambda<bool ()> const&, ***::ScopedLambda<void ()> const&, ***::TimeWithDynamicClockType const&) + 1928
frame #4: 0x00000001cefddfc4 JavaScriptCore`bool ***::Condition::waitUntil<***::Lock>(***::Lock&, ***::TimeWithDynamicClockType const&) + 192
frame #5: 0x00000001cefde334 JavaScriptCore`***::Detail::CallableWrapper<***::AutomaticThread::start(***::AbstractLocker const&)::$_0, void>::call() + 208
frame #6: 0x00000001cf01f8f8 JavaScriptCore`***::Thread::entryPoint(***::Thread::NewThreadContext*) + 260
frame #7: 0x00000001cf021444 JavaScriptCore`***::wtfThreadEntryPoint(void*) + 16
frame #8: 0x00000001bfc19840 libsystem_pthread.dylib`_pthread_start + 168
thread #24, name = 'Heap Helper Thread'
frame #0: 0x00000001bfcfaccc libsystem_kernel.dylib`__psynch_cvwait + 8
frame #1: 0x00000001bfc1d21c libsystem_pthread.dylib`_pthread_cond_wait + 676
frame #2: 0x00000001cf021c64 JavaScriptCore`***::ThreadCondition::timedWait(***::Mutex&, ***::WallTime) + 148
frame #3: 0x00000001cf00723c JavaScriptCore`***::ParkingLot::parkConditionallyImpl(void const*, ***::ScopedLambda<bool ()> const&, ***::ScopedLambda<void ()> const&, ***::TimeWithDynamicClockType const&) + 1928
frame #4: 0x00000001cefddfc4 JavaScriptCore`bool ***::Condition::waitUntil<***::Lock>(***::Lock&, ***::TimeWithDynamicClockType const&) + 192
frame #5: 0x00000001cefde334 JavaScriptCore`***::Detail::CallableWrapper<***::AutomaticThread::start(***::AbstractLocker const&)::$_0, void>::call() + 208
frame #6: 0x00000001cf01f8f8 JavaScriptCore`***::Thread::entryPoint(***::Thread::NewThreadContext*) + 260
frame #7: 0x00000001cf021444 JavaScriptCore`***::wtfThreadEntryPoint(void*) + 16
frame #8: 0x00000001bfc19840 libsystem_pthread.dylib`_pthread_start + 168