Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[native-mt] suspendCoroutine: CoroutinesInternalError: Fatal exception in coroutines machinery for DispatchedContinuation #2363

Closed
william-reed opened this issue Nov 3, 2020 · 6 comments
Labels

Comments

@william-reed
Copy link

I am seeing a crash occurring inside of a suspendCoroutine block using 1.3.9-native-mt-2. The issue is shown in this sample project https://github.com/shit-city/multiplatform/tree/suspendCoroutine with a README explaining the relevant files.

isolated example code:

object Client {
    private val scope = CoroutineScope(Dispatchers.Default)

    fun subscribe() {
        scope.launch {
            suspendCoroutine<String> { cont ->
                MockCallback.listen({ result: String ->
                    cont.resume(result)
                }.freeze())
            }
        }
    }
}

/**
 * This is mocking an iOS based callback interface where the callback occurs on a different thread
 */
private object MockCallback {
    fun listen(onResponse: (String) -> Unit) {
        dispatch_async(dispatch_get_main_queue()) {
            onResponse("hello")
        }
    }
}
Uncaught Kotlin exception: kotlinx.coroutines.CoroutinesInternalError: Fatal exception in coroutines machinery for DispatchedContinuation[WorkerCoroutineDispatcherImpl@1e66948, Continuation @ $subscribe$lambda-0COROUTINE$0]. Please read KDoc to 'handleFatalException' method and report this incident to maintainers
Uncaught Kotlin exception: kotlin.native.IncorrectDereferenceException: illegal attempt to access non-shared foo.bar.kotlin.mobile.MockCallback.$listen$lambda-0$FUNCTION_REFERENCE$1@10318c8 from other thread
    at 0   KotlinAppShared                     0x000000010b0c8dec kfun:kotlin.Error#<init>(kotlin.String?;kotlin.Throwable?){} + 124 (/Users/teamcity/buildAgent/work/cae0e6559deed4c4/runtime/src/main/kotlin/kotlin/Exceptions.kt:14:63)
    at 1   KotlinAppShared                     0x000000010b238f3c kfun:kotlinx.coroutines.CoroutinesInternalError#<init>(kotlin.String;kotlin.Throwable){} + 124 (/opt/buildAgent/work/44ec6e850d5c63f0/kotlinx-coroutines-core/common/src/Exceptions.common.kt:28:77)
    at 2   KotlinAppShared                     0x000000010b287089 kfun:kotlinx.coroutines.DispatchedTask#handleFatalException(kotlin.Throwable?;kotlin.Throwable?){} + 953 (/opt/buildAgent/work/44ec6e850d5c63f0/kotlinx-coroutines-core/common/src/internal/DispatchedTask.kt:93:22)
    at 3   KotlinAppShared                     0x000000010b286c67 kfun:kotlinx.coroutines.DispatchedTask#run(){} + 3543 (/opt/buildAgent/work/44ec6e850d5c63f0/kotlinx-coroutines-core/common/src/internal/DispatchedTask.kt:64:13)
    at 4   KotlinAppShared                     0x000000010b234406 kfun:kotlinx.coroutines.EventLoopImplBase#processNextEvent(){}kotlin.Long + 838 (/opt/buildAgent/work/44ec6e850d5c63f0/kotlinx-coroutines-core/common/src/EventLoop.common.kt:274:18)
    at 5   KotlinAppShared                     0x000000010b2a428f kfun:kotlinx.coroutines#runEventLoop(kotlinx.coroutines.EventLoop?;kotlin.Function0<kotlin.Boolean>){} + 911 (/opt/buildAgent/work/44ec6e850d5c63f0/kotlinx-coroutines-core/native/src/Builders.kt:80:40)
    at 6   KotlinAppShared                     0x000000010b2acf27 kfun:kotlinx.coroutines.WorkerCoroutineDispatcherImpl.start$lambda-0#internal + 407 (/opt/buildAgent/work/44ec6e850d5c63f0/kotlinx-coroutines-core/native/src/Workers.kt:49:17)
    at 7   KotlinAppShared                     0x000000010b2ad10e kfun:kotlinx.coroutines.WorkerCoroutineDispatcherImpl.$start$lambda-0$FUNCTION_REFERENCE$35.invoke#internal + 62 (/opt/buildAgent/work/44ec6e850d5c63f0/kotlinx-coroutines-core/native/src/Workers.kt:47:24)
    at 8   KotlinAppShared                     0x000000010b2ad16e kfun:kotlinx.coroutines.WorkerCoroutineDispatcherImpl.$start$lambda-0$FUNCTION_REFERENCE$35.$<bridge-UNN>invoke(){}#internal + 62 (/opt/buildAgent/work/44ec6e850d5c63f0/kotlinx-coroutines-core/native/src/Workers.kt:47:24)
    at 9   KotlinAppShared                     0x000000010b0ff3b7 WorkerLaunchpad + 183 (/Users/teamcity/buildAgent/work/cae0e6559deed4c4/runtime/src/main/kotlin/kotlin/native/concurrent/Internal.kt:69:54)
    at 10  KotlinAppShared                     0x000000010b1fad7f _ZN6Worker19processQueueElementEb + 3135
    at 11  KotlinAppShared                     0x000000010b1fa126 _ZN12_GLOBAL__N_113workerRoutineEPv + 54
    at 12  libsystem_pthread.dylib             0x00007fff522b7109 _pthread_start + 148
    at 13  libsystem_pthread.dylib             0x00007fff522b2b8b thread_start + 15
Caused by: kotlin.native.concurrent.InvalidMutabilityException: mutation attempt of frozen foo.bar.kotlin.mobile.Client.$subscribe$lambda-0COROUTINE$0@543688
     at 0   KotlinAppShared                     0x000000010b0cfedd kfun:kotlin.Throwable#<init>(kotlin.String?){} + 93 (/Users/teamcity/buildAgent/work/cae0e6559deed4c4/runtime/src/main/kotlin/kotlin/Throwable.kt:23:37)   at 0   KotlinAppShared                     0x000000010b0cfedd kfun:kotlin.Throwable#<init>(kotlin.String?){} + 93 (/Users/teamcity/buildAgent/work/cae0e6559deed4c4/runtime/src/main/kotlin/kotlin/Throwable.kt:23:37)

    at 1   KotlinAppShared                     0x000000010b0c8efb kfun:kotlin.Exception#<init>(kotlin.String?){} + 91 (/Users/teamcity/buildAgent/work/cae0e6559deed4c4/runtime/src/main/kotlin/kotlin/Exceptions.kt:23:44)    at 1   KotlinAppShared                     0x000000010b0c8efb kfun:kotlin.Exception#<init>(kotlin.String?){} + 91 (/Users/teamcity/buildAgent/work/cae0e6559deed4c4/runtime/src/main/kotlin/kotlin/Exceptions.kt:23:44)

    at 2   KotlinAppShared                     0x000000010b0c90bb kfun:kotlin.RuntimeException#<init>(kotlin.String?){} + 91 (/Users/teamcity/build    at 2   KotlinAppShared                     0x000000010b0c90bb kfun:kotlin.RuntimeException#<init>(kotlin.String?){} + 91 (/Users/teamcity/buildAgent/work/cae0e6559deed4c4/runtime/src/main/kotlin/kotlin/Exceptions.kt:34:44)
