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:
Joseph CursioandClaude Opus 5.5 authored and GitHub committed 2026-11-23 12:46:09 +01:00
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)
}
}