# Does await\* create race conditions?

**URL:** <https://forum.dfinity.org/t/does-await-create-race-conditions/20433>\
**Category:** Motoko\
**Created:** [June 4, 2023, 7:02am UTC](https://forum.dfinity.org/t/does-await-create-race-conditions/20433 "2023-06-04T07:02:30Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![qwertytrewq](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/qwertytrewq/32/10423_2.png) [@qwertytrewq](https://forum.dfinity.org/u/qwertytrewq)\
**Post date:** [June 4, 2023, 7:02am UTC](https://forum.dfinity.org/t/does-await-create-race-conditions/20433/1 "2023-06-04T07:02:30Z")

</div>

As I know, in

```auto
f();
await g();
h();

```

there may be intermediary code execution from other calls between `f()` and `g()`.

Is it true also for

```auto
f();
await* g();
h();

```

or is in this case the entire sequence of three statements atomic?

---

<div class="post-metadata">

**Author:** ![rossberg](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/rossberg/32/795_2.png) [@rossberg](https://forum.dfinity.org/u/rossberg)\
**Post date:** [June 4, 2023, 8:33am UTC](https://forum.dfinity.org/t/does-await-create-race-conditions/20433/2 "2023-06-04T08:33:34Z")

</div>

No.

In the first version, there _definitely is_ a commit point and interleaving.

In the second version, there _may_ be a commit point and interleaving.

So the second case _may_ execute everything atomically, but you cannot predict that. If you could rely on that being the case, then you probably wouldn’t need any await in the first place.

Furthermore, if there is an error (trap) during execution of h(), then in the first case, execution is rolled back to the await. In the second case execution is rolled back to either (somewhere inside) the await\* _or beyond_, you can’t predict it – even f() may or may not be rolled back.

---

<div class="post-metadata">

**Author:** ![timo](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/timo/32/5543_2.png) [@timo](https://forum.dfinity.org/u/timo)\
**Post date:** [June 4, 2023, 11:22am UTC](https://forum.dfinity.org/t/does-await-create-race-conditions/20433/3 "2023-06-04T11:22:09Z")

</div>

> [@qwertytrewq](#):
>
> between `f()` and `g()`.

Do you mean between g and h?

---

<div class="post-metadata">

**Author:** ![qwertytrewq](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/qwertytrewq/32/10423_2.png) [@qwertytrewq](https://forum.dfinity.org/u/qwertytrewq)\
**Post date:** [June 4, 2023, 11:49am UTC](https://forum.dfinity.org/t/does-await-create-race-conditions/20433/4 "2023-06-04T11:49:37Z")

</div>

I meant _somewhere_ between `f` and `h`.

---

<div class="post-metadata">

**Author:** ![qwertytrewq](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/qwertytrewq/32/10423_2.png) [@qwertytrewq](https://forum.dfinity.org/u/qwertytrewq)\
**Post date:** [June 4, 2023, 11:55am UTC](https://forum.dfinity.org/t/does-await-create-race-conditions/20433/5 "2023-06-04T11:55:37Z")

</div>

@rossberg So, after

```auto
func f() { ... };
func h() { ... };
func g() async* {};

```

```auto
f();
await* g();
h();

```

may execute atomically and may not? (even though the context of `g` is predictable, empty in our case)

---

<div class="post-metadata">

**Author:** ![ZhenyaUsenko](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/zhenyausenko/32/12200_2.png) [@ZhenyaUsenko](https://forum.dfinity.org/u/ZhenyaUsenko)\
**Post date:** [June 4, 2023, 12:08pm UTC](https://forum.dfinity.org/t/does-await-create-race-conditions/20433/6 "2023-06-04T12:08:51Z")

</div>

I will be unpredictable only if you have conditional awaits inside **g**

---

<div class="post-metadata">

**Author:** ![rossberg](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/rossberg/32/795_2.png) [@rossberg](https://forum.dfinity.org/u/rossberg)\
**Post date:** [June 4, 2023, 2:14pm UTC](https://forum.dfinity.org/t/does-await-create-race-conditions/20433/7 "2023-06-04T14:14:30Z")

</div>

@qwertytrewq, yes, in that case it will be predictably atomic, but in that case the async\* of course is pointless in the first place. In a case where async\* is actually needed it will also be unpredictable, by construction.
