---
title: "Comment by Marek Langiewicz on \"Coroutines racing! Why, and how.\" | Hashnode"
description: "Why not just cancel the scope when a child wins? A basic approach: suspend fun awaitTrigger () { coroutineScope { val scope = this launch { awaitAutomaticTrigge"
url: "https://hashnode.com/posts/coroutines-racing-why-and-how/618a2953f991b025366df06f/comment/61922d46fd4e554b5f3e8111"
---

[Discussion](https://hashnode.com/posts/coroutines-racing-why-and-how/618a2953f991b025366df06f)

Comment [Coroutines racing! Why, and how.](https://hashnode.com/posts/coroutines-racing-why-and-how/618a2953f991b025366df06f)

[Marek Langiewicz](https://hashnode.com/@Langara)

[Nov 15, 2021](https://hashnode.com/posts/coroutines-racing-why-and-how/618a2953f991b025366df06f/comment/61922d46fd4e554b5f3e8111)

Why not just cancel the scope when a child wins? A basic approach:

```
suspend fun awaitTrigger() { coroutineScope {
    val scope = this
    launch { awaitAutomaticTrigger(); scope.cancel() }
    launch { awaitManualTrigger(); scope.cancel() }
} }
```

It should gracefully cancel all losers. Also more flexible: we can for example always wait for some specific slow child by skipping cancel invocation.

[Louis CAD](https://hashnode.com/@LouisCAD)

[Nov 15, 2021](https://hashnode.com/posts/coroutines-racing-why-and-how/618a2953f991b025366df06f/comment/61922d46fd4e554b5f3e8111#reply-61923ed80ba49f4c919485af)

This cannot work, because it also cancels the coroutine that is about to win.

[Marek Langiewicz](https://hashnode.com/@Langara)

[Nov 15, 2021](https://hashnode.com/posts/coroutines-racing-why-and-how/618a2953f991b025366df06f/comment/61922d46fd4e554b5f3e8111#reply-61925692fd3a1a5a01a8894a)

[Louis CAD](https://hashnode.com/@LouisCAD) Yes, but that's fine because the winner already finished it's work (even though winner's Job is still active).

[Louis CAD](https://hashnode.com/@LouisCAD)

[Nov 15, 2021](https://hashnode.com/posts/coroutines-racing-why-and-how/618a2953f991b025366df06f/comment/61922d46fd4e554b5f3e8111#reply-6192605ffd4e554b5f3e84e4)

[Marek Langiewicz](https://hashnode.com/@Langara) Nope, the winner's job is not finished until after the `launch` block is complete, so cancellation happens before, so it cannot work.

Here's a playground that proves it: [pl.kotl.in/MArMNltGT](https://pl.kotl.in/MArMNltGT)

[Marek Langiewicz](https://hashnode.com/@Langara)

[Nov 15, 2021](https://hashnode.com/posts/coroutines-racing-why-and-how/618a2953f991b025366df06f/comment/61922d46fd4e554b5f3e8111#reply-61929bc9fd4e554b5f3e89b6)

[Louis CAD](https://hashnode.com/@LouisCAD) You're right. It crashes. Looks like the `coroutineScope {...}` doesn't swollow cancellation (in this particular edge case) even when explicitly asked to cancel. I find it surprising because cancellation should be cooperative.. Even stranger thing is that when I change `coroutineScope { ... }` to`launch { ... }.join()` then it starts working as I expected in the first place. Either it's a bug in coroutines or I don't quite get all guarantees in coroutines machinery.
