question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[ktor-server-netty] Unable to send responses larger than ~2GB

See original GitHub issue

We are unable to send responses larger than 2GB which include the Content-Length header. The issue would appear to be here: https://github.com/ktorio/ktor/blob/e2e622c90f9beba9248249714ec58c0d33e7cfd7/ktor-server/ktor-server-netty/src/io/ktor/server/netty/cio/NettyResponsePipeline.kt#L191

This is also mentioned in the stacktrace we get. We are running version ktor 0.9.2 with ktor-server-netty.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
DanThranecommented, Jun 15, 2018

Sure. The response body is streamed directly from an external system, hence our need for writing directly to the ByteWriteChannel. The code works, even for files larger than Int.MAX_VALUE, as long as we don’t send the Content-Length. For files smaller than Int.MAX_VALUE we can send the Content-Length.

get("foo") {
    val size: Long = // ...
    val contentType: ContentType = // ...
    call.respondDirectWrite(size, contentType, HttpStatusCode.OK) {
        // Write something
    }
}

suspend fun ApplicationCall.respondDirectWrite(
    size: Long? = null,
    contentType: ContentType? = null,
    status: HttpStatusCode? = null,
    writer: suspend ByteWriteChannel.() -> Unit
) {
    val message = DirectWriteContent(writer, size, contentType, status)
    return respond(message)
}

class DirectWriteContent(
    private val writer: suspend ByteWriteChannel.() -> Unit,
    override val contentLength: Long? = null,
    override val contentType: ContentType? = null,
    override val status: HttpStatusCode? = null
) : OutgoingContent.WriteChannelContent() {
    override suspend fun writeTo(channel: ByteWriteChannel) {
        writer(channel)
    }
}
0reactions
e5lcommented, Aug 14, 2018

Awesome. Please reopen this if it happens again.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't upload file larger than 2GB to webdav by requests #1784
Dear Sir, I can't upload file larger than 2GB to webdav server by requests, and the following is the error message.
Read more >
Project 'fatjar' not found in root project - Stack Overflow
on IntelliJ's terminal just like it says I get the following error: FAILURE: Build failed with an exception. What went wrong: Project 'fatjar' ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found