# Sending NonSendable value error disappear when wrapping the expression in a local closure

**URL:** <https://forums.swift.org/t/sending-nonsendable-value-error-disappear-when-wrapping-the-expression-in-a-local-closure/82054>\
**Category:** Using Swift\
**Tags:** actors, strict-concurrency\
**Created:** [September 10, 2025, 11:19am UTC](https://forums.swift.org/t/sending-nonsendable-value-error-disappear-when-wrapping-the-expression-in-a-local-closure/82054 "2025-09-10T11:19:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Serika\_PHB](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/serika_phb/32/37091_2.png) [@Serika\_PHB](https://forums.swift.org/u/Serika_PHB)\
**Post date:** [September 10, 2025, 11:19am UTC](https://forums.swift.org/t/sending-nonsendable-value-error-disappear-when-wrapping-the-expression-in-a-local-closure/82054/1 "2025-09-10T11:19:02Z")

</div>

In the following code, method `testA` in `MyActor` takes in an NonSendable value that it does not own. Here calling function `testB` with this NonSendable parameter will get an error since we are trying to send this NonSendable value crossing actor boundaries.

```swift
class NonSendable {
    var value: Int = 0
    func inc() {
        value += 1
    }
}

@concurrent
func testB(_ value: NonSendable) async {
    value.inc()
}

actor MyActor {
    let value = NonSendable()
    func test() async {
        await testA(a: value)
    }
    func testA(a: NonSendable) async {
        await testB(a) // Sending 'a' risks causing data races
    }
}

```

However, if I wrap the expression `await testB(a)` in a local closure, the error disappears

```swift
actor MyActor {
    let value = NonSendable()
    func test() async {
        await testA(a: value)
    }
    func testA(a: NonSendable) async {
        // no error
        let c = {
            await testB(a)
        }
        await c()
    }
}

```

I’m not very sure yet but it does not look like a desired behaviour. Actually, if we intensionally change the `inc` function in `NonSendable` to something like this:

```swift
func inc() {
    let newValue = value + 1;
    print("something")
    value = newValue
}

```

Then run the `test` method of an instance of `MyActor` with lots of tasks concurrently:

```swift
let a = MyActor()

await withTaskGroup { group in 
    for _ in 0 ..< 100 {
        group.addTask {
            await a.test()
        }
    }
}

```

We are getting smaller than 100 in the end, which seems like a data race problem.

---

<div class="post-metadata">

**Author:** ![jamieQ](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jamieq/32/5117_2.png) [@jamieQ](https://forums.swift.org/u/jamieQ)\
**Post date:** [September 12, 2025, 12:47am UTC](https://forums.swift.org/t/sending-nonsendable-value-error-disappear-when-wrapping-the-expression-in-a-local-closure/82054/2 "2025-09-12T00:47:36Z")

</div>

this is definitely a bug – if you run the example with TSAN it detects the runtime data race. i took the liberty of reporting it [here](https://github.com/swiftlang/swift/issues/84249). FYI @hborla @Michael_Gottesman i believe this is a gap in RBI.

---

<div class="post-metadata">

**Author:** ![Serika\_PHB](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/serika_phb/32/37091_2.png) [@Serika\_PHB](https://forums.swift.org/u/Serika_PHB)\
**Post date:** [September 12, 2025, 2:16am UTC](https://forums.swift.org/t/sending-nonsendable-value-error-disappear-when-wrapping-the-expression-in-a-local-closure/82054/3 "2025-09-12T02:16:17Z")

</div>

Thanks! I didn’t know the thread sanitizer in swift before. Looks like a useful tool.

---

<div class="post-metadata">

**Author:** ![rayx](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/rayx/32/12204_2.png) [@rayx](https://forums.swift.org/u/rayx)\
**Post date:** [September 12, 2025, 2:24am UTC](https://forums.swift.org/t/sending-nonsendable-value-error-disappear-when-wrapping-the-expression-in-a-local-closure/82054/4 "2025-09-12T02:24:34Z")

</div>

This is probably the same as [#79262](https://github.com/swiftlang/swift/issues/79262).
