# Why does sending a sendable value risk causing data races?

**URL:** <https://forums.swift.org/t/why-does-sending-a-sendable-value-risk-causing-data-races/73074>\
**Category:** Using Swift\
**Tags:** concurrency\
**Created:** [July 9, 2024, 8:20pm UTC](https://forums.swift.org/t/why-does-sending-a-sendable-value-risk-causing-data-races/73074 "2024-07-09T20:20:06Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![nh7a](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nh7a/32/1141_2.png) [@nh7a](https://forums.swift.org/u/nh7a)\
**Post date:** [July 9, 2024, 8:20pm UTC](https://forums.swift.org/t/why-does-sending-a-sendable-value-risk-causing-data-races/73074/1 "2024-07-09T20:20:06Z")

</div>

With Xcode 16 beta 3 (Swift 6 mode), the code below _no longer_ builds.

```swift
@MainActor struct Foo {
  var num: Int { 42 }
  nonisolated func foo(_ int: Int) async {}
  func bar() async {
    await foo(num) // 🛑 Sending 'self.num' risks causing data races
  }
}

```

> Sending main actor-isolated 'self.num' to nonisolated instance method 'foo' risks causing data races between nonisolated and main actor-isolated uses

Since `Int` is `Sendable`, I don't see how this makes sense, but does it?

---

<div class="post-metadata">

**Author:** ![Michael\_Gottesman](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/michael_gottesman/32/280_2.png) [@Michael\_Gottesman](https://forums.swift.org/u/Michael_Gottesman)\
**Post date:** [July 9, 2024, 10:52pm UTC](https://forums.swift.org/t/why-does-sending-a-sendable-value-risk-causing-data-races/73074/2 "2024-07-09T22:52:13Z")

</div>

This is a known issue. It was fixed in [f0fff2e5a02d8f56fc272ed0562a888d10f17f9b](https://github.com/swiftlang/swift/commit/f0fff2e5a02d8f56fc272ed0562a888d10f17f9b)

---

<div class="post-metadata">

**Author:** ![nh7a](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nh7a/32/1141_2.png) [@nh7a](https://forums.swift.org/u/nh7a)\
**Post date:** [July 10, 2024, 2:15am UTC](https://forums.swift.org/t/why-does-sending-a-sendable-value-risk-causing-data-races/73074/3 "2024-07-10T02:15:08Z")

</div>

Cool. Meanwhile, is any workaround available, or do we need to wait until Xcode 16 beta 4?

---

<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:** [July 14, 2024, 6:04pm UTC](https://forums.swift.org/t/why-does-sending-a-sendable-value-risk-causing-data-races/73074/4 "2024-07-14T18:04:59Z")

</div>

> [@nh7a](#):
>
> Cool. Meanwhile, is any workaround available, or do we need to wait until Xcode 16 beta 4?

Your can store the variable locally before using it as a parameter:

```swift
@MainActor struct Foo {
    var num: Int { 42 }
    nonisolated func foo(_ int: Int) async {}
    func bar() async {
        var num = num // sometimes a let works too, but not always
        await foo(num)
    }
}

```

---

<div class="post-metadata">

**Author:** ![willtemperley](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/willtemperley/32/14886_2.png) [@willtemperley](https://forums.swift.org/u/willtemperley)\
**Post date:** [July 16, 2024, 7:41am UTC](https://forums.swift.org/t/why-does-sending-a-sendable-value-risk-causing-data-races/73074/5 "2024-07-16T07:41:12Z")

</div>

> [@nh7a](#):
>
> Cool. Meanwhile, is any workaround available, or do we need to wait until Xcode 16 beta 4?

I'm going back to Xcode 16 Beta 2 because of this.

---

<div class="post-metadata">

**Author:** ![wangzhizhou](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/wangzhizhou/32/23325_2.png) [@wangzhizhou](https://forums.swift.org/u/wangzhizhou)\
**Post date:** [July 19, 2024, 10:50am UTC](https://forums.swift.org/t/why-does-sending-a-sendable-value-risk-causing-data-races/73074/6 "2024-07-19T10:50:10Z")

</div>

Great, this workaround is useful for me

---

<div class="post-metadata">

**Author:** ![willtemperley](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/willtemperley/32/14886_2.png) [@willtemperley](https://forums.swift.org/u/willtemperley)\
**Post date:** [July 25, 2024, 5:49am UTC](https://forums.swift.org/t/why-does-sending-a-sendable-value-risk-causing-data-races/73074/7 "2024-07-25T05:49:34Z")

</div>

Seems to be the same in Beta 4 unfortunately

---

<div class="post-metadata">

**Author:** ![paulhdk](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/paulhdk/32/37481_2.png) [@paulhdk](https://forums.swift.org/u/paulhdk)\
**Post date:** [September 26, 2024, 9:10pm UTC](https://forums.swift.org/t/why-does-sending-a-sendable-value-risk-causing-data-races/73074/8 "2024-09-26T21:10:57Z")

</div>

I also ran into this issue, but am unable to build my project with the Swift 6.0.1 toolchain, as Xcode reports an SDK/toolchain mismatch when trying to import SwiftUI.

See my corresponding post here: [Unable to Use SwiftUI With Latest Swift 6.0.1. Compiler Due to Toolchain / SDK Mismatch](https://forums.swift.org/t/unable-to-use-swiftui-with-latest-swift-6-0-1-compiler-due-to-toolchain-sdk-mismatch/74972)

---

<div class="post-metadata">

**Author:** ![sasouth](https://avatars.discourse-cdn.com/v4/letter/s/b3f665/32.png) [@sasouth](https://forums.swift.org/u/sasouth)\
**Post date:** [October 9, 2024, 4:06pm UTC](https://forums.swift.org/t/why-does-sending-a-sendable-value-risk-causing-data-races/73074/9 "2024-10-09T16:06:02Z")

</div>

What is the status here ? I still see this error in Xcode 16.1 beta 3 with a string type being passed from a nonisolated function into a MainActor bound task.

---

<div class="post-metadata">

**Author:** ![Michael\_Gottesman](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/michael_gottesman/32/280_2.png) [@Michael\_Gottesman](https://forums.swift.org/u/Michael_Gottesman)\
**Post date:** [October 9, 2024, 7:14pm UTC](https://forums.swift.org/t/why-does-sending-a-sendable-value-risk-causing-data-races/73074/10 "2024-10-09T19:14:52Z")

</div>

@sasouth can you post a specific test case that shows the issue so I can try to reproduce. There may be something else going on.

In terms of the original issue, I just validated that we do not get any errors with the original test case of the OP in both Xcode 16 and Xcode 16.1 Beta 3.
