From e32e9ef60ceb495b1afd775fc0a46a7b31594ffe Mon Sep 17 00:00:00 2001 From: Philippe Hausler Date: Mon, 28 Jul 2025 16:50:48 -0700 Subject: [PATCH 1/4] [WIP] Draft pitch for share --- Evolution/NNNN-share.md | 178 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 178 insertions(+) create mode 100644 Evolution/NNNN-share.md diff --git a/Evolution/NNNN-share.md b/Evolution/NNNN-share.md new file mode 100644 index 00000000..deb8bdb7 --- /dev/null +++ b/Evolution/NNNN-share.md @@ -0,0 +1,178 @@ +# Share + +## Introduction + +Many of the AsyncSequence adopting types only permit a one singular consumption. However there are many times that the same produced values are useful in more than one place. Out of that mechanism there are a few approaches to share, distribute, and broadcast those values. This proposal will focus on one concept; sharing. Sharing is where each consumption independently can make forward progress and get the same values but do not replay from the beginning of time. + +## Motivation + +There are many potential usages for the sharing concept of AsyncSequences. + +One such example is the case where a source of data as an asynchronous sequence needs to be consumed by updating UI, logging, and additionally a network connection. This particular case does not matter on which uses but instead that those uses are independent of each other. It would not be expected for networking to block or delay the updates to UI, nor should logging. This example case also illustrates that the isolation of each side might be different and that some of the sides may not tolerate coalescing or dropping values. + +There are many other use cases that have been requested for this family of algorithms. Since the release of AsyncAlgorithms it has perhaps been the most popularly requested set of behaviors as additions to the package. + +## Proposed solution + +AsyncAlgorithms will introduce a new extension function on AsyncSequence that will provide a shareable asynchronous sequence that will produce the same values upon iteration from multiple instances of it's AsyncIterator. Those iterations can take place in multiple isolations. + +When values from a differing isolation cannot be coalesced, the two options available are either awaiting (an exertion of back-pressure across the sequences) or buffering (an internal back-pressure to a buffer). Replaying the values from the beginning of the creation of the sequence is a distinctly different behavior that should be considered a different use case. This then leaves the behavioral characteristic of this particular operation of share as; sharing a buffer of values started from the initialization of a new iteration of the sequence. Control over that buffer should then have options to determine the behavior, similar to how AsyncStream allows that control. It should have options to be unbounded, buffering the oldest count of elements, or buffering the newest count of elements. + +It is critical to identify that this is one algorithm in the family of algorithms for sharing values. It should not attempt to solve all behavioral requirements but instead serve a common set of them that make cohesive sense together. This proposal is not mutually exclusive to the other algorithms in the sharing family. + +## Detailed design + +It is not just likely but perhaps a certainty that other algorithms will end up needing the same concept of a buffering policy beyond just AsyncStream and the new sharing mechanism. A new type in AsyncAlgorithms will be introduced to handle this. [^BufferingPolicy] + +```swift +/// A strategy that handles exhaustion of a buffer’s capacity. +public enum BufferingPolicy: Sendable { + /// Continue to add to the buffer, without imposing a limit on the number + /// of buffered elements. + case unbounded + + /// When the buffer is full, discard the newly received element. + /// + /// This strategy enforces keeping at most the specified number of oldest + /// values. + case bufferingOldest(Int) + + /// When the buffer is full, discard the oldest element in the buffer. + /// + /// This strategy enforces keeping at most the specified number of newest + /// values. + case bufferingNewest(Int) +} +``` + +A new extension will be added to return a concrete type representing the share algorithm. This extension will take a buffering policy to identify how the buffer will be handled when iterations do not consume at the same rate. + +A new AsyncSequence type will be introduced that is explicitly marked as `Sendable`. This annotation identifies to the developer that this sequence can be shared and stored. Because the type is intended to be stored it cannot be returned by the extension as a `some AsyncSequence & Sendable` since that cannot be assigned to a stored property. Additionally the type of `AsyncShareSequence`, since indented to be stored, will act as a quasi erasing-barrier to the type information of previous sequences in the chain of algorithms in that it will only hold the generic information of the `Element` and `Failure` as part of it's public interface and not the "Base" asynchronous sequence it was created from. + +```swift +extension AsyncSequence where Element: Sendable { + public func share( + bufferingPolicy: BufferingPolicy = .unbounded + ) -> AsyncShareSequence +} + +public struct AsyncShareSequence: AsyncSequence, Sendable { + public struct Iterator: AsyncIteratorProtocol { + public mutating func next(isolation actor: isolated (any Actor)?) async throws(Failure) -> Element? + } + + public func makeAsyncIterator() -> Iterator +} + +@available(*, unavailable) +extension AsyncShareSequence.Iterator: Sendable { } +``` + +The buffer internally to the share algorithm will only extend back to the furthest element available but there will only be a singular buffer shared across all iterators. This ensures that with the application of the buffering policy the storage size is as minimal as possible while still allowing all iterations to avoid dropping values and keeping the memory usage in check. + +## Runtime Behavior + +The construction of the `AsyncShareSequence` will initially construct a shared iteration reference. This means that all instances of the structure of the `AsyncShareSequence` will reference to the same iteration. + +Upon creation of the `Iterator` via `makeAsyncIterator` a new "side" will be constructed to identify the specific iterator interacting with the shared iteration. Then when next is invoked is where the first actual action takes place. + +The next method will first checkout from a critical region the underlying AsyncIterator from the base. If that is successful (i.e. no other iteration sides have already checked it out) then it will invoke the next method of that iterator (forwarding in the actor isolation). If an element is produced then it enqueues the element to the shared buffer, checks in the iterator, adjusts the index in the buffer, and finds all pending continuations all in a shared critical region by a mutex. Then those continuations will be resumed with the given element. + +If no element is returned by the base iterator (signifying the terminal state);then the process is similar except it will instead mark the sequence as finished and resume with nil to any active continuations. Similarly with failures that will set the state as terminal but also store the error for further iteration points that need eventual termination. + +Then all sides are "drained" such that continuations are placed into the shared state and resumed when an element is available for that position. + +Practically this all means that a given iteration may be "behind" another and can eventually catch up (provided it is within the buffer limit). + +```swift +let exampleSource = [0, 1, 2, 3, 4].async.share() + +let t1 = Task { + for await element in exampleSource { + if element == 0 { + try? await Task.sleep(for: .seconds(1)) + } + print("Task 1", element) + } +} + +let t2 = Task { + for await element in exampleSource { + if element == 3 { + try? await Task.sleep(for: .seconds(1)) + } + print("Task 2", element) + } +} + +await t1.value +await t2.value + +``` + +This example will print a possible ordering of the following: + +``` +Task 2 0 +Task 2 1 +Task 2 2 +Task 1 0 +Task 2 3 +Task 2 4 +Task 1 1 +Task 1 2 +Task 1 3 +Task 1 4 +``` + +The order of the interleaving of the prints are not guaranteed; however the order of the elements per iteration is. Likewise in this buffering case it is guaranteed that all values are represented in the output. + +If the creation were altered to the following: + +```swift +let exampleSource = [0, 1, 2, 3, 4].async.share(bufferingPolicy: .bufferingNewest(2)) +``` + +The output would print the possible ordering of: + +``` +Task 2 0 +Task 2 1 +Task 2 2 +Task 1 0 +Task 2 4 +Task 1 3 +Task 1 4 +``` + +Some values are dropped due to the buffering policy, but eventually they reach consistency. Which similarly works for the following: + +``` +let exampleSource = [0, 1, 2, 3, 4].async.share(bufferingPolicy: .bufferingOldest(2)) +``` + +``` +Task 2 0 +Task 2 1 +Task 2 2 +Task 1 0 +Task 2 4 +Task 1 1 +Task 1 2 +``` + +However in this particular case the newest values are the dropped elements. + +## Usage + +It is expected that this operator will be unlike other + +## Effect on API resilience + +This is an additive API and no existing systems are changed, however it will introduce a few new types that will need to be maintained as ABI interfaces. Since the intent of this is to provide a mechanism to store AsyncSequences to a shared context the type must be exposed as ABI (for type sizing). + +## Alternatives considered + +[^BufferingPolicy] It has been considered that this particular policy would be nested inside the `AsyncShareSequence` type. However since this seems to be something that will be useful for other types it makes sense to expose it as a top level type. However if it is determined that a general form of a buffering policy would require additional behaviors this might be a debatable placement to move back to an interior type similar to AsyncStream. + + From 8447eef7863062aa928cdf47ccbff52b4829b363 Mon Sep 17 00:00:00 2001 From: Philippe Hausler Date: Thu, 31 Jul 2025 12:49:31 -0700 Subject: [PATCH 2/4] Update the proposal with some initial feedback and add a first draft implementation of share --- Evolution/NNNN-share.md | 64 +-- .../AsyncAlgorithms/AsyncShareSequence.swift | 437 ++++++++++++++++++ 2 files changed, 451 insertions(+), 50 deletions(-) create mode 100644 Sources/AsyncAlgorithms/AsyncShareSequence.swift diff --git a/Evolution/NNNN-share.md b/Evolution/NNNN-share.md index deb8bdb7..9fa5ba83 100644 --- a/Evolution/NNNN-share.md +++ b/Evolution/NNNN-share.md @@ -22,65 +22,33 @@ It is critical to identify that this is one algorithm in the family of algorithm ## Detailed design -It is not just likely but perhaps a certainty that other algorithms will end up needing the same concept of a buffering policy beyond just AsyncStream and the new sharing mechanism. A new type in AsyncAlgorithms will be introduced to handle this. [^BufferingPolicy] +A new extension will be added to return a `Sendable` `AsyncSequence`. This extension will take a buffering policy to identify how the buffer will be handled when iterations do not consume at the same rate. -```swift -/// A strategy that handles exhaustion of a buffer’s capacity. -public enum BufferingPolicy: Sendable { - /// Continue to add to the buffer, without imposing a limit on the number - /// of buffered elements. - case unbounded - - /// When the buffer is full, discard the newly received element. - /// - /// This strategy enforces keeping at most the specified number of oldest - /// values. - case bufferingOldest(Int) - - /// When the buffer is full, discard the oldest element in the buffer. - /// - /// This strategy enforces keeping at most the specified number of newest - /// values. - case bufferingNewest(Int) -} -``` - -A new extension will be added to return a concrete type representing the share algorithm. This extension will take a buffering policy to identify how the buffer will be handled when iterations do not consume at the same rate. - -A new AsyncSequence type will be introduced that is explicitly marked as `Sendable`. This annotation identifies to the developer that this sequence can be shared and stored. Because the type is intended to be stored it cannot be returned by the extension as a `some AsyncSequence & Sendable` since that cannot be assigned to a stored property. Additionally the type of `AsyncShareSequence`, since indented to be stored, will act as a quasi erasing-barrier to the type information of previous sequences in the chain of algorithms in that it will only hold the generic information of the `Element` and `Failure` as part of it's public interface and not the "Base" asynchronous sequence it was created from. +The `Sendable` annotation identifies to the developer that this sequence can be shared and stored in an existental `any`. ```swift extension AsyncSequence where Element: Sendable { public func share( - bufferingPolicy: BufferingPolicy = .unbounded - ) -> AsyncShareSequence + bufferingPolicy: AsyncBufferSequencePolicy = .unbounded + ) -> some AsyncSequence & Sendable } - -public struct AsyncShareSequence: AsyncSequence, Sendable { - public struct Iterator: AsyncIteratorProtocol { - public mutating func next(isolation actor: isolated (any Actor)?) async throws(Failure) -> Element? - } - - public func makeAsyncIterator() -> Iterator -} - -@available(*, unavailable) -extension AsyncShareSequence.Iterator: Sendable { } ``` -The buffer internally to the share algorithm will only extend back to the furthest element available but there will only be a singular buffer shared across all iterators. This ensures that with the application of the buffering policy the storage size is as minimal as possible while still allowing all iterations to avoid dropping values and keeping the memory usage in check. +The buffer internally to the share algorithm will only extend back to the furthest element available but there will only be a singular buffer shared across all iterators. This ensures that with the application of the buffering policy the storage size is as minimal as possible while still allowing all iterations to avoid dropping values and keeping the memory usage in check. The signature reuses the existing `AsyncBufferSequencePolicy` type to specify the behavior around buffering either responding to how it should limit emitting to the buffer or what should happen when the buffer is exceeded. ## Runtime Behavior -The construction of the `AsyncShareSequence` will initially construct a shared iteration reference. This means that all instances of the structure of the `AsyncShareSequence` will reference to the same iteration. +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. -Upon creation of the `Iterator` via `makeAsyncIterator` a new "side" will be constructed to identify the specific iterator interacting with the shared iteration. Then when next is invoked is where the first actual action takes place. +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. -The next method will first checkout from a critical region the underlying AsyncIterator from the base. If that is successful (i.e. no other iteration sides have already checked it out) then it will invoke the next method of that iterator (forwarding in the actor isolation). If an element is produced then it enqueues the element to the shared buffer, checks in the iterator, adjusts the index in the buffer, and finds all pending continuations all in a shared critical region by a mutex. Then those continuations will be resumed with the given element. +That construction then creates an initial shared state and buffer. No task is started initially; it is only upon the first demand that the task backing the iteration is started; this means on the first call to next a task is spun up servicing all potential sides. The order of which the sides are serviced is not specified and cannot be relied upon, however the order of delivery within a side is always guarenteed to be ordered. The singular task servicing the iteration will be the only place holding any sort of iterator from the base `AsyncSequence`; so that iterator is isolated and not sent from one isolation to another. That iteration first awaits any limit availability and then awaits for a demand given by a side. After-which it then awaits an element or terminal event from the iterator and enqueues the elements to the buffer. -If no element is returned by the base iterator (signifying the terminal state);then the process is similar except it will instead mark the sequence as finished and resume with nil to any active continuations. Similarly with failures that will set the state as terminal but also store the error for further iteration points that need eventual termination. +The buffer itself is only held in one location, each side however has a cursor index into that buffer and when values are consumed it adjusts the indexes accordingly; leaving the buffer usage only as big as the largest deficit. This means that new sides that are started post initial start up will not have a "replay" effect; that is a similar but distinct algorithm and is not addressed by this proposal. Any buffer size sensitive systems that wish to adjust behavior should be aware that specifying a policy is a suggested step. However in common usage similar to other such systems servicing desktop and mobile applications the default and common behavior is to be unbounded. This allows for a progressive disclosure from common usage that just works out of the box with no configuration, to more advanced cases that need finer grained control. Furthermore there are scenarios where one might want ways of identifing dropped value events within the iteration of a side, this is something that will be addressed later in an upcoming proposal. -Then all sides are "drained" such that continuations are placed into the shared state and resumed when an element is available for that position. +As previously stated, the isolation of the iteration of the upstream/base AsyncSequence is to a detached task, this ensures that individual sides can have independent cancellation. Those cancellations will have the effect of remvoing that side from the shared iteration and cleaning up accordingly (including adjusting the trimming of the internal buffer). + +Representing concurrent access is difficult to express all potential examples but there are a few cases included with this proposal to illustrate some of the behaviors. If a more comprehensive behavioral analysis is needed, it is strongly suggested to try out the pending pull request to identify how specific behaviors work. Please keep in mind that the odering between tasks is not specified, only the order within one side of iteration. Practically this all means that a given iteration may be "behind" another and can eventually catch up (provided it is within the buffer limit). @@ -127,7 +95,7 @@ Task 1 4 The order of the interleaving of the prints are not guaranteed; however the order of the elements per iteration is. Likewise in this buffering case it is guaranteed that all values are represented in the output. -If the creation were altered to the following: +If the creation were instead altered to the following: ```swift let exampleSource = [0, 1, 2, 3, 4].async.share(bufferingPolicy: .bufferingNewest(2)) @@ -163,16 +131,12 @@ Task 1 2 However in this particular case the newest values are the dropped elements. -## Usage - -It is expected that this operator will be unlike other - ## Effect on API resilience This is an additive API and no existing systems are changed, however it will introduce a few new types that will need to be maintained as ABI interfaces. Since the intent of this is to provide a mechanism to store AsyncSequences to a shared context the type must be exposed as ABI (for type sizing). ## Alternatives considered -[^BufferingPolicy] It has been considered that this particular policy would be nested inside the `AsyncShareSequence` type. However since this seems to be something that will be useful for other types it makes sense to expose it as a top level type. However if it is determined that a general form of a buffering policy would require additional behaviors this might be a debatable placement to move back to an interior type similar to AsyncStream. +It has been considered that the buffering policy would be nested inside the `AsyncShareSequence` type. However since this seems to be something that will be useful for other types it makes sense to use an existing type from a top level type. However if it is determined that a general form of a buffering policy would require additional behaviors this might be a debatable placement to move back to an interior type similar to AsyncStream. diff --git a/Sources/AsyncAlgorithms/AsyncShareSequence.swift b/Sources/AsyncAlgorithms/AsyncShareSequence.swift new file mode 100644 index 00000000..f893db75 --- /dev/null +++ b/Sources/AsyncAlgorithms/AsyncShareSequence.swift @@ -0,0 +1,437 @@ +import Synchronization + +@available(macOS 26.0, *) +extension AsyncSequence where Element: Sendable { + @available(macOS 26.0, *) // TODO: fix the availability for this to be defined as @available(AsyncAlgorithms 1.1, *) + public func share(bufferingPolicy: AsyncBufferSequencePolicy = .unbounded) -> some AsyncSequence & Sendable { + return AsyncShareSequence(self, bufferingPolicy: bufferingPolicy) + } +} + +@available(macOS 26.0, *) +struct AsyncShareSequence: Sendable where Base.Element: Sendable { + final class Side { + struct State { + var continuaton: CheckedContinuation, Never>? + var position = 0 + + func offset(_ adjustment: Int) -> State { + State(continuaton: continuaton, position: position - adjustment) + } + } + + let iteration: Iteration + let id: Int + + init(_ iteration: Iteration) { + self.iteration = iteration + id = iteration.registerSide() + } + + deinit { + iteration.unregisterSide(id) + } + + func next(isolation actor: isolated (any Actor)?) async throws(Failure) -> Element? { + try await iteration.next(isolation: actor, id: id) + } + } + + final class Iteration: Sendable { + // this is the swapped state of transferring the base to the iterating task + // it does send the Base... but only one transfer + enum IteratingTask: @unchecked Sendable { + case pending(Base) + case starting + case running(Task) + case cancelled + + var isStarting: Bool { + switch self { + case .starting: true + default: false + } + } + + func cancel() { + switch self { + case .running(let task): + task.cancel() + default: + break + } + } + } + struct State: Sendable { + enum StoragePolicy: Sendable { + case unbounded + case bufferingOldest(Int) + case bufferingNewest(Int) + } + + var generation = 0 + var sides = [Int: Side.State]() + var iteratingTask: IteratingTask + var buffer = [Element]() + var finished = false + var failure: Failure? + var limit: CheckedContinuation? + var demand: CheckedContinuation? + + let storagePolicy: StoragePolicy + + init(_ base: Base, bufferingPolicy: AsyncBufferSequencePolicy) { + self.iteratingTask = .pending(base) + switch bufferingPolicy.policy { + case .bounded: self.storagePolicy = .unbounded + case .bufferingOldest(let bound): self.storagePolicy = .bufferingOldest(bound) + case .bufferingNewest(let bound): self.storagePolicy = .bufferingNewest(bound) + case .unbounded: self.storagePolicy = .unbounded + } + } + + mutating func trimBuffer() { + if let minimumIndex = sides.values.map({ $0.position }).min(), minimumIndex > 0 { + buffer.removeFirst(minimumIndex) + sides = sides.mapValues { + $0.offset(minimumIndex) + } + } + } + + mutating func emit(_ value: T) -> (T, CheckedContinuation?, CheckedContinuation?, Bool) { + defer { + limit = nil + demand = nil + } + if case .cancelled = iteratingTask { + return (value, limit, demand, true) + } else { + return (value, limit, demand, false) + } + } + + mutating func enqueue(_ element: Element) { + let count = buffer.count + + switch storagePolicy { + case .unbounded: + buffer.append(element) + case .bufferingOldest(let limit): + if count < limit { + buffer.append(element) + } + case .bufferingNewest(let limit): + if count < limit { + buffer.append(element) + } else if count > 0 { + buffer.removeFirst() + buffer.append(element) + } + } + } + + mutating func finish() { + finished = true + } + + mutating func fail(_ error: Failure) { + finished = true + failure = error + } + } + + let state: Mutex + let limit: Int? + + init(_ base: Base, bufferingPolicy: AsyncBufferSequencePolicy) { + state = Mutex(State(base, bufferingPolicy: bufferingPolicy)) + switch bufferingPolicy.policy { + case .bounded(let limit): + self.limit = limit + default: + self.limit = nil + } + } + + func cancel() { + // TODO: this currently is a hard cancel, it should be refined to only cancel when everything is terminal + let (task, limit, demand, cancelled) = state.withLock { state -> (IteratingTask?, CheckedContinuation?, CheckedContinuation?, Bool) in + defer { + state.iteratingTask = .cancelled + state.limit = nil + state.demand = nil + } + return state.emit(state.iteratingTask) + } + task?.cancel() + limit?.resume(returning: cancelled) + demand?.resume() + } + + func registerSide() -> Int { + state.withLock { state in + defer { state.generation += 1 } + state.sides[state.generation] = Side.State() + return state.generation + } + } + + func unregisterSide(_ id: Int) { + let (side, continuation, cancelled) = state.withLock { state -> (Side.State?, CheckedContinuation?, Bool) in + let side = state.sides.removeValue(forKey: id) + state.trimBuffer() + if let limit, state.buffer.count < limit { + defer { state.limit = nil } + if case .cancelled = state.iteratingTask { + return (side, state.limit, true) + } else { + return (side, state.limit, false) + } + } else { + if case .cancelled = state.iteratingTask { + return (side, nil, true) + } else { + return (side, nil, false) + } + } + } + if let continuation { + continuation.resume(returning: cancelled) + } + if let side { + side.continuaton?.resume(returning: .success(nil)) + } + } + + func iterate() async -> Bool { + if let limit { + let cancelled = await withCheckedContinuation { (continuation: CheckedContinuation) in + let (resume, cancelled) = state.withLock { state -> (CheckedContinuation?, Bool) in + if state.buffer.count >= limit { + state.limit = continuation + if case .cancelled = state.iteratingTask { + return (nil, true) + } else { + return (nil, false) + } + } else { + assert(state.limit == nil) + if case .cancelled = state.iteratingTask { + return (continuation, true) + } else { + return (continuation, false) + } + } + } + if let resume { + resume.resume(returning: cancelled) + } + } + if cancelled { + return false + } + } + + // await a demand + await withCheckedContinuation { (continuation: CheckedContinuation) in + let hasPendingDemand = state.withLock { state in + for (_, side) in state.sides { + if side.continuaton != nil { + return true + } + } + state.demand = continuation + return false + } + if hasPendingDemand { + continuation.resume() + } + } + return state.withLock { state in + switch state.iteratingTask { + case .cancelled: + return false + default: + return true + } + } + } + + func cancel(id: Int) { + unregisterSide(id) // doubly unregistering is idempotent but has a side effect of emitting nil if present + } + + struct Resumption { + let continuation: CheckedContinuation, Never> + let result: Result + + func resume() { + continuation.resume(returning: result) + } + } + + func emit(_ result: Result) { + let (resumptions, demandContinuation) = state.withLock { state -> ([Resumption], CheckedContinuation?) in + var resumptions = [Resumption]() + switch result { + case .success(let element): + if let element { + state.enqueue(element) + } else { + state.finished = true + } + case .failure(let failure): + state.finished = true + state.failure = failure + } + for (id, side) in state.sides { + if let continuation = side.continuaton { + if side.position < state.buffer.count { + resumptions.append(Resumption(continuation: continuation, result: .success(state.buffer[side.position]))) + state.sides[id]?.position += 1 + state.sides[id]?.continuaton = nil + } else if state.finished { + state.sides[id]?.continuaton = nil + if let failure = state.failure { + resumptions.append(Resumption(continuation: continuation, result: .failure(failure))) + } else { + resumptions.append(Resumption(continuation: continuation, result: .success(nil))) + } + } + } + } + state.trimBuffer() + if let limit, state.buffer.count < limit { + defer { + state.demand = nil + } + return (resumptions, state.demand) + } else { + return (resumptions, nil) + } + } + if let demandContinuation { + demandContinuation.resume() + } + for resumption in resumptions { + resumption.resume() + } + } + + func next(isolation actor: isolated (any Actor)?, id: Int) async throws(Failure) -> Element? { + let (base, cancelled) = state.withLock { state -> (Base?, Bool) in + switch state.iteratingTask { + case .pending(let base): + state.iteratingTask = .starting + return (base, false) + case .cancelled: + return (nil, true) + default: + return (nil, false) + } + } + if cancelled { return nil } + if let base { + nonisolated(unsafe) let transfer = base.makeAsyncIterator() + let task = Task.detached { [transfer, self] in + var iterator = transfer + do { + while await iterate() { + if let element = try await iterator.next() { + emit(.success(element)) + } else { + emit(.success(nil)) + } + } + } catch { + emit(.failure(error as! Failure)) + } + } + state.withLock { state in + precondition(state.iteratingTask.isStarting) + state.iteratingTask = .running(task) + } + } + let result: Result = await withTaskCancellationHandler { + await withCheckedContinuation { continuation in + let (res, limitContinuation, demandContinuation, cancelled) = state.withLock { state -> (Result?, CheckedContinuation?, CheckedContinuation?, Bool) in + let side = state.sides[id]! + if side.position < state.buffer.count { + // There's an element available at this position + let element = state.buffer[side.position] + state.sides[id]?.position += 1 + state.trimBuffer() + return state.emit(.success(element)) + } else { + // Position is beyond the buffer + if let failure = state.failure { + return state.emit(.failure(failure)) + } else if state.finished { + return state.emit(.success(nil)) + } else { + state.sides[id]?.continuaton = continuation + return state.emit(nil) + } + } + } + if let limitContinuation { + limitContinuation.resume(returning: cancelled) + } + if let demandContinuation { + demandContinuation.resume() + } + if let res { + continuation.resume(returning: res) + } + } + } onCancel: { + cancel(id: id) + } + + return try result.get() + } + } + + final class Extent: Sendable { + let iteration: Iteration + + init(_ base: Base, bufferingPolicy: AsyncBufferSequencePolicy) { + iteration = Iteration(base, bufferingPolicy: bufferingPolicy) + } + + deinit { + iteration.cancel() + } + } + + let extent: Extent + + init(_ base: Base, bufferingPolicy: AsyncBufferSequencePolicy) { + extent = Extent(base, bufferingPolicy: bufferingPolicy) + } +} + +@available(macOS 26.0, *) +extension AsyncShareSequence: AsyncSequence { + typealias Element = Base.Element + typealias Failure = Base.Failure + + struct Iterator: AsyncIteratorProtocol { + + + let side: Side + + init(_ iteration: Iteration) { + side = Side(iteration) + } + + mutating func next(isolation actor: isolated (any Actor)?) async throws(Failure) -> Element? { + try await side.next(isolation: actor) + } + } + + func makeAsyncIterator() -> Iterator { + Iterator(extent.iteration) + } +} From c8de4d05b2a86427db586df4709efea1a9c48bd0 Mon Sep 17 00:00:00 2001 From: Philippe Hausler Date: Thu, 31 Jul 2025 14:22:57 -0700 Subject: [PATCH 3/4] Fix the remaining todo on hard cancellation vs soft cancellation --- .../AsyncAlgorithms/AsyncShareSequence.swift | 40 ++++++++++++++----- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/Sources/AsyncAlgorithms/AsyncShareSequence.swift b/Sources/AsyncAlgorithms/AsyncShareSequence.swift index f893db75..36dc702d 100644 --- a/Sources/AsyncAlgorithms/AsyncShareSequence.swift +++ b/Sources/AsyncAlgorithms/AsyncShareSequence.swift @@ -75,6 +75,7 @@ struct AsyncShareSequence: Sendable where Base.Element: Sen var buffer = [Element]() var finished = false var failure: Failure? + var cancelled = false var limit: CheckedContinuation? var demand: CheckedContinuation? @@ -155,14 +156,17 @@ struct AsyncShareSequence: Sendable where Base.Element: Sen } func cancel() { - // TODO: this currently is a hard cancel, it should be refined to only cancel when everything is terminal let (task, limit, demand, cancelled) = state.withLock { state -> (IteratingTask?, CheckedContinuation?, CheckedContinuation?, Bool) in - defer { - state.iteratingTask = .cancelled - state.limit = nil - state.demand = nil + if state.sides.count == 0 { + defer { + state.iteratingTask = .cancelled + state.cancelled = true + } + return state.emit(state.iteratingTask) + } else { + state.cancelled = true + return state.emit(nil) } - return state.emit(state.iteratingTask) } task?.cancel() limit?.resume(returning: cancelled) @@ -178,21 +182,32 @@ struct AsyncShareSequence: Sendable where Base.Element: Sen } func unregisterSide(_ id: Int) { - let (side, continuation, cancelled) = state.withLock { state -> (Side.State?, CheckedContinuation?, Bool) in + let (side, continuation, cancelled, iteratingTaskToCancel) = state.withLock { state -> (Side.State?, CheckedContinuation?, Bool, IteratingTask?) in let side = state.sides.removeValue(forKey: id) state.trimBuffer() + let cancelRequested = state.sides.count == 0 && state.cancelled if let limit, state.buffer.count < limit { defer { state.limit = nil } if case .cancelled = state.iteratingTask { - return (side, state.limit, true) + return (side, state.limit, true, nil) } else { - return (side, state.limit, false) + defer { + if cancelRequested { + state.iteratingTask = .cancelled + } + } + return (side, state.limit, false, cancelRequested ? state.iteratingTask : nil) } } else { if case .cancelled = state.iteratingTask { - return (side, nil, true) + return (side, nil, true, nil) } else { - return (side, nil, false) + defer { + if cancelRequested { + state.iteratingTask = .cancelled + } + } + return (side, nil, false, cancelRequested ? state.iteratingTask : nil) } } } @@ -202,6 +217,9 @@ struct AsyncShareSequence: Sendable where Base.Element: Sen if let side { side.continuaton?.resume(returning: .success(nil)) } + if let iteratingTaskToCancel { + iteratingTaskToCancel.cancel() + } } func iterate() async -> Bool { From 954eb9951c4b1d4f2e6e2fae2630e8752e6e03c9 Mon Sep 17 00:00:00 2001 From: Philippe Hausler Date: Sat, 2 Aug 2025 20:06:07 -0700 Subject: [PATCH 4/4] Update Sources/AsyncAlgorithms/AsyncShareSequence.swift Co-authored-by: Jamie <2119834+jamieQ@users.noreply.github.com> --- Sources/AsyncAlgorithms/AsyncShareSequence.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/AsyncAlgorithms/AsyncShareSequence.swift b/Sources/AsyncAlgorithms/AsyncShareSequence.swift index 36dc702d..a7baf1a0 100644 --- a/Sources/AsyncAlgorithms/AsyncShareSequence.swift +++ b/Sources/AsyncAlgorithms/AsyncShareSequence.swift @@ -12,7 +12,7 @@ extension AsyncSequence where Element: Sendable { struct AsyncShareSequence: Sendable where Base.Element: Sendable { final class Side { struct State { - var continuaton: CheckedContinuation, Never>? + var continuation: CheckedContinuation, Never>? var position = 0 func offset(_ adjustment: Int) -> State {