Unverified Commit 1cbde7b1 authored by Igor Demin's avatar Igor Demin Committed by GitHub

Make dispatcherToBlockOn dispatcher based on daemon threads. (#814)

After https://github.com/JetBrains/skiko/pull/798, we changed Dispatcher.IO to custom dispatcher.

Dispatcher.IO is based on daemon threads (thread that receives InterruptException on application exit).

Our new dispatcher isn't. The proof:
```
import java.util.concurrent.Executors
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.runBlocking

fun main() {
    runBlocking(Dispatchers.IO) {
        println(Thread.currentThread().isDaemon) // prints true
    }
    runBlocking(Executors.newCachedThreadPool().asCoroutineDispatcher()) {
        println(Thread.currentThread().isDaemon) // prints false
    }
    val defaultFactory = Executors.defaultThreadFactory()
    runBlocking(Executors.newCachedThreadPool {
        defaultFactory.newThread(it).apply {
            isDaemon = true
        }
    }.asCoroutineDispatcher()) {
        println(Thread.currentThread().isDaemon) // prints true
    }
}
```

This PR makes threads daemon.

Reported in [Kotlin Slack](https://kotlinlang.slack.com/archives/C01D6HTPATV/p1697400320739369) - applications exits too long.
parent 91c3db84
...@@ -3,6 +3,7 @@ package org.jetbrains.skiko.redrawer ...@@ -3,6 +3,7 @@ package org.jetbrains.skiko.redrawer
import kotlinx.coroutines.asCoroutineDispatcher import kotlinx.coroutines.asCoroutineDispatcher
import java.util.concurrent.Executors import java.util.concurrent.Executors
private val defaultFactory = Executors.defaultThreadFactory()
/** /**
* Dispatcher intended for use in coroutines that blocks (not suspends) for indefinite amount of time. * Dispatcher intended for use in coroutines that blocks (not suspends) for indefinite amount of time.
...@@ -10,4 +11,8 @@ import java.util.concurrent.Executors ...@@ -10,4 +11,8 @@ import java.util.concurrent.Executors
* We can't use `Dispatchers.IO` here because it's limited by 64 threads and under heavy IO workload all of them might be occupied * We can't use `Dispatchers.IO` here because it's limited by 64 threads and under heavy IO workload all of them might be occupied
* which leads to skipped frames * which leads to skipped frames
*/ */
internal val dispatcherToBlockOn = Executors.newCachedThreadPool().asCoroutineDispatcher() internal val dispatcherToBlockOn = Executors.newCachedThreadPool {
\ No newline at end of file defaultFactory.newThread(it).apply {
isDaemon = true
}
}.asCoroutineDispatcher()
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment