Compare commits

..
4 Commits
Author SHA1 Message Date
Tim Condon 789dcf1f3d fix for Swift Collections 1.7.0 (#456) 2026-09-23 19:56:14 +02:00
Tianhao Liu 11645b8feb [AsyncStreaming] Propagate cancellation to late readers (#449)
* [AsyncStreaming] Propagate cancellation to late readers

Distinguish cancellation from normal source completion when a reader continuation is installed after the channel has already finished.

* [AsyncStreaming] Distinguish terminal read states

Track cancellation separately from an already-consumed terminal result so normal source completion does not throw CancellationError. Precondition on repeated reads after cancellation, a final element, or a source failure, with coverage for each terminal path.

* [AsyncStreaming] Handle pending reader cancellation

Deliver cancellation exactly once when it races with a resumed reader, preserve fused final-element completion, and align terminal-read tests and documentation with the reader contract.

* [AsyncStreaming] Test cancellation through public API

Remove the internal-only state-machine test so the regression suite exercises the supported AsyncStreaming interface without @testable import.
2026-09-03 10:53:07 +02:00
da-hye0 a5195afbd5 Fix typos in Evolution and DocC docs (#446) 2026-08-09 14:23:18 +02:00
Zayan KhanandClaude Fable 5 46b3cc3a4e Fix filename typo and template placeholder in Evolution docs (#445)
Rename 0016-mutli-producer-single-consumer-channel.md to the
correctly spelled 0016-multi-...; the document's own proposal link
already uses the correct spelling and currently 404s. Fill in the
NNNN template placeholder in 0017-map-error.md's proposal link.

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-07-23 14:17:02 +02:00
10 changed files with 184 additions and 63 deletions

No files matched your search

+1 -1
View File
@@ -48,7 +48,7 @@ The buffer internally to the share algorithm will only extend back to the furthe
## Runtime Behavior
The runtime behaviors fall into a few categories; ordering, iteration isolation, cancellation, and lifetimes. To understand the beahviors there are a terms useful to define. Each creation of the AsyncIterator of the sequence and invocation of next will be referred to a side of the share iteration. The back pressure to the system to fetch a new element or termination is refered to as demand. The limit which is the pending gate for awaiting until the buffer has been serviced used for the `AsyncBufferSequencePolicy.bounded(_ : Int)` policy. The last special definition is that of the extent which is specifically in this case the lifetime of the asynchronous sequence itself.
The runtime behaviors fall into a few categories; ordering, iteration isolation, cancellation, and lifetimes. To understand the behaviors there are a terms useful to define. Each creation of the AsyncIterator of the sequence and invocation of next will be referred to a side of the share iteration. The back pressure to the system to fetch a new element or termination is referred to as demand. The limit which is the pending gate for awaiting until the buffer has been serviced used for the `AsyncBufferSequencePolicy.bounded(_ : Int)` policy. The last special definition is that of the extent which is specifically in this case the lifetime of the asynchronous sequence itself.
When the underlying type backing the share algorithm is constructed a new extent is created; this is used for tracking the reference lifetime under the hood and is used to both house the iteration but also to identify the point at which no more sides can be constructed. When no more sides can be constructed and no sides are left to iterate then the backing iteration is canceled. This prevents any un-referenced task backing the iteration to not be leaked by the algorith itself.
+2 -2
View File
@@ -1,13 +1,13 @@
# Map Error
* Proposal: [SAA-NNNN](NNNN-map-error.md)
* Proposal: [SAA-0017](0017-map-error.md)
* Authors: [Clive Liu](https://github.com/clive819) [Philippe Hausler](https://github.com/phausler)
* Review Manager: TBD
* Status: **Awaiting review**
## Introduction
Asynchronous sequences often particpate in carefully crafted systems of algorithms spliced together. Some of those algorithms require that both the element type and the failure type are the same as each-other. In order to line those types up for the element we have the map algorithm today, however there are other times at which when using more specific APIs that transforming the Failure type is needed.
Asynchronous sequences often participate in carefully crafted systems of algorithms spliced together. Some of those algorithms require that both the element type and the failure type are the same as each-other. In order to line those types up for the element we have the map algorithm today, however there are other times at which when using more specific APIs that transforming the Failure type is needed.
## Motivation
+2 -1
View File
@@ -145,6 +145,7 @@ let package = Package(
"AsyncStreaming",
.product(name: "BasicContainers", package: "swift-collections"),
.product(name: "ContainersPreview", package: "swift-collections"),
.product(name: "SpanPreview", package: "swift-collections"),
],
swiftSettings: [
.enableExperimentalFeature("SuppressedAssociatedTypesWithDefaults"),
@@ -165,7 +166,7 @@ if Context.environment["SWIFTCI_USE_LOCAL_DEPS"] == nil {
package.dependencies += [
.package(
url: "https://github.com/apple/swift-collections.git",
from: "1.5.1",
from: "1.7.0",
traits: [.trait(name: "UnstableContainersPreview", condition: .when(traits: ["UnstableAsyncStreaming"]))]
)
]
@@ -45,7 +45,7 @@ The buffer internally to the share algorithm will only extend back to the furthe
## Runtime Behavior
The runtime behaviors fall into a few categories; ordering, iteration isolation, cancellation, and lifetimes. To understand the beahviors there are a terms useful to define. Each creation of the AsyncIterator of the sequence and invocation of next will be referred to a side of the share iteration. The back pressure to the system to fetch a new element or termination is refered to as demand. The limit which is the pending gate for awaiting until the buffer has been serviced used for the `AsyncBufferSequencePolicy.bounded(_ : Int)` policy. The last special definition is that of the extent which is specifically in this case the lifetime of the asynchronous sequence itself.
The runtime behaviors fall into a few categories; ordering, iteration isolation, cancellation, and lifetimes. To understand the behaviors there are a terms useful to define. Each creation of the AsyncIterator of the sequence and invocation of next will be referred to a side of the share iteration. The back pressure to the system to fetch a new element or termination is referred to as demand. The limit which is the pending gate for awaiting until the buffer has been serviced used for the `AsyncBufferSequencePolicy.bounded(_ : Int)` policy. The last special definition is that of the extent which is specifically in this case the lifetime of the asynchronous sequence itself.
When the underlying type backing the share algorithm is constructed a new extent is created; this is used for tracking the reference lifetime under the hood and is used to both house the iteration but also to identify the point at which no more sides can be constructed. When no more sides can be constructed and no sides are left to iterate then the backing iteration is canceled. This prevents any un-referenced task backing the iteration to not be leaked by the algorith itself.
@@ -446,8 +446,9 @@ extension DuplexAsyncChannel {
/// in `read`) — and whose outer `.second` arm carries the failure
/// thrown by `body`.
///
/// - Important: After the reader observes a non-`nil` `finalElement`,
/// calling `read(body:)` again is a programmer error.
/// - Important: After the reader observes a non-`nil` `finalElement` or
/// `read(body:)` throws a read-side error, calling `read(body:)` again is
/// a programmer error.
@inlinable
public mutating func read<Return: ~Copyable, BodyFailure: Error>(
body: (inout UniqueDeque<Element>, consuming FinalElement?) async throws(BodyFailure) -> Return
@@ -513,13 +514,8 @@ extension DuplexAsyncChannel {
throw .second(error)
}
case .returnNil:
var empty = UniqueDeque<Element>()
do throws(BodyFailure) {
return try await body(&empty, nil)
} catch {
throw .second(error)
}
case .throwCancellation:
throw .first(.second(CancellationError()))
case .suspend:
do {
@@ -272,6 +272,8 @@ extension MultiProducerSingleConsumerAsyncChannel {
switch consume action {
case .resumeReader(let c):
c.resume()
case .resumeReaderWithCancellationError(let c):
c.resume(throwing: .second(CancellationError()))
case .none:
break
}
@@ -410,12 +412,12 @@ extension MultiProducerSingleConsumerAsyncChannel._Storage {
case .channeling(let s):
let producers = Array(s.suspendedProducers.lazy.map { $0.1 })
let onTerminations = s.onTerminations
self = .init(state: .finished(.init(sourceFinished: false)))
self = .init(state: .finished(.consumed))
return .failProducersAndCallOnTerminations(producers, onTerminations)
case .sourceFinished(let s):
let onTerminations = s.onTerminations
self = .init(state: .finished(.init(sourceFinished: true)))
self = .init(state: .finished(.consumed))
return .callOnTerminations(onTerminations)
case .finished(let s):
@@ -679,16 +681,16 @@ extension MultiProducerSingleConsumerAsyncChannel._Storage {
/// The channel was finished with a failure and the buffer is now drained;
/// throw the failure to the reader.
case throwFailure(Failure?, [(UInt64, @Sendable () -> Void)])
case returnNil
/// Cancellation finished the channel before it could be delivered to the reader.
case throwCancellation
}
@inlinable
mutating func readAvailable() -> ReadAvailableAction {
switch consume self._state {
case .channeling(var s):
let isProducerBufferEmpty = s.buffer.withValue {
$0.borrow()!.value.isEmpty
}
// Optional chaining borrows the deque; unwrap only the Boolean result.
let isProducerBufferEmpty = s.buffer.withValue { $0?.isEmpty }!
guard isProducerBufferEmpty else {
// We are going to swap the two buffers around. The cached buffer
// may not exist yet on the first read; fall back to a fresh empty
@@ -731,7 +733,7 @@ extension MultiProducerSingleConsumerAsyncChannel._Storage {
let fe = s.finalElement.swap(newValue: nil)
let onTerminations = s.onTerminations
let failure = s.failure
self = .init(state: .finished(.init(sourceFinished: true)))
self = .init(state: .finished(.consumed))
if let failure {
return .throwFailure(failure, onTerminations)
@@ -745,8 +747,14 @@ extension MultiProducerSingleConsumerAsyncChannel._Storage {
)
case .finished(let s):
self = .init(state: .finished(s))
return .returnNil
switch s {
case .cancelled:
self = .init(state: .finished(.consumed))
return .throwCancellation
case .consumed:
self = .init(state: .finished(.consumed))
preconditionFailure("MultiProducerSingleConsumerAsyncChannel.read called after termination")
}
}
}
@@ -800,6 +808,9 @@ extension MultiProducerSingleConsumerAsyncChannel._Storage {
@usableFromInline
enum SuspendReadAction: ~Copyable, Sendable {
case resumeReader(UnsafeContinuation<Void, EitherError<Failure, CancellationError>>)
case resumeReaderWithCancellationError(
UnsafeContinuation<Void, EitherError<Failure, CancellationError>>
)
}
@inlinable
@@ -811,7 +822,7 @@ extension MultiProducerSingleConsumerAsyncChannel._Storage {
guard s.readerContinuation == nil else {
fatalError("MultiProducerSingleConsumerAsyncChannel internal inconsistency: concurrent readers")
}
let isEmpty = s.buffer.withValue { $0.borrow()!.value.isEmpty }
let isEmpty = s.buffer.withValue { $0?.isEmpty }!
if !isEmpty {
self = .init(state: .channeling(s))
return .resumeReader(continuation)
@@ -825,8 +836,16 @@ extension MultiProducerSingleConsumerAsyncChannel._Storage {
return .resumeReader(continuation)
case .finished(let s):
self = .init(state: .finished(s))
return .resumeReader(continuation)
switch s {
case .cancelled:
// Cancellation may finish the channel before this continuation is installed.
// Consume that pending cancellation exactly once.
self = .init(state: .finished(.consumed))
return .resumeReaderWithCancellationError(continuation)
case .consumed:
self = .init(state: .finished(.consumed))
preconditionFailure("MultiProducerSingleConsumerAsyncChannel.read called after termination")
}
}
}
@@ -850,10 +869,11 @@ extension MultiProducerSingleConsumerAsyncChannel._Storage {
let reader = s.readerContinuation.take()
let producers = Array(s.suspendedProducers.lazy.map { $0.1 })
let onTerminations = s.onTerminations
self = .init(state: .finished(.init(sourceFinished: false)))
if let reader {
self = .init(state: .finished(.consumed))
return .resumeReaderWithCancellationError(reader, producers, onTerminations)
}
self = .init(state: .finished(.cancelled))
return .failProducersAndCallOnTerminations(producers, onTerminations)
case .sourceFinished(let s):
@@ -980,12 +1000,9 @@ extension MultiProducerSingleConsumerAsyncChannel._Storage._StateMachine {
}
@usableFromInline
struct Finished: ~Copyable, Sendable {
@usableFromInline
var sourceFinished: Bool
@inlinable
init(sourceFinished: Bool) { self.sourceFinished = sourceFinished }
enum Finished: Sendable {
case cancelled
case consumed
}
case channeling(Channeling)
@@ -320,8 +320,9 @@ extension MultiProducerSingleConsumerAsyncChannel {
/// the task is canceled while suspended in `read`) — and whose outer
/// `.second` arm carries the failure thrown by `body`.
///
/// - Important: After the reader observes a non-`nil` `finalElement`,
/// calling `read(body:)` again is a programmer error.
/// - Important: After the reader observes a non-`nil` `finalElement` or
/// `read(body:)` throws a read-side error, calling `read(body:)` again is
/// a programmer error.
@inlinable
public mutating func read<Return: ~Copyable, BodyFailure: Error>(
body: (inout UniqueDeque<Element>, consuming FinalElement?) async throws(BodyFailure) -> Return
@@ -391,13 +392,8 @@ extension MultiProducerSingleConsumerAsyncChannel {
throw .second(error)
}
case .returnNil:
var empty = UniqueDeque<Element>()
do throws(BodyFailure) {
return try await body(&empty, nil)
} catch {
throw .second(error)
}
case .throwCancellation:
throw .first(.second(CancellationError()))
case .suspend:
do {
@@ -12,6 +12,7 @@
#if UnstableAsyncStreaming && compiler(>=6.4)
import BasicContainers
import ContainersPreview
import SpanPreview
extension Array {
init(_ span: Span<Element>) {
@@ -17,6 +17,8 @@ import ContainersPreview
import DequeModule
import Testing
private struct MultiProducerSingleConsumerAsyncChannelTestError: Error {}
@Suite(.serialized)
struct MultiProducerSingleConsumerAsyncChannelTests {
// MARK: - AsyncReader.read
@@ -75,7 +77,120 @@ struct MultiProducerSingleConsumerAsyncChannelTests {
@available(macOS 15.0, iOS 18.0, tvOS 18.0, watchOS 11.0, visionOS 2.0, *)
@Test
func readReturnsEmptyBufferOnEOSAfterFinish() async throws {
func readThrowsCancellationWhenTaskStartsCancelled() async {
await withTaskGroup(of: Void.self) { group in
group.cancelAll()
// A child added to an already-cancelled group starts cancelled, so the
// cancellation handler runs before the reader continuation is installed.
group.addTask {
await MultiProducerSingleConsumerAsyncChannel.withChannel(
of: Int.self,
backpressureStrategy: .watermark(low: 1, high: 2)
) { channel, source in
var channel = channel
_ = consume source
do {
_ = try await channel.read { _, _ in
Issue.record("read body must not run after cancellation")
}
Issue.record("expected CancellationError")
} catch let EitherError<EitherError<Never, CancellationError>, Never>.first(.second(error)) {
_ = error
} catch {
Issue.record("unexpected error: \(error)")
}
}
}
await group.waitForAll()
}
}
@available(macOS 15.0, iOS 18.0, tvOS 18.0, watchOS 11.0, visionOS 2.0, *)
@Test
func readDoesNotThrowCancellationWhenSourceFinishes() async throws {
try await MultiProducerSingleConsumerAsyncChannel.withChannel(
of: Int.self,
backpressureStrategy: .watermark(low: 1, high: 2)
) { channel, source in
var channel = channel
let source = source
source.finish()
var sawFinalElement = false
try await channel.read { buffer, finalElement in
#expect(buffer.count == 0)
sawFinalElement = finalElement != nil
}
#expect(sawFinalElement)
}
}
@available(macOS 15.0, iOS 18.0, tvOS 18.0, watchOS 11.0, visionOS 2.0, *)
@Test
func readAfterCancellationPreconditions() async {
await #expect(processExitsWith: .failure) {
await withTaskGroup(of: Void.self) { group in
group.cancelAll()
group.addTask {
await MultiProducerSingleConsumerAsyncChannel.withChannel(
of: Int.self,
backpressureStrategy: .watermark(low: 1, high: 2)
) { channel, source in
var channel = channel
_ = consume source
try? await channel.read { _, _ in }
try? await channel.read { _, _ in }
}
}
await group.waitForAll()
}
}
}
@available(macOS 15.0, iOS 18.0, tvOS 18.0, watchOS 11.0, visionOS 2.0, *)
@Test
func readAfterFinalElementPreconditions() async {
await #expect(processExitsWith: .failure) {
try await MultiProducerSingleConsumerAsyncChannel.withChannel(
of: Int.self,
backpressureStrategy: .watermark(low: 1, high: 2)
) { channel, source in
var channel = channel
let source = source
source.finish()
try await channel.read { _, finalElement in
#expect(finalElement != nil)
}
try await channel.read { _, _ in }
}
}
}
@available(macOS 15.0, iOS 18.0, tvOS 18.0, watchOS 11.0, visionOS 2.0, *)
@Test
func readAfterFailurePreconditions() async {
await #expect(processExitsWith: .failure) {
await MultiProducerSingleConsumerAsyncChannel.withChannel(
of: Int.self,
throwing: MultiProducerSingleConsumerAsyncChannelTestError.self,
backpressureStrategy: .watermark(low: 1, high: 2)
) { channel, source in
var channel = channel
let source = source
source.finish(throwing: MultiProducerSingleConsumerAsyncChannelTestError())
try? await channel.read { _, _ in }
try? await channel.read { _, _ in }
}
}
}
@available(macOS 15.0, iOS 18.0, tvOS 18.0, watchOS 11.0, visionOS 2.0, *)
@Test
func readReturnsEmptyBufferAndFinalElementOnEOSAfterFinish() async throws {
try await MultiProducerSingleConsumerAsyncChannel.withChannel(
of: Int.self,
backpressureStrategy: .watermark(low: 2, high: 4)
@@ -86,17 +201,18 @@ struct MultiProducerSingleConsumerAsyncChannelTests {
var writeBuffer = UniqueArray<Int>(minimumCapacity: 1)
writeBuffer.append(1)
try await source.write(buffer: &writeBuffer)
source.finish()
try await channel.read { buffer, _ in
try await channel.read { buffer, finalElement in
#expect(buffer.count == 1)
#expect(finalElement == nil)
buffer.removeAll()
}
var sawEmpty = false
try await channel.read { buffer, _ in
sawEmpty = buffer.count == 0
source.finish()
try await channel.read { buffer, finalElement in
#expect(buffer.count == 0)
#expect(finalElement != nil)
}
#expect(sawEmpty)
}
}
@@ -426,15 +542,12 @@ struct MultiProducerSingleConsumerAsyncChannelTests {
var source = source
group.addTask {
// Reader drains until EOS.
// Reader drains until it observes the final element.
while true {
var done = false
try await channel.read { buffer, _ in
if buffer.count == 0 {
done = true
} else {
buffer.removeAll()
}
try await channel.read { buffer, finalElement in
buffer.removeAll()
done = finalElement != nil
}
if done { break }
}
@@ -465,13 +578,10 @@ struct MultiProducerSingleConsumerAsyncChannelTests {
nonisolated(unsafe) var collected: [Int] = []
var done = false
while !done {
try await channel.read { buffer, _ in
if buffer.count == 0 {
done = true
} else {
var c = buffer.consumeAll()
while let v = c.next() { collected.append(v) }
}
try await channel.read { buffer, finalElement in
var c = buffer.consumeAll()
while let v = c.next() { collected.append(v) }
done = finalElement != nil
}
}
#expect(collected == Array(0..<total))