question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Parent coroutine is frozen when child coroutine on background thread is launched

See original GitHub issue

coroutines_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:closed
  • Created 4 years ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
elizarovcommented, Jan 16, 2020

@brendanw This test hangs because you cannot do withContext(Dispatchers.Main) from here. However, if you change to withContext(Dispatchers.Default) then it works fine for me (channel is not frozen).

0reactions
brendanwcommented, Mar 14, 2020

Ah, duh. Yup that makes sense.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found