Agent/work/cae0e6559deed4c4/runtime/src/main/kotlin/kotlin/Exceptions.kt:34:44)
    at 3   KotlinAppShared                     0x000000010b0fb73b kfun:kotlin.native.IncorrectDereferenceException#<init>(kotlin.String){} + 91 (/Users/teamcity/buildAgent/work/cae0e6559deed4c4/runtime/src/main/kotlin/kotlin/native/Runtime.kt:30:36)
    at 3   KotlinAppShared                     0x000000010b0fdf5b kfun:kotlin.native.concurrent.InvalidMutabilityException#<init>(kotlin.String){} + 91 (/Users/teamcity/buildAgent/work/cae0e6559deed4c4/runtime/src/main/kotlin/kotlin/native/concurrent/Freezing.kt:22:60)
    at 4   KotlinAppShared                     0x000000010b0ffacd ThrowIllegalObjectSharingException + 621 (/Users/teamcity/buildAgent/work/cae0e6559deed4c4/runtime/src/main/kotlin/kotlin/native/concurrent/Internal.kt:98:11)    at 4   KotlinAppShared                     0x000000010b0ff812 ThrowInvalidMutabilityException + 690 (/Users/teamcity/buildAgent/work/cae0e6559deed4c4/runtime/src/main/kotlin/kotlin/native/concurrent/Internal.kt:92:11)

    at 5   KotlinAppShared                     0x000000010b1e3802 _ZN12_GLOBAL__N_128throwIllegalSharingExceptionEP9ObjHeader + 34
    at 6   KotlinAppShared                     0x000000010b2129ac _ZNK16KRefSharedHolder3refEv + 124
    at 5   KotlinAppShared                     0x000000010b1f60bc MutationCheck + 108
    at 7   KotlinAppShared                     0x000000010b0762ea _ZL39Kotlin_Interop_unwrapKotlinObjectHolderP11objc_object + 42
    at 6   KotlinAppShared                     0x000000010b0f1726 kfun:kotlin.coroutines.native.internal.ContinuationImpl.<set-intercepted>#internal + 102 (/Users/teamcity/buildAgent/work/cae0e6559deed4c4/runtime/src/main/kotlin/kotlin/coroutines/ContinuationImpl.kt:98:13)    at 8   KotlinAppShared                     0x000000010b073a8a _4b6f746c696e4170703a6d6f62696c65_knbridge4 + 138 (/Users/williamreed/dev/multiplatform/mobile/src/iosMain/kotlin/foo/bar/kotlin/mobile/Client.kt:32:51)

    at 9   libdispatch.dylib                   0x000000010bc15f11 _dispatch_call_block_and_release + 12
    at 7   KotlinAppShared                     0x000000010b0f1d64 kfun:kotlin.coroutines.native.internal.ContinuationImpl#releaseIntercepted(){} + 452 (/Users/teamcity/buildAgent/work/cae0e6559deed4c4/runtime/src/main/kotlin/kotlin/coroutines/ContinuationImpl.kt:110:9)
    at 10  libdispatch.dylib                   0x000000010bc16e8e _dispatch_client_callout + 8
    at 11  libdispatch.dylib                   0x000000010bc24d97 _dispatch_main_queue_callback_4CF + 1149
    at 8   KotlinAppShared                     0x000000010b0f0746 kfun:kotlin.coroutines.native.internal.BaseContinuationImpl#resumeWith(kotlin.Result<kotlin.Any?>){} + 1254 (/Users/teamcity/buildAgent/work/cae0e6559deed4c4/runtime/src/main/kotlin/kotlin/coroutines/ContinuationImpl.kt:36:17)    at 12  CoreFoundation                      0x00007fff23da1869 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9

    at 13  CoreFoundation                      0x00007fff23d9c3b9 __CFRunLoopRun + 2041    at 9   KotlinAppShared                     0x000000010b286982 kfun:kotlinx.coroutines.DispatchedTask#run(){} + 2802 (/opt/buildAgent/work/44ec6e850d5c63f0/kotlinx-coroutines-core/common/src/internal/DispatchedTask.kt:39:50)

    at 14  CoreFoundation                      0x00007fff23d9b8a4 CFRunLoopRunSpecific + 404
    at 10  KotlinAppShared                     0x000000010b234406 kfun:kotlinx.coroutines.EventLoopImplBase#processNextEvent(){}kotlin.Long + 838 (/opt/buildAgent/work/44ec6e850d5c63f0/kotlinx-coroutines-core/common/src/EventLoop.common.kt:274:18)
    at 15  GraphicsServices                    0x00007fff38c39bbe GSEventRunModal + 139
    at 16  UIKitCore                           0x00007fff49325968 UIApplicationMain + 1605
    ... and 9 more common stack frames skipped
    at 17  KotlinApp                           0x000000010adfc0db main + 75 (/Users/williamreed/dev/multiplatform/apple/KotlinApp/AppDelegate.swift:5:7)
    at 18  libdyld.dylib                       0x00007fff520ce1fd start + 1
