← Back to Pull requests

Skip the write in DuplexAsyncChannel.Writer.finish when the buffer is empty #476

Closed
unknown closed this pull request without merging on Nov 23, 2026 11:46 UTC
View on GitHub
unknown unknown commented on Nov 19, 2026 23:01 UTC
Author

This fixes #470. A second try await finish() on a cloned DuplexAsyncChannel.Writer threw MultiProducerSingleConsumerAsyncChannelAlreadyFinishedError, because finish(buffer:finalElement:) always wrote its buffer first, even an empty one. This is fix B from the issue, plus regression tests, so clonedWriterCanProduceConcurrently should 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 calls write(buffer:) only when the buffer has elements (DuplexAsyncChannel.swift:389):

if !buffer.isEmpty {
  try await self.write(buffer: &buffer)
}
self._storage.finish(throwing: nil, finalElement: finalElement)

try await writer.finish() resolves to the CallerAsyncWriter protocol extension, not the Writer's own sync finish(), and that extension calls finish(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 clonedWriterCanProduceConcurrently covers a direct try await clone.finish(). This adds three tests in DuplexAsyncChannelTests:

  • cloneFinishesThroughGenericCallerAsyncWriterAfterOtherWriterFinished: finishes the clone through a generic CallerAsyncWriter function, where there's no concrete finish() overload, and checks the reader still gets every element and the final element.

  • cloneFinishesThroughAsyncWriterAdapterAfterOtherWriterFinished: finishes the clone through asAsyncWriter().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 MultiProducerSingleConsumerAsyncChannelAlreadyFinishedError as clonedWriterCanProduceConcurrently; 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, with clonedWriterCanProduceConcurrently failing).

  • DuplexAsyncChannelTests, new tests included, run 20 times in a row: 20/20.

  • swift test: 361/361.

  • swift format lint --strict on the changed test file: clean.
  • What to scrutinise.

  • Semantics. With this change, the first finish closes the direction and later finishes with an empty buffer are no-ops; a later finish with elements throws. That matches the test's comment ("Either writer can close the direction independently") and the Writer's sync finish(finalElement:). It doesn't match the clone() 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.

  • Backpressure. An empty finish no longer goes through send, so a clone's try 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 with AlreadyFinishedError even though its elements were delivered. That's unchanged by this PR, but it's easier to hit now that finish() doesn't wait.

  • CI. The Windows 6.4 and Linux/Windows nightly-6.4.x jobs will stay red: they crash in the SIL ownership verifier while building 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

    Diff: 502 lines (+376 / -126) across 6 files Reviewer: FranzBusch Review duration: 5084m
    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
    0.53
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    56%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 56.8% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52.5% (MEDIUM) · Higher = larger blast radius
    52.5%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 56.8% · Lower = rushed or shallow review
    56.8%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 22.7% · Safe to merge
    22.7%