Skip to content

KTOR-9483 Curl: Implement response body backpressure#5585

Open
osipxd wants to merge 2 commits into
mainfrom
osipxd/curl-freeze
Open

KTOR-9483 Curl: Implement response body backpressure#5585
osipxd wants to merge 2 commits into
mainfrom
osipxd/curl-freeze

Conversation

@osipxd
Copy link
Copy Markdown
Member

@osipxd osipxd commented May 7, 2026

Subsystem
ktor-client-curl

Motivation
KTOR-9483 Curl: backpressure implementation is never used
KTOR-9527 Curl: Freeze when receiving large responses

Solution
Replace runBlocking with non-blocking writes via writeBuffer/flushWriteBuffer. When the channel's flush buffer reaches CHANNEL_MAX_SIZE, return WRITEFUNC_PAUSE to pause the specific easy handle. A background coroutine then calls flush(), suspending until the consumer drains the buffer, after which curl_easy_pause(CURLPAUSE_CONT) resumes the transfer.

Also adds ByteChannel.flushNeeded (@InternalAPI) and a slow-consumer download test.

@osipxd osipxd self-assigned this May 7, 2026
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 7, 2026

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: a72bd530-d0c4-4e49-bb01-c060bf86df84

📥 Commits

Reviewing files that changed from the base of the PR and between d590118 and 65b0479.

📒 Files selected for processing (8)
  • ktor-client/ktor-client-curl/desktop/src/io/ktor/client/engine/curl/CurlProcessor.kt
  • ktor-client/ktor-client-curl/desktop/src/io/ktor/client/engine/curl/internal/CurlHttpResponseBody.kt
  • ktor-client/ktor-client-curl/desktop/src/io/ktor/client/engine/curl/internal/CurlMultiApiHandler.kt
  • ktor-client/ktor-client-tests/common/test/io/ktor/client/tests/DownloadTest.kt
  • ktor-client/ktor-client-tests/common/test/io/ktor/client/tests/ExceptionsTest.kt
  • ktor-io/api/ktor-io.api
  • ktor-io/api/ktor-io.klib.api
  • ktor-io/common/src/io/ktor/utils/io/ByteChannel.kt
✅ Files skipped from review due to trivial changes (1)
  • ktor-client/ktor-client-tests/common/test/io/ktor/client/tests/ExceptionsTest.kt
🚧 Files skipped from review as they are similar to previous changes (6)
  • ktor-io/api/ktor-io.klib.api
  • ktor-io/common/src/io/ktor/utils/io/ByteChannel.kt
  • ktor-client/ktor-client-tests/common/test/io/ktor/client/tests/DownloadTest.kt
  • ktor-io/api/ktor-io.api
  • ktor-client/ktor-client-curl/desktop/src/io/ktor/client/engine/curl/CurlProcessor.kt
  • ktor-client/ktor-client-curl/desktop/src/io/ktor/client/engine/curl/internal/CurlHttpResponseBody.kt

📝 Walkthrough

Walkthrough

Adds a new ByteChannel.hasFreeSpace API, refactors cURL response body streaming to use writeBuffer/flushWriteBuffer with coroutine pause/resume and normalized cancellation, updates tests for slow consumers and platform exclusions, and bumps a libcurl version comment.

Changes

Backpressure Handling and Cancellation in cURL Response Streaming

