# coroutineScope() thread safety?

**URL:** <https://discuss.kotlinlang.org/t/coroutinescope-thread-safety/21892>\
**Category:** Support\
**Created:** [May 20, 2021, 5:42pm UTC](https://discuss.kotlinlang.org/t/coroutinescope-thread-safety/21892 "2021-05-20T17:42:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nottheoilrig](https://avatars.discourse-cdn.com/v4/letter/n/f9ae1b/32.png) [@nottheoilrig](https://discuss.kotlinlang.org/u/nottheoilrig)\
**Post date:** [May 20, 2021, 5:42pm UTC](https://discuss.kotlinlang.org/t/coroutinescope-thread-safety/21892/1 "2021-05-20T17:42:28Z")

</div>

I haven’t been able to reproduce this yet in an isolated example, however I’m seeing intermittent errors in my app that could be explained if, in the following example, “JOB 2” were printed after “EXIT SCOPE”:

```kotlin
suspend fun main() {
    withContext(Dispatchers.Default) {
        launch job1@{
            this@withContext.launch { println("JOB 2") }
        }
    }
    println("EXIT SCOPE")
}

```

I expect `withContext()` to postpone “EXIT SCOPE” until at least job 1 is complete, by which time I expect job 2 will have been added to the scope, further postponing “EXIT SCOPE” until “JOB 2” is complete. Is this guaranteed, however?

In the right multithreaded environment, is it possible for `withContext()` to end before job 2 is complete?

If this isn’t guaranteed I can instead arrange to call `this@job1.launch { println("JOB 2") }`, however in my app (where scope is an instance variable) it’s currently more natural as is.

---

<div class="post-metadata">

**Author:** ![chipays](https://avatars.discourse-cdn.com/v4/letter/c/ad7895/32.png) [@chipays](https://discuss.kotlinlang.org/u/chipays)\
**Post date:** [May 20, 2021, 6:58pm UTC](https://discuss.kotlinlang.org/t/coroutinescope-thread-safety/21892/2 "2021-05-20T18:58:59Z")

</div>

`withContext` returns as soon as the lambda block completes. If you want to await all launched jobs in a certain scope, consider using [coroutineScope](https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/coroutine-scope.html) instead.

---

<div class="post-metadata">

**Author:** ![nottheoilrig](https://avatars.discourse-cdn.com/v4/letter/n/f9ae1b/32.png) [@nottheoilrig](https://discuss.kotlinlang.org/u/nottheoilrig)\
**Post date:** [May 20, 2021, 8:53pm UTC](https://discuss.kotlinlang.org/t/coroutinescope-thread-safety/21892/3 "2021-05-20T20:53:40Z")

</div>

> [@chipays](#):
>
> `withContext` returns as soon as the lambda block completes.

If true wouldn’t the following print

1. Leave withContext()
2. Delayed

instead of the reverse?

```kotlin
import kotlin.coroutines.EmptyCoroutineContext
suspend fun main() {
    withContext(EmptyCoroutineContext) {
        launch {
            delay(1000L)
            println("Delayed")
        }
    }
    println("Leave withContext()")
}
```

---

<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:** [May 21, 2021, 1:21am UTC](https://discuss.kotlinlang.org/t/coroutinescope-thread-safety/21892/4 "2021-05-21T01:21:35Z")

</div>

`withContext` and `coroutineScope` don’t resume until all child jobs have finished.

job 2 can finish before or after job 1 finishes, but it will always finish before `withContext` resumes.

I’ve had problems dealing with CoroutineScope instance variables where I use the wrong scope accidentally:

```auto
val myJob = scope.launch {
    someFlow.launchIn(scope) { } // Oops, i mean't "this", not "scope", myJob will finish immediately
}
myJob.invokeOnCompletion { expectSomeFlowCollectionFinished() }

```

Perhaps you also are running into a similar situation using an unintended scope?