(lldb) 
@william-reed
Copy link
Author

This appears to be a similar bug report https://youtrack.jetbrains.com/issue/KT-42876

@qwwdfsad qwwdfsad added the native label Dec 7, 2020
@qwwdfsad
Copy link
Collaborator

qwwdfsad commented Dec 8, 2020

I've run your code with Kotlin 1.4.0 and coroutines 1.4.2-native-mt as unit test and it seems to work as expected.
Could you please update your sample and see if the problem is gone?

If it's not, then it's likely to be a Swift interop issue that should be investigated separately

@varahash
Copy link

varahash commented Dec 9, 2020

@qwwdfsad Here is unit test reproducer (kotlin 1.4.20):

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import platform.darwin.dispatch_async
import platform.darwin.dispatch_get_global_queue
import platform.posix.QOS_CLASS_UTILITY
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine
import kotlin.native.concurrent.freeze
import kotlin.test.Test

class CoroutinesTest {
    @Test
    fun testCoroutines() {
        val job = CoroutineScope(Dispatchers.Default).launch {
            val cl = MyClass()
            cl.doWork()
        }
        while (job.isActive) {
        }
    }
}

class MyClass {
    suspend fun doWork() {
        suspendCoroutine<Unit> { continuation ->
            continuation.freeze()
            dispatch_async(dispatch_get_global_queue(QOS_CLASS_UTILITY.toLong(), 0), {
                continuation.resume(Unit)
            }.freeze())
        }
    }
}

Fails with following error:

