DISPATCH_INTERNAL_CRASH on _dispatch_queue_cleanup

I'm getting crash on pthread_exit for one of threads which was spawned by dispatch.

This thread is next to above and looks healty:

configuration of cmake:

        -DCMAKE_SYSTEM_NAME="Android" \
        -DCMAKE_TOOLCHAIN_FILE=$NDK/build/cmake/android.toolchain.cmake \
        -DCMAKE_SYSROOT=$NDK/toolchains/llvm/prebuilt/darwin-x86_64/sysroot \
        -G "Ninja" $CURRENT_DIR


Please give me some information. where I should start debugging and looking at?

I'm sorry, the UI makes it difficult to understand what exactly is going on. Are you sure that you are looking at the crashing thread? What does frame 0 look like it is doing? Did you look at the disassembly of the innermost frame on the crashing thread?

I think it would be useful to get the equivalent of WinDBG's ~*k or gdb's thread apply all bt. That might shed some clue. Also, Dispatch has some pretty good messages on what went wrong, so providing the actual printed message of libdispatch would also be useful.

it was crashing inside _dispatch_queue_cleanup(void *ctxt) function with message: "Premature thread exit while a dispatch queue is running".

Hm.. I was trying reproduce crash, but can't anymore. Before it was 50% chances.

That sounds like a race condition. It sounds like pthread_cancel may have been invoked on the thread handle? @adierking may have some thoughts on this as well.

Android Studio :) It has lldb debugger.

Finally got crash, here is backtrace:

(lldb) bt
* thread #48, name = 'Thread-8', stop reason = signal SIGTRAP
  * frame #0: 0x0000007d45482240 libCoreFoundation.so`_libdispatch_tsd_cleanup [inlined] _dispatch_queue_cleanup(ctxt=0x0000007d25e16500) at queue.c:7111
    frame #1: 0x0000007d45482240 libCoreFoundation.so`_libdispatch_tsd_cleanup(ctx=0x0000007d412b22a8) at queue.c:7397
    frame #2: 0x0000007de66484fc libc.so`pthread_key_clean_all() + 124
    frame #3: 0x0000007de6647f4c libc.so`pthread_exit + 76
    frame #4: 0x0000007de6647e24 libc.so`__pthread_start(void*) + 308
    frame #5: 0x0000007de65e6a50 libc.so`__start_thread + 72

I can check something more.

Actually, are you calling pthread_exit yourself on a thread that is managed by dispatch? If so, that is invalid. Can you please provide some source code that demonstrates the issue?

No, I'm not.

Currently no clear case.

Hm, also I spotted some strange message in my logs, maybe it's linked

I know it's JVM, but I had not seen such messages with older dispatch (much older).

I think that there is something wrong in the android setup (not in dispatch) - the stack trace indicates that pthread_exit was invoked, the question is how.

Thank you. I will try investigate on my side.