swift-atomics
swift-atomics copied to clipboard
The constructs provided by this package need to be marked `Sendable`
Swift 5.5 introduces Sendable, which puts new constraints on transferring values across concurrency domains.
The constructs provided by this package are designed to be safe to use without synchronization, so they should be marked Sendable. (Hopefully people aren't passing around naked ManagedAtomic values, but in case they do, the compiler should know that this is safe.)
Information
- Package version: 1.0.1
- Platform version: iOS 15 & macOS Monterey
- Swift version: 5.5
Checklist
- [X] If possible, I've reproduced the issue using the
mainbranch of this package. - [X] I've searched for existing reports of the same issue.
The big question that needs to be answered is this: do we need to restrict ManagedAtomic<T>/UnsafeAtomic<T> to be only marked Sendable if T itself is also Sendable?
For example, SomeClass below is very much not safe to transfer across concurrency domains, so it looks like an atomic reference to it shouldn't be, either.
class SomeClass: AtomicReference {
var array: [Int] = []
}
let v: ManagedAtomic<SomeClass> = ...
let instance = v.load(ordering: .sequentiallyConsistent) // OK
instance.array.append(42) // data race
Putting this back on 1.0.2 -- we'll need to decide whether this can technically go in a patch release, but I think we'll want to release this together with #41 in any case. We may just need to rename the milestone to 1.1.0.
This is more urgent than I expected, as UnsafeAtomic has gained an implicit, unconditional, Sendable conformance in the Swift 5.5 release. The implicit conformance isn't right, and the longer we wait to fix it, the more likely it becomes that someone starts depending on it.
Filed #47 to track the UnsafeAtomic issue, which we will need to fix as soon as possible.
Fixing the Sendable conformance for UnsafeAtomic can be done in an emergency patch release, but conforming ManagedAtomic to Sendable can only be done in a new minor release.
The new Swift Concurrency Checking warnings in Xcode 14 (in Targeted mode) told me that my ManagedAtomicLazyReference are not sendable, even though it's one of the main reason to use this package, so having this fixed would be great 😁
When trying to adopt Atomics in Swift NIO, we see CI failure because of missing Sendable conformance in Swift 5.5, where @preconcurrency is not supported.
Since we cannot drop 5.5 at the time, it’s better to add the conformance in Atomics, which holds all the managed atomic types. These values are not intended for production, but instead as part of the test utilities.