Layer / File(s) Summary
Public API
ktor-io/common/src/io/ktor/utils/io/ByteChannel.kt, ktor-io/api/ktor-io.api, ktor-io/api/ktor-io.klib.api
Adds hasFreeSpace: Boolean accessor on ByteChannel exposing buffer free-space state.
CurlHttpResponseBody — imports & scope
ktor-client/ktor-client-curl/desktop/src/io/ktor/client/engine/curl/internal/CurlHttpResponseBody.kt
Switch imports to ktor.utils.io.core.*, remove atomicfu/runBlocking usage, make the class a CoroutineScope with an internal Job and attachJob(job).
CurlHttpResponseBody — write & pause
ktor-client/ktor-client-curl/desktop/src/io/ktor/client/engine/curl/internal/CurlHttpResponseBody.kt
onBodyChunkReceived now writes via writeBuffer.writeFully() + flushWriteBuffer(), uses hasFreeSpace/paused to return WRITEFUNC_PAUSE and launches awaitFreeSpace() when needed.
CurlHttpResponseBody — close
.../CurlHttpResponseBody.kt
close closes the bodyChannel write side and cancels the internal job, normalizing the cancellation cause into a CancellationException.
CurlProcessor cancellation
ktor-client/ktor-client-curl/desktop/src/io/ktor/client/engine/curl/CurlProcessor.kt
runEventLoop().invokeOnCompletion preserves an existing CancellationException cause and only wraps non-cancellation throwables before calling curlScope.cancel(...).
Tests and platform validation
ktor-client/ktor-client-tests/common/test/io/ktor/client/tests/DownloadTest.kt, .../ExceptionsTest.kt
Adds testDownloadWithSlowConsumer (consumes 4 MiB with 1 ms delays) and removes Curl from an exclusions list in testErrorOnResponseCoroutine.
Interop metadata
ktor-client/ktor-client-curl/desktop/interop/libcurl.def
Update libcurl version comment from 8.10.1 to 8.18.0.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

  • ktorio/ktor#4886: Modifies CurlMultiApiHandler.perform (overlaps cURL perform/pause behavior changes).
  • ktorio/ktor#5187: Edits in CurlMultiApiHandler.perform and related curl engine handling.
  • ktorio/ktor#5469: Related adjustments to CurlProcessor and easy-handle lifecycle/cancellation.

Suggested reviewers

  • bjhham
  • e5l
🚥 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 title directly describes the main objective of the PR: implementing response body backpressure for the Curl HTTP client engine.
Description check ✅ Passed The description follows the required template with all three sections (Subsystem, Motivation, Solution) properly filled out with relevant details and issue references.
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 docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch osipxd/curl-freeze

Tip

💬 Introducing Slack Agent: The best way for teams to turn conversations into code.

Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.

  • Generate code and open pull requests
  • Plan features and break down work
  • Investigate incidents and troubleshoot customer tickets together
  • Automate recurring tasks and respond to alerts with triggers
  • Summarize progress and report instantly

Built for teams:

  • Shared memory across your entire org—no repeating context
  • Per-thread sandboxes to safely plan and execute work
  • Governance built-in—scoped access, auditability, and budget controls

One agent for your entire SDLC. Right inside Slack.

👉 Get started


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.

}

@Test
fun testDownloadWithSlowConsumer() = clientTests(timeout = 10.seconds) {
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This test doesn't check if back pressure is properly implemented, but at least checks that client doesn't fail in slow consumer scenario.

}
}

return WRITEFUNC_PAUSE
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It turned out that libcurl replays last chunk for which we returned WRITEFUNC_PAUSE, so we should return WRITEFUNC_PAUSE before reading data, not after it.

private var flushBufferSize = 0

@InternalAPI
public val flushNeeded: Boolean
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This is the main subject to discuss here. Is it okay to add this field? Are there any other ways to check that flush is needed, I'm not aware of?
Another possible name is hasFreeSpace (in line with awaitFreeSpace).

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.

I think flushNeeded is a little confusing and exposes some implementation details. I'd go with hasFreeSpace or isFull.

@osipxd osipxd force-pushed the osipxd/curl-freeze branch 3 times, most recently from b77a093 to dd6b99e Compare May 8, 2026 09:11
@osipxd osipxd marked this pull request as ready for review May 8, 2026 09:11
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: dd6b99e8d2

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +66 to 67
paused = false
onUnpause()
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Skip unpausing after response body has been closed

pauseUntilFreeSpaceAvailable() always calls onUnpause() in finally, even when the response is being closed/cancelled. In that path, close() can run curl_easy_cleanup for the same easy handle in CurlMultiApiHandler.cleanupEasyHandle, but the queued unpause is still processed later in perform() via curl_easy_pause(handle, CURLPAUSE_CONT). This creates a use-after-cleanup risk for slow-consumer or cancelled downloads where the waiter coroutine is cancelled during shutdown, and can crash or corrupt transfers intermittently.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
ktor-client/ktor-client-curl/desktop/src/io/ktor/client/engine/curl/internal/CurlHttpResponseBody.kt (1)

72-76: 💤 Low value

close() with null cause creates a redundant CancellationException wrapper.

