You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
The kdoc for Dispatchers.Unconfined states the following:
/** * [...] * * ### Event loop * Event loop semantics is a purely internal concept and have no guarantees on the order of execution * except that all queued coroutines will be executed on the current thread in the lexical scope of the outermost * unconfined coroutine. * * For example, the following code: * ``` * withContext(Dispatchers.Unconfined) { * println(1) * withContext(Dispatchers.Unconfined) { // Nested unconfined * println(2) * } * println(3) * } * println("Done") * ``` * Can print both "1 2 3" and "1 3 2", this is an implementation detail that can be changed. * But it is guaranteed that "Done" will be printed only when both `withContext` are completed.*/
I believe this incorrect and you are in fact guaranteed that it will print "1 2 3". This is because withContext actually produces a result so something like this would then be broken:
withContext(Dispatchers.Unconfined) {
val result = withContext(Dispatchers.Unconfined) { 1+1}
println(result)
}
Instead I believe the author meant to use launch inside of the outer withContext which would indeed have an undefined behavior.
Provide a Reproducer
I can obviously not prove this but running the snippet from the documentation does produce "1 2 3" as it should.
The text was updated successfully, but these errors were encountered:
The previous example used `withContext` which is guaranteed to complete before the code after it runs. The updated example uses `launch` which I think was originally intended.
Fixes#3605
Fixes https://youtrack.jetbrains.com/issue/KT-47032/
Describe the bug
The kdoc for
Dispatchers.Unconfined
states the following:I believe this incorrect and you are in fact guaranteed that it will print "1 2 3". This is because
withContext
actually produces a result so something like this would then be broken:Instead I believe the author meant to use
launch
inside of the outerwithContext
which would indeed have an undefined behavior.Provide a Reproducer
I can obviously not prove this but running the snippet from the documentation does produce "1 2 3" as it should.
The text was updated successfully, but these errors were encountered: