Large responses are not completely sent with 'Connection: close'
See original GitHub issueWhen 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:
- Created 5 years ago
- Comments:5 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 headerConnection: close
. We will mostly likely be adding this in the nginx config as a workaround: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:
The nginx docs have this to say:
Same problem on
io.ktor.server.tomcat
and maybe other: