mirror of
https://github.com/apple/swift-async-algorithms
synced 2026-11-24 04:17:52 +00:00
Skip the write in DuplexAsyncChannel.Writer.finish when the buffer is empty (#476)
* Skip the write in DuplexAsyncChannel.Writer.finish when the buffer is empty A cloned writer's finish(buffer:finalElement:) always called write(buffer:), even with an empty buffer. Once another clone had finished the channel, that write threw MultiProducerSingleConsumerAsyncChannelAlreadyFinishedError, so a second `try await finish()` failed although it had nothing to write. This is why clonedWriterCanProduceConcurrently has failed since #459. Only write when the buffer has elements. A clone that still has buffered elements after the channel finished keeps throwing. Resolves #470. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Test finishing a cloned writer through the protocol and the adapter clonedWriterCanProduceConcurrently only covers a direct `try await clone.finish()`. Also cover a generic CallerAsyncWriter and asAsyncWriter(), where there is no concrete finish() overload, after the other writer has finished. Both threw MultiProducerSingleConsumerAsyncChannelAlreadyFinishedError before the previous commit. A third test checks that finishing a clone with elements still throws once the channel has finished, so those elements are never dropped silently. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
088f92a30c
commit
963eecf5ee
2 files changed
+90
-1
No files matched your search
@@ -386,7 +386,9 @@ extension DuplexAsyncChannel {
|
||||
buffer: inout Buffer,
|
||||
finalElement: consuming FinalElement
|
||||
) async throws {
|
||||
try await self.write(buffer: &buffer)
|
||||
if !buffer.isEmpty {
|
||||
try await self.write(buffer: &buffer)
|
||||
}
|
||||
self._storage.finish(throwing: nil, finalElement: finalElement)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -387,6 +387,93 @@ struct DuplexAsyncChannelTests {
|
||||
}
|
||||
}
|
||||
|
||||
/// Finishes `writer` through the `CallerAsyncWriter` protocol, where there is
|
||||
/// no concrete `finish()` overload to pick instead.
|
||||
@available(macOS 27.0, iOS 27.0, watchOS 27.0, tvOS 27.0, visionOS 27.0, *)
|
||||
private static func finishThroughProtocol<Writer: CallerAsyncWriter & ~Copyable>(
|
||||
_ writer: consuming Writer
|
||||
) async throws(Writer.WriteFailure) where Writer.FinalElement == Void {
|
||||
try await writer.finish()
|
||||
}
|
||||
|
||||
@available(macOS 27.0, iOS 27.0, watchOS 27.0, tvOS 27.0, visionOS 27.0, *)
|
||||
@Test
|
||||
func cloneFinishesThroughGenericCallerAsyncWriterAfterOtherWriterFinished() async throws {
|
||||
try await DuplexAsyncChannel<Int, Void, Never>.withDuplex(
|
||||
of: Int.self,
|
||||
backpressureStrategy: .watermark(low: 5, high: 50)
|
||||
) { writerA, readerA, writerB, readerB in
|
||||
var writerA = writerA
|
||||
var readerB = readerB
|
||||
_ = readerA
|
||||
_ = writerB
|
||||
|
||||
let clone = writerA.clone()
|
||||
var buffer = UniqueArray<Int>(minimumCapacity: 3)
|
||||
for value in 0..<3 { buffer.append(value) }
|
||||
try await writerA.write(buffer: &buffer)
|
||||
|
||||
try await writerA.finish()
|
||||
try await Self.finishThroughProtocol(clone)
|
||||
|
||||
var collected: [Int] = []
|
||||
var done = false
|
||||
while !done {
|
||||
try await readerB.read { buffer, finalElement in
|
||||
var consumer = buffer.consumeAll()
|
||||
while let value = consumer.next() { collected.append(value) }
|
||||
if finalElement != nil { done = true }
|
||||
}
|
||||
}
|
||||
#expect(collected == [0, 1, 2])
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 27.0, iOS 27.0, watchOS 27.0, tvOS 27.0, visionOS 27.0, *)
|
||||
@Test
|
||||
func cloneFinishesThroughAsyncWriterAdapterAfterOtherWriterFinished() async throws {
|
||||
try await DuplexAsyncChannel<Int, Void, Never>.withDuplex(
|
||||
of: Int.self,
|
||||
backpressureStrategy: .watermark(low: 5, high: 50)
|
||||
) { writerA, readerA, writerB, readerB in
|
||||
var writerA = writerA
|
||||
_ = readerA
|
||||
_ = writerB
|
||||
_ = readerB
|
||||
|
||||
let clone = writerA.clone()
|
||||
try await writerA.finish()
|
||||
try await clone.asAsyncWriter().finish()
|
||||
}
|
||||
}
|
||||
|
||||
@available(macOS 27.0, iOS 27.0, watchOS 27.0, tvOS 27.0, visionOS 27.0, *)
|
||||
@Test
|
||||
func cloneFinishWithElementsAfterOtherWriterFinishedStillThrows() async throws {
|
||||
try await DuplexAsyncChannel<Int, Void, Never>.withDuplex(
|
||||
of: Int.self,
|
||||
backpressureStrategy: .watermark(low: 5, high: 50)
|
||||
) { writerA, readerA, writerB, readerB in
|
||||
var writerA = writerA
|
||||
_ = readerA
|
||||
_ = writerB
|
||||
_ = readerB
|
||||
|
||||
let clone = writerA.clone()
|
||||
try await writerA.finish()
|
||||
|
||||
// Elements that can no longer be delivered must not be dropped silently.
|
||||
var buffer = UniqueArray<Int>(minimumCapacity: 1)
|
||||
buffer.append(1)
|
||||
do {
|
||||
try await clone.finish(buffer: &buffer, finalElement: ())
|
||||
Issue.record("Expected error")
|
||||
} catch {
|
||||
#expect(error is MultiProducerSingleConsumerAsyncChannelAlreadyFinishedError)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Body-error wrapping
|
||||
|
||||
@available(macOS 27.0, iOS 27.0, watchOS 27.0, tvOS 27.0, visionOS 27.0, *)
|
||||
|
||||
Reference in new issue
Block a user