Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
trathschlag opened this issue Jan 29, 2018 · 3 comments
Closed
Assignees
Labels

Comments

@trathschlag
Copy link

trathschlag commented Jan 29, 2018

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.

@trathschlag
Copy link
Author

I did some further investigation and it seems to me that the exception is swallowed by kotlinx.coroutines:

https://github.com/Kotlin/kotlinx.coroutines/blob/985f56e54060d05387d25319e630f39e2cce5d5d/core/kotlinx-coroutines-io/src/main/kotlin/kotlinx/coroutines/experimental/io/jvm/javaio/Blocking.kt#L76

Shouldn't the exception be rethrown here?

@cy6erGn0m
Copy link
Contributor

Fixed in kotlinx.coroutines: Kotlin/kotlinx.coroutines@f0c1d99

Will be available once get released

@cy6erGn0m cy6erGn0m added the bug label Feb 2, 2018
@trathschlag
Copy link
Author

Thanks a lot for fixing!

@orangy orangy closed this as completed Feb 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants