Skip to content

Kdoc for Dispatchers.Unconfined is possibly incorrect #3605

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
ansman opened this issue Jan 27, 2023 · 0 comments · Fixed by #3607
Closed

Kdoc for Dispatchers.Unconfined is possibly incorrect #3605

ansman opened this issue Jan 27, 2023 · 0 comments · Fixed by #3607
Labels

Comments

@ansman
Copy link
Contributor

ansman commented Jan 27, 2023

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.

@ansman ansman added the bug label Jan 27, 2023
ansman added a commit to ansman/kotlinx.coroutines that referenced this issue Jan 30, 2023
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.

This fixes Kotlin#3605 and [KT-47032](https://youtrack.jetbrains.com/issue/KT-47032/Incorrect-code-example-for-Dispatchers.Unconfined-in-kotlinx.coroutines-docs)
dkhalanskyjb pushed a commit that referenced this issue Feb 1, 2023
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/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant