Skip to content

Fix AsyncBufferSequence leaking base sequence #352

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 37 additions & 11 deletions Sources/AsyncAlgorithms/Buffer/AsyncBufferSequence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,45 @@ public struct AsyncBufferSequence<Base: AsyncSequence & Sendable>: AsyncSequence
}

public struct Iterator: AsyncIteratorProtocol {
var storageType: StorageType
final class InternalClass {
private var storageType: StorageType

public mutating func next() async rethrows -> Element? {
switch self.storageType {
case .transparent(var iterator):
let element = try await iterator.next()
self.storageType = .transparent(iterator)
return element
case .bounded(let storage):
return try await storage.next()?._rethrowGet()
case .unbounded(let storage):
return try await storage.next()?._rethrowGet()
fileprivate init(storageType: StorageType) {
self.storageType = storageType
}

deinit {
switch self.storageType {
case .transparent: break
case .bounded(let storage):
storage.iteratorDeinitialized()
case .unbounded(let storage):
storage.iteratorDeinitialized()
}
}

public func next() async rethrows -> Element? {
switch self.storageType {
case .transparent(var iterator):
let element = try await iterator.next()
self.storageType = .transparent(iterator)
return element
case .bounded(let storage):
return try await storage.next()?._rethrowGet()
case .unbounded(let storage):
return try await storage.next()?._rethrowGet()
}
}
}

let internalClass: InternalClass

fileprivate init(storageType: StorageType) {
internalClass = InternalClass(storageType: storageType)
}

public mutating func next() async rethrows -> Element? {
try await internalClass.next()
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/AsyncAlgorithms/Buffer/BoundedBufferStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
//===----------------------------------------------------------------------===//

@available(AsyncAlgorithms 1.0, *)
final class BoundedBufferStorage<Base: AsyncSequence>: Sendable where Base: Sendable {
struct BoundedBufferStorage<Base: AsyncSequence>: Sendable where Base: Sendable {
private let stateMachine: ManagedCriticalState<BoundedBufferStateMachine<Base>>

init(base: Base, limit: Int) {
Expand Down Expand Up @@ -152,7 +152,7 @@ final class BoundedBufferStorage<Base: AsyncSequence>: Sendable where Base: Send
}
}

deinit {
func iteratorDeinitialized() {
self.interrupted()
}
}
4 changes: 2 additions & 2 deletions Sources/AsyncAlgorithms/Buffer/UnboundedBufferStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
//===----------------------------------------------------------------------===//

@available(AsyncAlgorithms 1.0, *)
final class UnboundedBufferStorage<Base: AsyncSequence>: Sendable where Base: Sendable {
struct UnboundedBufferStorage<Base: AsyncSequence>: Sendable where Base: Sendable {
private let stateMachine: ManagedCriticalState<UnboundedBufferStateMachine<Base>>

init(base: Base, policy: UnboundedBufferStateMachine<Base>.Policy) {
Expand Down Expand Up @@ -120,7 +120,7 @@ final class UnboundedBufferStorage<Base: AsyncSequence>: Sendable where Base: Se
}
}

deinit {
func iteratorDeinitialized() {
self.interrupted()
}
}