Skip to content

fix(http): release response bodies a download stops reading - #639

Merged
linroid merged 3 commits into
mainfrom
fix/http-release-abandoned-bodies
Oct 23, 2026
Merged

linroid merged 3 commits into
mainfrom
fix/http-release-abandoned-bodies

Conversation

@linroid

@linroid linroid commented Oct 23, 2026

Copy link
Copy Markdown
Owner

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 server soak 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 was kotlinx.io.Segment byte arrays, and removing every task freed none of it.

A JFR old-object sample traced them to roots:

byte[8192] <- kotlinx.io.Segment ... <- Buffer <- ByteChannel.flushBuffer
  <- ByteChannelUtils lambda <- ByteChannel.closeHandler <- writer completion handler
  <- StandaloneCoroutine <- ChildHandleNode ... <- HttpClient.clientJob
  <- ClientCache <- JvmTransports <- KtorHttpEngine

Here's what happens:

  1. bodyAsChannel() makes Ktor's DefaultTransform start a writer coroutine that copies the raw response into a new channel. That coroutine runs under the client's job, not the response's.
  2. A download stops reading while the copy waits for room in that channel, which happens easily under a speed limit.
  3. Ktor's cleanup cancels the response job and the raw body. It never cancels the channel we were reading.
  4. So the copier stays suspended forever, holding its buffered data.

Fix

KtorHttpEngine cancels the channel it reads, with the cause, whenever it stops early. This covers transfers and request()'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:

live heap growth after GC
before +184 MB
after +5 MB, then +1 MB for 8 more downloads paused 4 times each

Tests

  • KtorHttpEngineReleaseTest (jvmTest):
    • A Ketch with the CIO client configured as JvmTransports does, given to the engine as-is so its job can be inspected.
    • A 4-connection, 4 MB/s download paused three times while bodies are still arriving.
    • No coroutine should be left under the client's job; there were 12 without the fix.
  • ktor JVM tests pass; iOS compiles.

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.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-23T20:19:07.930352Z 116c1af PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@claude

claude Bot commented Oct 23, 2026

Copy link
Copy Markdown

Code review

No issues found. Checked for bugs and CLAUDE.md compliance.

@github-actions

Copy link
Copy Markdown
Contributor

Test Results

 2 208 files  +1   2 236 suites  +1   56m 50s ⏱️ -31s
15 430 tests +1  15 162 ✅ +1  268 💤 ±0  0 ❌ ±0 
18 797 runs  +1  18 529 ✅ +1  268 💤 ±0  0 ❌ ±0 

Results for commit 208cbd5. ± Comparison against base commit bafe7ff.

@linroid
linroid merged commit 3539d4a into main Oct 23, 2026
14 checks passed
@linroid
linroid deleted the fix/http-release-abandoned-bodies branch October 23, 2026 21:07
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.

1 participant