Skip to content

native-mt: Select fail when crossing threads #2529

Closed
@whyoleg

Description

@whyoleg

Simplified rsocket-kotlin code to minimal example:

@Test
fun selectTest() = test {
    val priorityChannel = Channel<String>(Int.MAX_VALUE)
    val commonChannel = Channel<String>(Int.MAX_VALUE)

    GlobalScope.launch {
        repeat(5) {
            priorityChannel.send("123")
            delay(100)
        }
    }

    GlobalScope.launch {
        repeat(5) {
            commonChannel.send("NOT")
            delay(100)
        }
    }

    delay(200)

    GlobalScope.launch(Dispatchers.Unconfined) {
        repeat(10) {
            val v = select<String> {
                priorityChannel.onReceive { it }
                commonChannel.onReceive { it }
            }
            println(v)
        }
    }.join()
}

Error:

Uncaught Kotlin exception: kotlin.native.IncorrectDereferenceException: illegal attempt to access non-shared io.rsocket.kotlin.core.RSocketTest.$selectTest$lambda-14$lambda-13$<anonymous>_6_8COROUTINE$23@428a9a08 bound to `worker 1` from `DefaultDispatcher`
    at 0   test.kexe                           0x0000000102d7b8b0 kfun:kotlin.native.concurrent.WorkerBoundReference#<get-value>(){}1:0 + 1264
    at 1   test.kexe                           0x0000000102e23d83 kfun:kotlinx.coroutines.internal.ShareableObject#useRef(){}1:0 + 115
    at 2   test.kexe                           0x0000000102e248b4 kfun:kotlinx.coroutines.internal.ShareableBlock.invoke#internal.99 + 100
    at 3   test.kexe                           0x0000000102d767da kfun:kotlin.coroutines.intrinsics.<no name provided>_16.invokeSuspend_19#internal + 250
    at 4   test.kexe                           0x0000000102d757e3 kfun:kotlin.coroutines.native.internal.BaseContinuationImpl#resumeWith(kotlin.Result<kotlin.Any?>){} + 211
    at 5   test.kexe                           0x0000000102e10a86 kfun:kotlinx.coroutines.internal#resumeCancellableWith@kotlin.coroutines.Continuation<0:0>(kotlin.Result<0:0>;kotlin.Function1<kotlin.Throwable,kotlin.Unit>?){0§<kotlin.Any?>} + 854
    at 6   test.kexe                           0x0000000102dd4001 kfun:kotlinx.coroutines#startCoroutineImpl(kotlinx.coroutines.CoroutineStart;0:1;kotlin.coroutines.Continuation<0:0>;kotlin.Function1<kotlin.Throwable,kotlin.Unit>?;kotlin.coroutines.SuspendFunction1<0:1,0:0>){0§<kotlin.Any?>;1§<kotlin.Any?>} + 385
    at 7   test.kexe                           0x0000000102e1c5ed kfun:kotlinx.coroutines.startCoroutine#internal + 493
    at 8   test.kexe                           0x0000000102e1c3bb kfun:kotlinx.coroutines#startCoroutine(kotlinx.coroutines.CoroutineStart;0:1;kotlin.coroutines.Continuation<0:0>;kotlin.Function1<kotlin.Throwable,kotlin.Unit>?;kotlin.coroutines.SuspendFunction1<0:1,0:0>){0§<kotlin.Any?>;1§<kotlin.Any?>} + 155
    at 9   test.kexe                           0x0000000102df113a kfun:kotlinx.coroutines.channels.AbstractChannel.ReceiveSelect.completeResumeReceive#internal + 218
    at 10  test.kexe                           0x0000000102df298e kfun:kotlinx.coroutines.channels.AbstractSendChannel#offerInternal(1:0){}kotlin.Any + 254
    at 11  test.kexe                           0x0000000102dfbed2 kfun:kotlinx.coroutines.channels.LinkedListChannel#offerInternal(1:0){}kotlin.Any + 146
    at 12  test.kexe                           0x0000000102df2d32 kfun:kotlinx.coroutines.channels.AbstractSendChannel#send(1:0){} + 146
    at 13  test.kexe                           0x0000000102e99aec kfun:io.rsocket.kotlin.core.RSocketTest.$selectTest$lambda-14$lambda-11COROUTINE$21.invokeSuspend#internal + 828
    at 14  test.kexe                           0x0000000102d757e3 kfun:kotlin.coroutines.native.internal.BaseContinuationImpl#resumeWith(kotlin.Result<kotlin.Any?>){} + 211
    at 15  test.kexe                           0x0000000102e10f2d kfun:kotlinx.coroutines.DispatchedTask#run(){} + 669
    at 16  test.kexe                           0x0000000102ddf2b1 kfun:kotlinx.coroutines.EventLoopImplBase#processNextEvent(){}kotlin.Long + 3217
    at 17  test.kexe                           0x0000000102e1cf60 kfun:kotlinx.coroutines#runEventLoop(kotlinx.coroutines.EventLoop?;kotlin.Function0<kotlin.Boolean>){} + 544
    at 18  test.kexe                           0x0000000102e21ea0 kfun:kotlinx.coroutines.WorkerCoroutineDispatcherImpl.$start$lambda-0$FUNCTION_REFERENCE$273.$<bridge-UNN>invoke(){}#internal + 256
    at 19  test.kexe                           0x0000000102f5155f _ZN6Worker19processQueueElementEb + 1007
    at 20  test.kexe                           0x0000000102f5111d _ZN12_GLOBAL__N_113workerRoutineEPv + 45
    at 21  libsystem_pthread.dylib             0x00007fff2037a950 _pthread_start + 224
    at 22  libsystem_pthread.dylib             0x00007fff2037647b thread_start + 15

Kotlin version: 1.4.21
Coroutines version: 1.4.2-native-mt

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions