# Can somebody please explain GlobalScope.async to me?

**URL:** <https://discuss.kotlinlang.org/t/can-somebody-please-explain-globalscope-async-to-me/10600>\
**Category:** Uncategorized\
**Created:** [December 1, 2018, 9:49am UTC](https://discuss.kotlinlang.org/t/can-somebody-please-explain-globalscope-async-to-me/10600 "2018-12-01T09:49:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![martin.haeusler](https://avatars.discourse-cdn.com/v4/letter/m/e99b99/32.png) [@martin.haeusler](https://discuss.kotlinlang.org/u/martin.haeusler)\
**Post date:** [December 1, 2018, 9:49am UTC](https://discuss.kotlinlang.org/t/can-somebody-please-explain-globalscope-async-to-me/10600/1 "2018-12-01T09:49:03Z")

</div>

I’m currently dealing with the following method:

```auto
// overrides an interface, can't make it suspending
override fun doSomeWork(): Deferred<T> {
    return GlobalScope.async {
        // actual work here
    }
} 

```

This _seems_ to work. However, in some cases, when I call `doSomeWork().await()` in my code, the `await()` **will suspend the calling coroutine forever** (kind of like a dead-lock).

According to the [kotlin docs](https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-global-scope/):

> Application code usually should use application-defined [CoroutineScope](https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html), using [async](https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/async.html) or [launch](https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/launch.html) on the instance of [GlobalScope](https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-global-scope/index.md) is **highly discouraged**.

However, the docs do not explain **why** it is discouraged. It seems like the most meaningful way to produce a `Deferred<T>` object inside a non-suspending function. Could somebody clarify this?

I set out to look for a different solution, and what I am doing now is this:

```auto
// overrides an interface, can't make it suspending
override fun doSomeWork(): Deferred<T> {
    var result: Deferred<T>? = null
    runBlocking {
        result = async {
            // actual work here
        }
    }
    return result!!
} 

```

In contrast to the solution above, this one actually **works** (no deadlocks). If you ask me, this second (rather convoluted) solution should be _semantically_ equvialent to the first one, but apparently there _is_ a difference (which I cannot see).

Could somebody help me unravel this mystery?

---

<div class="post-metadata">

**Author:** ![darksnake](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/darksnake/32/2479_2.png) [@darksnake](https://discuss.kotlinlang.org/u/darksnake)\
**Post date:** [December 1, 2018, 1:03pm UTC](https://discuss.kotlinlang.org/t/can-somebody-please-explain-globalscope-async-to-me/10600/2 "2018-12-01T13:03:19Z")

</div>

The coroutines are not safe from deadlocks. At least if you are using some blocking code somewhere. If you exhaust thread pool somewhere, you will have the lock. Scopes so not save you from deadlock, they provide a way to terminate dead coroutines.

In your case, you probably start additional coroutines inside async somewhere or event call your `doSomeWork` from a coroutine. In the last case. Using `runBlocking` could disentangle this lock, but it is really bad solution. Better to find the reason behind the lock.

---

<div class="post-metadata">

**Author:** ![martin.haeusler](https://avatars.discourse-cdn.com/v4/letter/m/e99b99/32.png) [@martin.haeusler](https://discuss.kotlinlang.org/u/martin.haeusler)\
**Post date:** [December 1, 2018, 1:23pm UTC](https://discuss.kotlinlang.org/t/can-somebody-please-explain-globalscope-async-to-me/10600/3 "2018-12-01T13:23:08Z")

</div>

Thanks but… that doesn’t really answer any of the questions above…

How would you debug this situation? I know how to do it with threads (basically fire up the profiler and do a thread dump, look at the stack trace), but with coroutines that method doesn’t help. I know for a fact that if I implemented the same functionality with threads, there would not be any chance of deadlocking in the code I’m working on (do a bunch of HTTP requests in parallel, collect the results, assemble a DTO, write it into a cache, return), it must have something to do with coroutines in particular.

---

<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:** [December 1, 2018, 2:34pm UTC](https://discuss.kotlinlang.org/t/can-somebody-please-explain-globalscope-async-to-me/10600/4 "2018-12-01T14:34:07Z")

</div>

I suggest you to take a look here

> <https://github.com/Kotlin/kotlinx.coroutines/blob/master/docs/coroutines-guide.md>

it contains some coroutines basics.

---

<div class="post-metadata">

**Author:** ![martin.haeusler](https://avatars.discourse-cdn.com/v4/letter/m/e99b99/32.png) [@martin.haeusler](https://discuss.kotlinlang.org/u/martin.haeusler)\
**Post date:** [December 2, 2018, 1:10am UTC](https://discuss.kotlinlang.org/t/can-somebody-please-explain-globalscope-async-to-me/10600/5 "2018-12-02T01:10:53Z")

</div>

Hi folks,

so after digging very deep into the topic and learning a lot along the way, I realized that the coroutines are not to blame. I’m using KTOR, and need to call a REST microservice. For that, I used a `HttpClient(Apache)`. It turns out that this very HTTP client library is [known to cause indefinite waiting times](https://stackoverflow.com/questions/9925113/httpclient-stuck-without-any-exception). Well, this is precisely what hit me. As soon as I replaced it with `HttpClient(CIO)`, everything went back to normal, regardless if I used `GlobalScope.async` or some `otherScope.async`.  
I just leave this here in case that anybody else bumps into this. Be aware of the Apache HTTP client.
