Skip to content

respondWrite-Writer is never closed, even if client closes connection #301

Closed
@trathschlag

Description

If I run this application:

fun main(args: Array<String>) {
    val server = embeddedServer(Netty, 8080) {
        routing {
            get("data") {
                call.respondWrite(ContentType("application", "text/csv")) {
                    while (true) {
                        this.write("some,entry\n")
                        delay(1000)
                        println("still computing")
                        this.flush()
                    }
                }
            }
        }
    }
    server.start(wait = true)
}

and then do a curl and exit at some point:

$ curl "localhost:8080/data"
some,entry
some,entry
some,entry
some,entry
^C
$

The server outputs:

still computing
still computing
still computing
still computing
still computing
11:22:32.672 [nettyWorkerPool-3-1] WARN io.netty.channel.DefaultChannelPipeline - An exceptionCaught() event was fired, and it reached at the tail of the pipeline. It usually means the last handler in the pipeline did not handle the exception.
java.nio.channels.ClosedChannelException: null
	at io.netty.channel.AbstractChannel$AbstractUnsafe.write(...)(Unknown Source)
still computing
still computing
still computing
still computing
still computing
still computing
...

And still computing keeps being printed.

I see no way to react to a closed connection. In my real example, I even have an expensive computation running, which I want to abort, as soon as the client closes the connection. By the way: If I change the loop to:

while (true) {
    this.write("some,entry\n")
    println("still computing")
    this.flush()
}

The still computing messages stop immediately if I close the connection on the client side.

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions