---
title: "Discussion on \"Coroutines racing! Why, and how.\" | Hashnode"
description: "Discussion on \"Coroutines racing! Why, and how.\". In this article, you'll see how to race coroutines in Kotlin: The fastest suspending function will get its value returned, others will be cancelled."
url: "https://hashnode.com/posts/coroutines-racing-why-and-how/618a2953f991b025366df06f"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Coroutines racing! Why, and how.](https://blog.louiscad.com/coroutines-racing-why-and-how?utm_source=hashnode&utm_medium=feed)

In this article, you'll see how to race coroutines in Kotlin: The fastest suspending function will get its value returned, others will be cancelled.

Nov 8, 2021 · 7 min read · blog.louiscad.com  
Updated Nov 18, 2021  
Read the article

-   [#kotlin](https://hashnode.com/tag/kotlin)
-   [#coroutines](https://hashnode.com/tag/coroutines)
-   [#concurrency](https://hashnode.com/tag/concurrency)

## Written by

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

@LouisCAD · Tours, France

  
[View profile](https://hashnode.com/@LouisCAD)

Kotlin GDE

I contribute to the Kotlin ecosystem through open source projects, issue reporting, talks at conferences, and more. I've been developing for Android since 2014, using Kotlin since 2016, with coroutines since 2017, and in 2018, I started doing multiplatform development in libraries, then in production apps.

## Responses 3

Markdown supported · ⌘↵ to post

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

[Nov 19, 2021](https://hashnode.com/posts/coroutines-racing-why-and-how/618a2953f991b025366df06f/comment/619798f3df376a260c5876dd)

[Louis CAD](https://hashnode.com/@LouisCAD) A (fixed) basic approach ;-)

```
suspend fun awaitTrigger1() { coroutineScope {
    val win = coroutineContext::cancelChildren
    launch { awaitAutomaticTrigger(); win(null) }
    launch { awaitManualTrigger(); win(null) }
} }
```

version with some result

```
suspend fun awaitTrigger2(): T = coroutineScope {
    val deferred = CompletableDeferred<T>()
    launch { deferred.complete(awaitAutomaticTrigger()) }
    launch { deferred.complete(awaitManualTrigger()) }
    deferred.await().also { coroutineContext.cancelChildren() }
}
```

[pl.kotl.in/i1YWEs2lC](https://pl.kotl.in/i1YWEs2lC) [pl.kotl.in/oQyBHb2bg](https://pl.kotl.in/oQyBHb2bg)

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

[Nov 27, 2021](https://hashnode.com/posts/coroutines-racing-why-and-how/618a2953f991b025366df06f/comment/619798f3df376a260c5876dd#reply-61a23c16a707ae38156d6111)

That one is a good alternative. It's not immune to late racers (so, the use case of `race` rather than `raceOf`), but I believe it's possible to make some edits that make it supported and thread-safe for this use case.

Now I'm wondering what would perform best between your snippet and using select.

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

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

Same example with some result (for single thread dispatcher):

```
suspend fun awaitTrigger(): T {
    var t: T? = null
    coroutineScope {
        val scope = this
        launch { t = awaitAutomaticTrigger(); scope.cancel() }
        launch { t = awaitManualTrigger(); scope.cancel() }
    }
    return t!!
}
```

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

[Nov 15, 2021](https://hashnode.com/posts/coroutines-racing-why-and-how/618a2953f991b025366df06f/comment/61925833fd3a1a5a01a8897b#reply-6192611efd3a1a5a01a88a34)

Can't work for the same reason as the other one you shared. For reference, 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)

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.

[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.
