Skip to content

withTimeoutOrNull+channel hangs #1281

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

Closed
fvasco opened this issue Jun 19, 2019 · 1 comment
Closed

withTimeoutOrNull+channel hangs #1281

fvasco opened this issue Jun 19, 2019 · 1 comment

Comments

@fvasco
Copy link
Contributor

fvasco commented Jun 19, 2019

I detected a strange behaviour in the follow code:

import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.channels.*
import kotlinx.coroutines.delay
import kotlinx.coroutines.withTimeoutOrNull
import kotlinx.coroutines.yield

suspend fun main() {
    repeat(100) { step ->
        println("step $step")

        val producer = GlobalScope.produce(capacity = 1024 * 1024) {
            repeat(1_000_000) {
                send(it)
                yield()
            }
            println("End producer")
        }

        val consumer = GlobalScope.actor<Int?>(capacity = Channel.CONFLATED) {
            while (true) receive()
        }

        producer.receive()
        delay(200)

        withTimeoutOrNull(100) {
            while (true) consumer.send(producer.receiveOrNull())
        }
        producer.cancel()
    }
}

it hangs with output

step 0
step 1
step 2
End producer

Kotlin 1.3.40
Coroutine 1.2.2
Java adoptopenjdk-12-jdk-hotspot

@fvasco
Copy link
Contributor Author

fvasco commented Jun 20, 2019

As documented receiveOrNull "does not check for cancellation when it is not suspended"

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

No branches or pull requests

1 participant