# Coroutine Self-Waiting

**URL:** <https://discuss.kotlinlang.org/t/coroutine-self-waiting/8947>\
**Category:** Contributions\
**Created:** [August 8, 2018, 3:46pm UTC](https://discuss.kotlinlang.org/t/coroutine-self-waiting/8947 "2018-08-08T15:46:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![stallker2003](https://avatars.discourse-cdn.com/v4/letter/s/a88e4f/32.png) [@stallker2003](https://discuss.kotlinlang.org/u/stallker2003)\
**Post date:** [August 8, 2018, 3:46pm UTC](https://discuss.kotlinlang.org/t/coroutine-self-waiting/8947/1 "2018-08-08T15:46:20Z")

</div>

Hi,recently i started working with coroutines.  
By mistake made some deadlock when coroutine waits for himself.  
It was not trivial, cause action was hidden in sub-function.

Maybe we can track this trivial case, and make some warring in this case?

```
var job : Deferred<Unit?>? = null
fun main(args:Array<String>) {

    job = async { close_all() }

    runBlocking {
        job?.await()
    }
}

fun close_all(){
    println("Closing all Operations")
    runBlocking {
        job?.await()
    }
}

```

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [August 8, 2018, 5:09pm UTC](https://discuss.kotlinlang.org/t/coroutine-self-waiting/8947/2 "2018-08-08T17:09:22Z")

</div>

> [@stallker2003](#):
>
> Maybe we can track this trivial case, and make some warring in this case?

TL;DR. Not really possible.

What you are talking about takes some seriously complex control flow analysis and that is just the case when your `job` variable is a val. The compiler would need to check all possible paths through your program. Also you are talking about coroutines so there is parallel execution and stuff. The point is, even if it is possible in some rare cases to detect such an error statically it would be very complex to implement and be very error prone. It would never find all cases so the feature would not be reliable and it would take a long time.

---

<div class="post-metadata">

**Author:** ![stallker2003](https://avatars.discourse-cdn.com/v4/letter/s/a88e4f/32.png) [@stallker2003](https://discuss.kotlinlang.org/u/stallker2003)\
**Post date:** [August 9, 2018, 5:50am UTC](https://discuss.kotlinlang.org/t/coroutine-self-waiting/8947/3 "2018-08-09T05:50:17Z")

</div>

Well, i suppose you cannot do this by IDE. But you can still detect it during execution. As i understand you can just check if inside the job you are waiting for yourself, then just give a warring message (Of course there should be way to cancel this message in some weird cases).

---

<div class="post-metadata">

**Author:** ![fvasco](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fvasco/32/1961_2.png) [@fvasco](https://discuss.kotlinlang.org/u/fvasco)\
**Post date:** [August 9, 2018, 7:08am UTC](https://discuss.kotlinlang.org/t/coroutine-self-waiting/8947/4 "2018-08-09T07:08:19Z")

</div>

I consider your suggestion as a very hard task, however you can write an extension method `awaitForAnotherJob` to achieve this goal.

Finally you have not to use `async` in this way to invoke the blocking funcion `close_all()`, `async` should contain a suspending lambda [coroutines-examples/kotlin-coroutines-informal.md at master · Kotlin/coroutines-examples · GitHub](https://github.com/Kotlin/kotlin-coroutines/blob/master/kotlin-coroutines-informal.md#terminology)
