From 963eecf5ee65da8baf9ca30322d0e39f48312f33 Mon Sep 17 00:00:00 2001 From: Joseph Cursio Date: Mon, 23 Nov 2026 05:46:09 -0600 Subject: [PATCH] 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 * 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 --------- Co-authored-by: Claude Opus 5.5 --- .../DuplexChannel/DuplexAsyncChannel.swift | 4 +- .../DuplexAsyncChannelTests.swift | 87 +++++++++++++++++++ 2 files changed, 90 insertions(+), 1 deletion(-) diff --git a/Sources/AsyncStreaming/DuplexChannel/DuplexAsyncChannel.swift b/Sources/AsyncStreaming/DuplexChannel/DuplexAsyncChannel.swift index cccceeb..bc7fb47 100644 --- a/Sources/AsyncStreaming/DuplexChannel/DuplexAsyncChannel.swift +++ b/Sources/AsyncStreaming/DuplexChannel/DuplexAsyncChannel.swift @@ -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) } } diff --git a/Tests/AsyncStreamingTests/DuplexChannel/DuplexAsyncChannelTests.swift b/Tests/AsyncStreamingTests/DuplexChannel/DuplexAsyncChannelTests.swift index 9da5143..90f83aa 100644 --- a/Tests/AsyncStreamingTests/DuplexChannel/DuplexAsyncChannelTests.swift +++ b/Tests/AsyncStreamingTests/DuplexChannel/DuplexAsyncChannelTests.swift @@ -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: 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.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(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.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.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(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, *)