When cause == null: null as? CancellationException yields null, then CancellationException(null) creates a wrapper with a null message. This is functionally equivalent to calling cancel() with no arguments, but adds an unnecessary allocation.

✏️ Proposed simplification
-        cancel(cause as? CancellationException ?: CancellationException(cause))
+        cancel(cause?.let { it as? CancellationException ?: CancellationException("${it.message}", it) })

Or even simpler — let Kotlin's default null handle the no-cause case:

 override fun close(cause: Throwable?) {
     if (bodyChannel.isClosedForWrite) return
     bodyChannel.close(cause)
-    cancel(cause as? CancellationException ?: CancellationException(cause))
+    val cancellationCause = cause as? CancellationException
+        ?: cause?.let { CancellationException(it.message, it) }
+    cancel(cancellationCause)
 }
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@ktor-client/ktor-client-curl/desktop/src/io/ktor/client/engine/curl/internal/CurlHttpResponseBody.kt`
around lines 72 - 76, The close(cause: Throwable?) implementation creates an
unnecessary CancellationException wrapper when cause is null; change the cancel
call to pass the original cause directly (i.e., call cancel(cause as?
CancellationException ?: cause)) or simply cancel(cause) so that a null cause is
preserved and no redundant CancellationException object is allocated; update the
method using the existing symbols close, bodyChannel, and cancel to pass the
original cause without wrapping.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In
`@ktor-client/ktor-client-tests/common/test/io/ktor/client/tests/DownloadTest.kt`:
- Around line 30-31: The test function name testDownloadWithSlowConsumer should
follow the project's convention of using backtick-quoted descriptive strings for
test names. Rename the function by replacing the camelCase function name with a
backtick-quoted descriptive string that clearly describes what the test does,
such as converting testDownloadWithSlowConsumer to use backticks with a more
natural language description of the test behavior.

---

Nitpick comments:
In
`@ktor-client/ktor-client-curl/desktop/src/io/ktor/client/engine/curl/internal/CurlHttpResponseBody.kt`:
- Around line 72-76: The close(cause: Throwable?) implementation creates an
unnecessary CancellationException wrapper when cause is null; change the cancel
call to pass the original cause directly (i.e., call cancel(cause as?
CancellationException ?: cause)) or simply cancel(cause) so that a null cause is
preserved and no redundant CancellationException object is allocated; update the
method using the existing symbols close, bodyChannel, and cancel to pass the
original cause without wrapping.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 96f0890f-7573-41dd-b98c-9e005a25ab76

📥 Commits

Reviewing files that changed from the base of the PR and between 6d662e1 and dd6b99e.

📒 Files selected for processing (8)
  • ktor-client/ktor-client-curl/desktop/interop/libcurl.def
  • ktor-client/ktor-client-curl/desktop/src/io/ktor/client/engine/curl/CurlProcessor.kt
  • ktor-client/ktor-client-curl/desktop/src/io/ktor/client/engine/curl/internal/CurlHttpResponseBody.kt
  • ktor-client/ktor-client-tests/common/test/io/ktor/client/tests/DownloadTest.kt
  • ktor-client/ktor-client-tests/common/test/io/ktor/client/tests/ExceptionsTest.kt
  • ktor-io/api/ktor-io.api
  • ktor-io/api/ktor-io.klib.api
  • ktor-io/common/src/io/ktor/utils/io/ByteChannel.kt

@osipxd osipxd force-pushed the osipxd/curl-freeze branch from dd6b99e to d590118 Compare May 11, 2026 17:16
@osipxd osipxd enabled auto-merge (squash) May 11, 2026 17:17
Replace runBlocking with non-blocking writes and implement proper backpressure:
when the channel's flush buffer reaches CHANNEL_MAX_SIZE (1 MB), return
WRITEFUNC_PAUSE to pause the specific easy handle. Once the consumer drains
enough data, resume via curl_easy_pause(CURLPAUSE_CONT), reusing the unpause
infrastructure already present for request bodies.

Add a slow-consumer download test to verify large responses complete correctly.

Also fixes KTOR-9527
@osipxd osipxd force-pushed the osipxd/curl-freeze branch from d590118 to 65b0479 Compare May 11, 2026 17:24
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