Skip to content

Nested runBlocking and init block #1861

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 Mar 12, 2020 · 1 comment
Closed

Nested runBlocking and init block #1861

fvasco opened this issue Mar 12, 2020 · 1 comment

Comments

@fvasco
Copy link
Contributor

fvasco commented Mar 12, 2020

I found a trouble in the follow code:

import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking

object A {
    var ok = false

    init {
        println(1)
        runBlocking { }
        println(2)
        ok = true
        println(3)
    }

}

fun main() {
    runBlocking {
        launch { println(A.ok) }
        launch { println(A.ok) }
    }
}

I expect:

1
2
3
true
true

but it is:

1
false
2
3
true

openjdk version "11.0.6" 2020-01-14
kotlin 1.3.70
coroutine 1.3.4

@qwwdfsad
Copy link
Collaborator

This is the same problem as described #1697 (see my explanation here)

It is basically a tradeoff between "nested runblocking just hangs the thread and you have a deadlock" and "nested runblocking sometimes confuses people"

Closing as duplicate, let's move our discussion there

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

2 participants