# Coroutines and deadlocks

**URL:** <https://discuss.kotlinlang.org/t/coroutines-and-deadlocks/18060>\
**Category:** Support\
**Created:** [June 12, 2020, 10:44am UTC](https://discuss.kotlinlang.org/t/coroutines-and-deadlocks/18060 "2020-06-12T10:44:00Z")\
**Posts on this page:** 4\
**Page:** 2

<div class="post-metadata">

**Author:** ![mtimmerm](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/mtimmerm/32/7160_2.png) [@mtimmerm](https://discuss.kotlinlang.org/u/mtimmerm)\
**Post date:** [June 13, 2020, 1:20pm UTC](https://discuss.kotlinlang.org/t/coroutines-and-deadlocks/18060/21 "2020-06-13T13:20:56Z")

</div>

You would need to make blocking versions of your external API that enters a `runBlocking` context to call the suspending API. Just don’t call them from a coroutine.

Similarly, you can have `asSequence` the uses `runBlocking` as long as you don’t call it from a coroutine. If you need a version to call from a coroutine, it should make a `Flow`.

I know this seems annoying, but that’s because you haven’t experienced the pain of doing it in Java without using coroutines at all.

---

<div class="post-metadata">

**Author:** ![Loke](https://avatars.discourse-cdn.com/v4/letter/l/c68b51/32.png) [@Loke](https://discuss.kotlinlang.org/u/Loke)\
**Post date:** [June 13, 2020, 3:16pm UTC](https://discuss.kotlinlang.org/t/coroutines-and-deadlocks/18060/22 "2020-06-13T15:16:16Z")

</div>

Thanks, I will give it another try to see how far I get before I run into another issue.

One conclusion I’m drawing from this disussion is that `runBlocking` is only a safe function to call if you are in control of the entire application. For example, it would appear that calling it from within a library is unsafe, since you are not in control of the caller, and if your function ends up being called from another `runBlocking`, you can end up with a deadlock.

Is this is a reasonable interpretation of the situation? And if so, what is a safe way of calling suspending functions from a library that exposes non-suspending API calls (for example something that needs a Java API).

---

<div class="post-metadata">

**Author:** ![nickallendev](https://avatars.discourse-cdn.com/v4/letter/n/cdc98d/32.png) [@nickallendev](https://discuss.kotlinlang.org/u/nickallendev)\
**Post date:** [June 13, 2020, 5:13pm UTC](https://discuss.kotlinlang.org/t/coroutines-and-deadlocks/18060/23 "2020-06-13T17:13:06Z")

</div>

Implementing synchronous library APIs with asynchronous code is dangerous in general. This is not unique to Kotlin or coroutines. Last time I hit this type of problem was C++ with regular callbacks and we ended up ditching the async internals.

If implementing a synchronous API with internals, the library has to ensure that a blocking call will never be made from an event loop used as part of its implementation, even transitively.

For example, the synchronous library API could restrict itself to only use the dispatcher created by `runBlocking` (it loops synchronously inside `runBlocking`). A private thread pool could be used too but you’d need to ensure that these blocking library APIs were never called from the thread pool even internally.

It’s easy to mess up which is why it’s not recommended. If you are using coroutines you should really expose suspend methods.

I definitely recommend switching to `Flow`.

---

<div class="post-metadata">

**Author:** ![nazzinoz](https://avatars.discourse-cdn.com/v4/letter/n/bcef8e/32.png) [@nazzinoz](https://discuss.kotlinlang.org/u/nazzinoz)\
**Post date:** [October 15, 2025, 4:35am UTC](https://discuss.kotlinlang.org/t/coroutines-and-deadlocks/18060/24 "2025-10-15T04:35:34Z")

</div>

Sorry for being reply this late, but I think we can create a CoroutineScope to do that easily, right?

[Previous page](https://discuss.kotlinlang.org/t/coroutines-and-deadlocks/18060.md?page=1)
