# Data races with async/await and Strict Concurrency Checking = Complete

**URL:** <https://forums.swift.org/t/data-races-with-async-await-and-strict-concurrency-checking-complete/65404>\
**Category:** Using Swift\
**Tags:** concurrency\
**Created:** [June 7, 2023, 4:22pm UTC](https://forums.swift.org/t/data-races-with-async-await-and-strict-concurrency-checking-complete/65404 "2023-06-07T16:22:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lassejansen](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lassejansen/32/4712_2.png) [@lassejansen](https://forums.swift.org/u/lassejansen)\
**Post date:** [June 7, 2023, 4:22pm UTC](https://forums.swift.org/t/data-races-with-async-await-and-strict-concurrency-checking-complete/65404/1 "2023-06-07T16:22:14Z")

</div>

The crash below is 100% reproducible for me. My assumption was that with `Strict Concurrency Checking` set to `Complete` this shouldn't compile because `Repository` isn't `Sendable`. Is this a known bug?

 ![BAD_ACCESS](https://global.discourse-cdn.com/swift/original/3X/9/0/90947dad3557ad1723932544b4d84f0d7f7db13d.jpeg)

> **Code**
>
> ```swift
> import SwiftUI
> 
> @main
> struct AsyncAwaitWithoutActorApp: App {
> @ObservedObject
> var viewModel = ViewModel()
> 
> var body: some Scene {
> WindowGroup {
> Button("Start") {
> Task {
> await viewModel.run()
> }
> }
> }
> }
> }
> 
> class Repository {
> var results: [Int] = []
> 
> func add(value: Int) {
> results.append(value)
> }
> }
> 
> @MainActor
> class ViewModel: ObservableObject {
> var repository = Repository()
> 
> func run() async {
> for value in 0..<1000 {
> Task {
> await Operation(repository: self.repository).execute(value: value)
> }
> }
> }
> }
> 
> struct Operation {
> let repository: Repository
> 
> func execute(value: Int) async {
> repository.add(value: value)
> }
> }
> 
> ```

---

<div class="post-metadata">

**Author:** ![lassejansen](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lassejansen/32/4712_2.png) [@lassejansen](https://forums.swift.org/u/lassejansen)\
**Post date:** [June 7, 2023, 6:11pm UTC](https://forums.swift.org/t/data-races-with-async-await-and-strict-concurrency-checking-complete/65404/2 "2023-06-07T18:11:37Z")

</div>

Thinking further about this, is it even possible to prevent data races like this at compile time if async member functions on non-sendable, non-actor-bound types are allowed in the language?

---

<div class="post-metadata">

**Author:** ![FranzBusch](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/franzbusch/32/19447_2.png) [@FranzBusch](https://forums.swift.org/u/FranzBusch)\
**Post date:** [June 7, 2023, 7:46pm UTC](https://forums.swift.org/t/data-races-with-async-await-and-strict-concurrency-checking-complete/65404/3 "2023-06-07T19:46:20Z")

</div>

This is a known hole in Sendable checking right now. [Sendable checking hole when actors call non-mutating async methods on non-Sendable types · Issue #65315 · apple/swift · GitHub](https://github.com/apple/swift/issues/65315)

The problem is that any non actor isolated async method is running on the global concurrency pool. This means that any `async` method requires all its captures to be `Sendable`. The compiler is currently not diagnosing this.

You can make this code safe by making `Repository` an actor so that its mutable state is protected.

---

<div class="post-metadata">

**Author:** ![lassejansen](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lassejansen/32/4712_2.png) [@lassejansen](https://forums.swift.org/u/lassejansen)\
**Post date:** [June 7, 2023, 10:19pm UTC](https://forums.swift.org/t/data-races-with-async-await-and-strict-concurrency-checking-complete/65404/4 "2023-06-07T22:19:28Z")

</div>

Interesting, thanks.

> [@FranzBusch](#):
>
> You can make this code safe by making `Repository` an actor so that its mutable state is protected.

Yes I know, I was just wondering why the compiler wouldn't complain about it.

As a rule of thumb, to prevent data races, it looks like that all types that have async member functions should either be `Sendable`, or be bound to an actor, or be an actor themselves, right?

---

<div class="post-metadata">

**Author:** ![FranzBusch](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/franzbusch/32/19447_2.png) [@FranzBusch](https://forums.swift.org/u/FranzBusch)\
**Post date:** [June 7, 2023, 10:40pm UTC](https://forums.swift.org/t/data-races-with-async-await-and-strict-concurrency-checking-complete/65404/5 "2023-06-07T22:40:45Z")

</div>

> [@lassejansen](#):
>
> As a rule of thumb, to prevent data races, it looks like that all types that have async member functions should either be `Sendable`, or be bound to an actor, or be an actor themselves, right?

Correct.
