Skip the write in DuplexAsyncChannel.Writer.finish when the buffer is empty #476
All Vouch review confidence checks passed
Residual risk 0.23 = Change Risk (0.53) × (1 − Review Confidence (0.57)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.53 · 502 lines diff across 6 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.372
Shallow Review Depth
shallow_review_depth
0.289
Ai Authored
ai_authored
0.050
Real PR Conversation Timeline
1 comments
"Review active on #476"
general
weight: 0.35
FranzBusch
This fixes #470. A second
try await finish()on a clonedDuplexAsyncChannel.WriterthrewMultiProducerSingleConsumerAsyncChannelAlreadyFinishedError, becausefinish(buffer:finalElement:)always wrote its buffer first, even an empty one. This is fix B from the issue, plus regression tests, soclonedWriterCanProduceConcurrentlyshould pass again on the CI jobs that get far enough to run it.<details><summary>Change, tests, and what to scrutinise (Claude Opus 5.5)</summary>
Change.
Writer.finish(buffer:finalElement:)now callswrite(buffer:)only when the buffer has elements (DuplexAsyncChannel.swift:389):try await writer.finish()resolves to theCallerAsyncWriterprotocol extension, not the Writer's own syncfinish(), and that extension callsfinish(buffer:finalElement:)with an empty buffer. Once another writer of the direction had finished, the empty write threw. Finishing an already finished channel is already a no-op in the state machine, so skipping the empty write is enough. #470 has the full analysis.Tests. The existing
clonedWriterCanProduceConcurrentlycovers a directtry await clone.finish(). This adds three tests inDuplexAsyncChannelTests:cloneFinishesThroughGenericCallerAsyncWriterAfterOtherWriterFinished: finishes the clone through a genericCallerAsyncWriterfunction, where there's no concretefinish()overload, and checks the reader still gets every element and the final element.cloneFinishesThroughAsyncWriterAdapterAfterOtherWriterFinished: finishes the clone throughasAsyncWriter().finish().cloneFinishWithElementsAfterOtherWriterFinishedStillThrows: finishing a clone with elements after the channel finished still throws, so those elements are never dropped silently.Without the fix, the first two fail with the same
MultiProducerSingleConsumerAsyncChannelAlreadyFinishedErrorasclonedWriterCanProduceConcurrently; the third passes either way, since it pins behaviour the fix keeps.Results (macOS only: Apple Swift 6.4.2 from Xcode 27, macOS 27.2 SDK, swift-collections 1.7.6):
swift test --traits UnstableAsyncStreaming --filter AsyncStreamingTests: 94/94 (91 on main, withclonedWriterCanProduceConcurrentlyfailing).DuplexAsyncChannelTests, new tests included, run 20 times in a row: 20/20.swift test: 361/361.swift format lint --stricton the changed test file: clean.What to scrutinise.
finish(finalElement:). It doesn't match theclone()doc comment ("the direction stays open until every clone has finished or been dropped"), which would need source counting in the state machine. This PR leaves that doc comment alone; happy to change it or the behaviour, whichever you intend.finishno longer goes throughsend, so a clone'stry await finish()no longer suspends while the direction is above its high watermark. If another producer is suspended on backpressure when a finish happens, it still resumes withAlreadyFinishedErroreven though its elements were delivered. That's unchanged by this PR, but it's easier to hit now thatfinish()doesn't wait.AsyncStreamingTests(swiftlang/swift#89639), before any test runs. The new tests name every closure parameter, which avoids that crash's known trigger, but I haven't run them under the verifier, or on Linux or Windows.</details>
Resolves #470.
🤖 Generated with Claude Code