Fix a data race error with the internal Optional.takeSending (#399)

We added a little helper to move a non-Sendable type into an optional and take it out as sending so that it is disconnected. This was actually a data race hole which got fixed by the compiler so we need to use a proper disconnected abstractions.
This commit is contained in:
Franz Busch authored and GitHub committed 2026-02-11 11:02:07 -08:00
1 parent 103f5e5bea
commit 2971dd5d9f
3 files changed
+50 -19

No files matched your search

@@ -0,0 +1,38 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Async Algorithms open source project
//
// Copyright (c) 2026 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//
#if compiler(>=6.1)
// This is a helper type to move a non-Sendable value across isolation regions.
@usableFromInline
struct Disconnected<Value: ~Copyable>: ~Copyable, Sendable {
// This is safe since we take the value as sending and take consumes it
// and returns it as sending.
private nonisolated(unsafe) var value: Value?
@usableFromInline
init(value: consuming sending Value) {
self.value = .some(value)
}
@usableFromInline
consuming func take() -> sending Value {
nonisolated(unsafe) let value = self.value.take()!
return value
}
@usableFromInline
mutating func swap(newValue: consuming sending Value) -> sending Value {
nonisolated(unsafe) let value = self.value.take()!
self.value = consume newValue
return value
}
}
#endif
@@ -261,9 +261,9 @@ extension MultiProducerSingleConsumerAsyncChannel {
) throws -> MultiProducerSingleConsumerAsyncChannel<Element, Failure>.Source.SendResult {
// We need to move the value into an optional since we don't have call-once
// closures in the Swift yet.
var optionalSequence = Optional(sequence)
var optionalSequence = Disconnected(value: Optional(sequence))
let action = self._stateMachine.withLock {
let sequence = optionalSequence.takeSending()!
let sequence = optionalSequence.swap(newValue: nil)!
return $0.send(sequence)
}
@@ -313,11 +313,11 @@ extension MultiProducerSingleConsumerAsyncChannel {
callbackToken: UInt64,
onProduceMore: sending @escaping (Result<Void, Error>) -> Void
) {
// We need to move the value into an optional since we don't have call-once
// We need to move the value into a disconnected since we don't have call-once
// closures in the Swift yet.
var optionalOnProduceMore = Optional(onProduceMore)
var optionalOnProduceMore = Disconnected(value: Optional(onProduceMore))
let action = self._stateMachine.withLock {
let onProduceMore = optionalOnProduceMore.takeSending()!
let onProduceMore = optionalOnProduceMore.swap(newValue: nil)!
return $0.enqueueProducer(callbackToken: callbackToken, onProduceMore: onProduceMore)
}
@@ -1720,15 +1720,6 @@ enum _MultiProducerSingleConsumerSuspendedProducer {
case continuation(UnsafeContinuation<Void, Error>)
}
extension Optional where Wrapped: ~Copyable {
@usableFromInline
mutating func takeSending() -> sending Self {
let result = consume self
self = nil
return result
}
}
@usableFromInline
struct SendableConsumeOnceBox<Wrapped> {
@usableFromInline
@@ -1741,7 +1732,8 @@ struct SendableConsumeOnceBox<Wrapped> {
@inlinable
consuming func take() -> sending Wrapped {
return self.wrapped.takeSending()!
nonisolated(unsafe) let value = self.wrapped.take()!
return value
}
}
#endif
@@ -138,23 +138,24 @@ public struct MultiProducerSingleConsumerAsyncChannel<Element, Failure: Error>:
public struct ChannelAndStream: ~Copyable {
/// The channel.
@usableFromInline
var channel: MultiProducerSingleConsumerAsyncChannel?
var channel: Disconnected<MultiProducerSingleConsumerAsyncChannel?>
/// Takes and returns the channel.
///
/// - Important: If this is called more than once it will result in a runtime crash.
@inlinable
public mutating func takeChannel() -> sending MultiProducerSingleConsumerAsyncChannel {
return self.channel.takeSending()!
var channel = self.channel.swap(newValue: nil)
return channel.take()!
}
/// The source.
public var source: Source
init(
channel: consuming MultiProducerSingleConsumerAsyncChannel,
channel: consuming sending MultiProducerSingleConsumerAsyncChannel,
source: consuming Source
) {
self.channel = .some(channel)
self.channel = Disconnected(value: channel)
self.source = source
}
}