Autodesk / coroutineworker

Kotlin Coroutine-based workers for native
Apache License 2.0
370 stars 24 forks source link

iOS app crashes when using IODispatcher #82

Closed belousovgm closed 3 years ago

belousovgm commented 3 years ago

When I trying to run this code:

GlobalScope.launch {
    CoroutineWorker.withContext(Dispatchers.IO) {
         while (true) {
             delay(15)
         }
    }
}

approximately on 10-15th loop iteration iOS app crashes.

Kotlin: 1.4.31 coroutineworker: 0.6.2

Actual implementation of Dispatchers.IO:

actual val Dispatchers.IO: CoroutineDispatcher
    get() = IODispatcher
benasher44 commented 3 years ago

Hi thanks for the report! What version of kotlinx.coroutines are you using?

belousovgm commented 3 years ago

1.4.2-native-mt

benasher44 commented 3 years ago

Ah okay thanks for providing that. CoroutineWorker is meant to be used instead of the native-mt branch. It provides multi-threaded coroutine capabilities in an environment without native-mt. One day when native-mt is merged to main/master coroutines, this library will be no longer needed for example.

For your use case and with native-mt generally, you should use kotlinx.coroutines-provide primitives/methods—don't use CoroutineWorker, as the two are not designed to work together. In this particular case, you probably want to make your own global single-threaded dispatcher to use as an IO dispatcher and use the kotlinx.coroutines withContext method with that dispatcher here.

Going to close, as it's expected that native-mt is not designed to work with CoroutineWorker—in fact it will obsolete it one day. Thanks again for flagging!