Skip to content
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

Problem with delay with kotlinx-coroutines-javafx #3106

Closed
NkD opened this issue Dec 22, 2021 · 1 comment
Closed

Problem with delay with kotlinx-coroutines-javafx #3106

NkD opened this issue Dec 22, 2021 · 1 comment
Labels

Comments

@NkD
Copy link

@NkD NkD commented Dec 22, 2021

versions:

  • java - 17.0.1
  • javafx - 17.0.1
  • kotlin-stdlib-jdk8 - 1.6.10
  • kotlinx-coroutines-core - 1.6.0
  • kotlinx-coroutines-javafx - 1.6.0

smallest example of bug

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

fun main() {
    val jobCoroutineScope = CoroutineScope(Dispatchers.IO)
    println("started")
    for (job in 1..3) {
        jobCoroutineScope.launch {
            println("job[$job] started")
            for (step in 1..10) {
                  delay(100)
            }
            println("job[$job] ended")
        }
    }
    Thread.sleep(2000)
    println("ended")
}

BUG: Some jobs stay blocked forever in delay function.

I've been trying to figure out what the problem is. And it is because the delay is redirected to kotlinx.coroutines.javafx.JavaFxDispatcher#schedule, where the delay is implemented as a TimeLine for JavaFx. However, this Timeline is started outside of the JavaFX Thread and this will lead to unexpected results.

Workarounds:

  • wrap delay as withContext(Dispatchers.Main) { delay(100) } - Dispatchers.Main is JavaFx thread in this case.
  • add SysProp kotlinx.coroutines.main.delay=false - switch off redirecting to JavaFxDispatcher (Dispatchers.Main)
  • remove kotlinx-coroutines-javafx dependency (not possible for me)
@miikeat
Copy link

@miikeat miikeat commented Jan 11, 2022

This bug caused our tests to get stuck only on Jenkins!
Thanks for the Workarounds.

We just needed to add this line to our build.gradle.kts to get them working again:

tasks {
  test {
    systemProperty("kotlinx.coroutines.main.delay", "false")
  }
}

qwwdfsad added a commit that referenced this issue Jan 11, 2022
The approach from 1.6.0 has proven itself as unstable and multiple hard-to-understand bugs have been reported:

* JavaFx timer doesn't really work outside the main thread
* The frequent initialization pattern "runBlocking { doSomethingThatMayCallDelay() }" used on the main thread during startup now silently deadlocks
* The latter issue was reported both by Android and internal JB Compose users
* The provided workaround with system property completely switches off the desired behaviour that e.g. Compose may rely on, potentially introducing new sources of invalid behaviour

The original benefits does not outweigh these pitfalls, so the decision is to revert this changes in the minor release

Fixes #3113
Fixes #3106
yorickhenning pushed a commit to yorickhenning/kotlinx.coroutines that referenced this issue Jan 28, 2022
…tlin#3131)

The approach from 1.6.0 has proven itself as unstable and multiple hard-to-understand bugs have been reported:

* JavaFx timer doesn't really work outside the main thread
* The frequent initialization pattern "runBlocking { doSomethingThatMayCallDelay() }" used on the main thread during startup now silently deadlocks
* The latter issue was reported both by Android and internal JB Compose users
* The provided workaround with system property completely switches off the desired behaviour that e.g. Compose may rely on, potentially introducing new sources of invalid behaviour

The original benefits does not outweigh these pitfalls, so the decision is to revert this changes in the minor release

Fixes Kotlin#3113
Fixes Kotlin#3106
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

3 participants