Parent coroutine is frozen when child coroutine on background thread is launched
See original GitHub issuecoroutines_version=1.3.3-native-mt kotlin_version=1.3.61
GlobalScope.launch(Dispatchers.Main) {
this.ensureNeverFrozen()
val result = someBackgroundSuspend()
// `this` is now frozen. any other object referenced in this lambda is now frozen as well
}
suspend fun someBackgroundSuspend() = withContext(Dispatchers.Default) {
true
}
expected result: parent coroutine should not be frozen on native.
actual result: parent coroutine is frozen on native; makes it impossible to dispatch result
to any listeners without using atomics everywhere to prevent mutability exceptions.
When I am running something akin to this in my project I am actually seeing someBackgroundSuspend() never complete when I have the ensureNeverFrozen call beforehand.
GlobalScope.launch(Dispatchers.Main) {
this.ensureNeverFrozen()
val scope = CoroutineScope(Dispatchers.Default)
val job = scope.launch {
}
// `this` is not frozen
}
Issue Analytics
- State:
- Created 4 years ago
- Comments:11 (5 by maintainers)
Top Results From Across the Web
Launching Kotlin coroutine inside suspend fun inheriting ...
I want to put the endless loop implicitly on the thread that caller of the initService method runs in. In the real app...
Read more >Coroutines will FREEZE your UI - YouTube
The best Android courses in the world: https://codingwithmitch.com/In this video I talk about how coroutines fundamentally work.
Read more >Why exception handling with Kotlin Coroutines is so hard and ...
Exception handling is probably one of the hardest parts when learning Coroutines. In this blog post, I am going to describe the reasons...
Read more >Spin up child coroutine without blocking parent completion
I have a core class whose methods can be called from many threads. Currently, I mark the methods as synchronized to make sure...
Read more >Guide to UI programming with coroutines
Parent -child relation between jobs forms a hierarchy. A coroutine that performs some background job on behalf of the activity can create further...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@brendanw This test hangs because you cannot do
withContext(Dispatchers.Main)
from here. However, if you change towithContext(Dispatchers.Default)
then it works fine for me (channel
is not frozen).Ah, duh. Yup that makes sense.