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.

Large responses are not completely sent with 'Connection: close'

See original GitHub issue

When a ktor server sends a large amount of data over the network and the client specified Connection: close, the transfer will not complete.

curl http://poseidon:2000/ -H 'Connection: close' > /dev/null
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  8 1000k    8 85432    0     0  3776k      0 --:--:-- --:--:-- --:--:-- 3792k
curl: (18) transfer closed with 938568 bytes remaining to read

This doesn’t happen on localhost connections, only on those going over at least a LAN (maybe VMs too).

The following code in combination with the curl command reproduces the problem:

import io.ktor.application.call
import io.ktor.response.respond
import io.ktor.routing.get
import io.ktor.routing.routing
import io.ktor.server.engine.embeddedServer
import io.ktor.server.netty.Netty
import java.io.File

fun main(args: Array<String>) {
  val server = embeddedServer(Netty, 2000) {
    routing {
      get("/") {
        val data = ByteArray(1024 * 1024)
        File("/dev/urandom").inputStream().read(data)
        call.respond(data)
      }
    }
  }
  server.start()
}

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
jonstackscommented, Aug 29, 2018

We just hit this as well in production when trying to transfer a payload of about 1M. We are using nginx with the proxy_pass directive and by default it sets the header Connection: close. We will mostly likely be adding this in the nginx config as a workaround:

proxy_set_header Connection "keep-alive";

We ended up trying things like turning off proxy_buffering and that seemed to work as well. Hope this helps someone else.

Edit:

We ended up using this in our config:

proxy_http_version 1.1;
proxy_set_header Connection "";

The nginx docs have this to say:

Alternatively, HTTP/1.0 persistent connections can be used by passing the “Connection: Keep-Alive” header field to an upstream server, though this method is not recommended.

0reactions
rvadimcommented, Nov 22, 2018

Same problem on io.ktor.server.tomcat and maybe other:

~$ curl -H "Connection: close" localhost:8080 -v
* Rebuilt URL to: localhost:8080/
*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8080 (#0)
> GET / HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.52.1
> Accept: */*
> Connection: close
> 
< HTTP/1.1 200 
< Transfer-Encoding: chunked
< Connection: close
< Content-Type: text/html;charset=UTF-8
< Date: Thu, 22 Nov 2018 03:11:58 GMT
< 
* Illegal or missing hexadecimal sequence in chunked-encoding
* Curl_http_done: called premature == 1
* stopped the pause stream!
* Closing connection 0
curl: (56) Illegal or missing hexadecimal sequence in chunked-encoding
Read more comments on GitHub >

github_iconTop Results From Across the Web

4.7. The Mysteries of Connection Close - HTTP - O'Reilly
Connection Close Tolerance, Retries, and Idempotency​​ The situation is worse for pipelined connections. The client can enqueue a large number of requests, but ......
Read more >
What does "Connection: close" mean when used in the ...
I think that it means that the server will close the connection after sending the response the message (even if the client has...
Read more >
Jetty Close connection before Client receives full response ...
Client send "Connection:close" header;; Large response payload (~ few Mb);. It was working fine with Jetty 9.2. Was it changed on purpose? Or ......
Read more >
HTTP/1.1: Connections
If the server chooses to close the connection immediately after sending the response, it SHOULD send a Connection header including the connection-token close....
Read more >
Why Fiddler adds connection:close on CONNECT response?
Any Connection header in the successful response to the CONNECT request does not make any sense and gets ignored. CONNECT will establish a...
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