Uncaught Kotlin exception: kotlinx.coroutines.CoroutinesInternalError: Fatal exception in coroutines machinery for DispatchedContinuation[WorkerCoroutineDispatcherImpl@2e8befb8, Continuation @ $testCoroutines$lambda-0COROUTINE$0]. Please read KDoc to 'handleFatalException' method and report this incident to maintainers
Uncaught Kotlin exception: kotlin.native.concurrent.InvalidMutabilityException: mutation attempt of frozen kotlin.coroutines.SafeContinuation@d3108df8
Invalid connection: com.apple.coresymbolicationd
    at 0   test.kexe                           0x000000010e4ccb2e kfun:kotlin.Error#<init>(kotlin.String?;kotlin.Throwable?){} + 126 (/Users/teamcity1/teamcity_work/f01984a9f5203417/runtime/src/main/kotlin/kotlin/Exceptions.kt:14:63)
    at 1   test.kexe                           0x000000010e65795e kfun:kotlinx.coroutines.CoroutinesInternalError#<init>(kotlin.String;kotlin.Throwable){} + 126 (/opt/buildAgent/work/44ec6e850d5c63f0/kotlinx-coroutines-core/common/src/Exceptions.common.kt:28:77)
    at 2   test.kexe                           0x000000010e678e24 kfun:kotlinx.coroutines.DispatchedTask#handleFatalException(kotlin.Throwable?;kotlin.Throwable?){} + 900 (/opt/buildAgent/work/44ec6e850d5c63f0/kotlinx-coroutines-core/common/src/internal/DispatchedTask.kt:93:22)
    at 3   test.kexe                           0x000000010e678a37 kfun:kotlinx.coroutines.DispatchedTask#run(){} + 3223 (/opt/buildAgent/work/44ec6e850d5c63f0/kotlinx-coroutines-core/common/src/internal/DispatchedTask.kt:64:13)
    at 4   test.kexe                           0x000000010e652eb8 kfun:kotlinx.coroutines.EventLoopImplBase#processNextEvent(){}kotlin.Long + 840 (/opt/buildAgent/work/44ec6e850d5c63f0/kotlinx-coroutines-core/common/src/EventLoop.common.kt:274:18)
    at 5   test.kexe                           0x000000010e689b2b kfun:kotlinx.coroutines#runEventLoop(kotlinx.coroutines.EventLoop?;kotlin.Function0<kotlin.Boolean>){} + 843 (/opt/buildAgent/work/44ec6e850d5c63f0/kotlinx-coroutines-core/native/src/Builders.kt:80:40)
    at 6   test.kexe                           0x000000010e6925e9 kfun:kotlinx.coroutines.WorkerCoroutineDispatcherImpl.start$lambda-0#internal + 409 (/opt/buildAgent/work/44ec6e850d5c63f0/kotlinx-coroutines-core/native/src/Workers.kt:49:17)
    at 7   test.kexe                           0x000000010e6927e0 kfun:kotlinx.coroutines.WorkerCoroutineDispatcherImpl.$start$lambda-0$FUNCTION_REFERENCE$35.invoke#internal + 64 (/opt/buildAgent/work/44ec6e850d5c63f0/kotlinx-coroutines-core/native/src/Workers.kt:47:24)
    at 8   test.kexe                           0x000000010e692840 kfun:kotlinx.coroutines.WorkerCoroutineDispatcherImpl.$start$lambda-0$FUNCTION_REFERENCE$35.$<bridge-UNN>invoke(){}#internal + 64 (/opt/buildAgent/work/44ec6e850d5c63f0/kotlinx-coroutines-core/native/src/Workers.kt:47:24)
    at 9   test.kexe                           0x000000010e503589 WorkerLaunchpad + 185 (/Users/teamcity1/teamcity_work/f01984a9f5203417/runtime/src/main/kotlin/kotlin/native/concurrent/Internal.kt:69:54)
    at 10  test.kexe                           0x000000010e61d65f _ZN6Worker19processQueueElementEb + 3135

If we omit freeze() then the test will crash with the following error:

Uncaught Kotlin exception: kotlin.native.IncorrectDereferenceException: illegal attempt to access non-shared com.example.MyClass.$doWork$<anonymous>_6$FUNCTION_REFERENCE$0@6cf0ec68 from other thread
Invalid connection: com.apple.coresymbolicationd
    at 0   test.kexe                           0x000000010d4f1c9f kfun:kotlin.Throwable#<init>(kotlin.String?){} + 95 (/Users/teamcity1/teamcity_work/f01984a9f5203417/runtime/src/main/kotlin/kotlin/Throwable.kt:23:37)
    at 0   test.kexe                           0x000000010d4f1c9f kfun:kotlin.Throwable#<init>(kotlin.String?){} + 95 (/Users/teamcity1/teamcity_work/f01984a9f5203417/runtime/src/main/kotlin/kotlin/Throwable.kt:23:37)

@ln-12
Copy link

ln-12 commented Jan 22, 2021

@qwwdfsad I run into the same problem as described here. You can have a look at the ready to run project here (please note to also clone the contained submodule). When running and disconnecting the network, this line throws the following error:

Uncaught Kotlin exception: kotlin.native.IncorrectDereferenceException: illegal attempt to access non-shared com.github.ln12.connectivitymanagersample.shared.ConnectivityManager.$start$lambda-5$lambda-4$lambda-3$FUNCTION_REFERENCE$2@1b8b788 from other thread

I am using implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.2-native-mt").

@AlexITC
Copy link

AlexITC commented Mar 11, 2021

Any updates? our team has hit the same issue.

@qwwdfsad
Copy link
Collaborator

The suggested workaround is to use non-native-mt version along with a new memory model: #462 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants