Repository navigation
fix(http): release response bodies a download stops reading - #639
Merged
Merged
Conversation
Every pause of a plain-HTTP download leaked about 2 MB per connection for as long as Ketch ran. A `ketch server` soak with pauses, lane takeovers and connection changes reached a 1.9 GB live heap with 934 tasks, nearly all of it kotlinx.io segments. bodyAsChannel() has Ktor's default transformer copy the raw response into a new channel, in a coroutine of the client's job rather than the response's. When a download stops reading (a pause, a lane another connection took, a failure) while that copy waits for room, Ktor's cleanup cancels the response and its raw body but never that channel, so the copy waits forever with its buffer. KtorHttpEngine now cancels the channel it reads when it stops early, in transfers and in request()'s bodies. Six downloads paused three times each grew the live heap by 184 MB before, 5 MB after.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Code reviewNo issues found. Checked for bugs and CLAUDE.md compliance. |
…-release-abandoned-bodies # Conflicts: # AGENTS.md
…-release-abandoned-bodies # Conflicts: # library/ktor/src/commonMain/kotlin/com/linroid/ketch/engine/KtorHttpEngine.kt
Contributor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Each pause of a plain-HTTP download leaked about 2 MB per connection for the life of the process. Lane takeovers, resegmenting and failures that stop a transfer mid-body leak the same way.
I found this during a 20-minute
ketch serversoak with random pauses, resumes, connection and speed-limit changes. After it the live heap was 1.9 GB after a full GC, with 934 tasks. Nearly all of it waskotlinx.io.Segmentbyte arrays, and removing every task freed none of it.A JFR old-object sample traced them to roots:
Here's what happens:
bodyAsChannel()makes Ktor'sDefaultTransformstart awritercoroutine that copies the raw response into a new channel. That coroutine runs under the client's job, not the response's.Fix
KtorHttpEnginecancels the channel it reads, with the cause, whenever it stops early. This covers transfers andrequest()'s bodies, so the copier is released. Bodies the engine never takes (HEAD, probes, redirects, error statuses) are already released by Ktor's own cleanup.Six downloads paused three times each, against current
main:Tests
KtorHttpEngineReleaseTest(jvmTest):Ketchwith the CIO client configured asJvmTransportsdoes, given to the engine as-is so its job can be inspected.ktorJVM tests pass; iOS compiles.