Skip to content

KTOR-9561 Rethrow closedCause after copyTo#5634

Open
fru1tworld (fru1tworld) wants to merge 2 commits into
ktorio:mainfrom
fru1tworld:fix/9561-copyto-closed-cause
Open

KTOR-9561 Rethrow closedCause after copyTo#5634
fru1tworld (fru1tworld) wants to merge 2 commits into
ktorio:mainfrom
fru1tworld:fix/9561-copyto-closed-cause

Conversation

@fru1tworld

Copy link
Copy Markdown
Contributor

Subsystem
ktor-io

Motivation
KTOR-9561 ByteReadChannel.copyTo does not propagate source closedCause on normal exit

Solution
Call rethrowCloseCauseIfNeeded() in both copyTo overloads, matching readTo/readRemaining.

@coderabbitai

coderabbitai Bot commented May 19, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 8b297964-e2e7-4a89-94c1-9248090ed530

📥 Commits

Reviewing files that changed from the base of the PR and between eaf1986 and c984ca4.

📒 Files selected for processing (2)
  • ktor-io/common/src/io/ktor/utils/io/ByteChannel.kt
  • ktor-io/common/test/ByteReadChannelOperationsTest.kt

📝 Walkthrough

Walkthrough

A new private helper rethrowCloseCauseIfNeeded() in ByteChannel throws the wrapped close cause when present. awaitContent(min) now calls this helper after suspension and before returning, ensuring cancellation is propagated. Four tests verify the new behavior: awaitContent rethrows after suspension, and copyTo (with and without limit) propagates cancellation from a cancelled source or completes normally when closed via flushAndClose.

Changes

Close-cause propagation in ByteChannel.awaitContent

Layer / File(s) Summary
Implement close-cause propagation in awaitContent
ktor-io/common/src/io/ktor/utils/io/ByteChannel.kt
New private helper rethrowCloseCauseIfNeeded() throws wrapped closedCause when present; awaitContent(min) invokes it after suspension loop to re-check and propagate close causes.
Test awaitContent and copyTo close-cause propagation
ktor-io/common/test/ByteReadChannelOperationsTest.kt
Four new tests verify awaitContent rethrows closedCause after suspension, copyTo propagates cancellation from cancelled sources (with and without limit), and copyTo completes normally on standard close via flushAndClose.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related PRs

  • ktorio/ktor#5528: Both PRs update awaitContent's handling of closed/cancellation state—the main PR rethrows closedCause after suspension in ByteChannel, while #5528 adjusts RawSourceChannel.awaitContent's closed fast-path behavior for consistent cancellation handling.
  • ktorio/ktor#5216: Both PRs address cancellation/closedCause propagation in ByteReadChannel data-transfer flows; #5216 adjusts readTo/copyTo bounded copy loops while the main PR changes awaitContent to rethrow wrapped closedCause after suspension, a behavior these transfer utilities rely on.
  • ktorio/ktor#5575: Both PRs address the same cancellation/closedCause propagation mechanism in ByteChannel/ByteReadChannel; the main PR adjusts awaitContent and adds tests for rethrowing cancellation causes, while #5575 introduces attachWriterJob/invokeOnClose wiring to propagate streaming cancellation through copyTo.

Suggested reviewers

  • e5l
  • bjhham
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The pull request title clearly and specifically summarizes the main change: rethrowing closedCause after copyTo, matching the primary objective to fix KTOR-9561.
Description check ✅ Passed The pull request description is complete and well-structured, including all required sections (Subsystem, Motivation with issue reference, and Solution) with clear and sufficient detail.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@bjhham Bruce Hamilton (bjhham) left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you try the fix proposed in the YT ticket:

rethrowCloseCauseIfNeeded() should be made private to ByteChannel and called at the end of awaitContent() (after sleepWhile exits), so that any cancellation cause is always propagated regardless of whether the caller was suspended or executing between iterations. This would make copyTo (and all other readers built on awaitContent()) correct by default.

This is a more central refactor than the immediate workaround and should be addressed in a dedicated change.

This would ensure all looped flows like this are covered.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants