Files
swift-async-algorithms/Sources
Joseph CursioandClaude Opus 5.5 963eecf5ee 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>
2026-11-23 12:46:09 +01:00
..