ktor icon indicating copy to clipboard operation
ktor copied to clipboard

Can't close WebSocketSession with Netty/Jetty engine

Open petersamokhin opened this issue 5 years ago • 2 comments

Ktor Version

1.1.3 & 1.1.4

Ktor Engine Used (client or server and name)

The issue is only about a server. Netty, Jetty, Tomcat, CIO.

JVM Version, Operating System, and Relevant Context

java version "1.8.0_162"
Java(TM) SE Runtime Environment (build 1.8.0_162-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.162-b12, mixed mode)
ProductName:    Mac OS X
ProductVersion: 10.14.4
BuildVersion:   18E226

Feedback

There's (maybe) a bug:

Steps to reproduce

The simple code below is enough to reproduce.

webSocket("/ws") {
    close()
    // or terminate()
    // or close(CloseReason(...))
    // or close(Exception())
}

Connection is not closing with Netty or Jetty engine, but closing with CIO or Tomcat.

petersamokhin avatar Apr 16 '19 15:04 petersamokhin

Hi @petersamokhin.

I wrote the following code to reproduce the issue:

fun main() {
    embeddedServer(Netty, port = 7654) {
        install(WebSockets)
        routing {
            webSocket("/ws") {
                closeReason.invokeOnCompletion { cause -> println("Closed: $cause") }
                close()
            }
        }
    }.start(true)
}

And the client:

suspend fun main() {
    HttpClient(CIO) {
        install(WebSockets)
    }.use { client ->
        client.ws(method = HttpMethod.Get, host = "localhost", port = 7654, path = "/ws") {
            closeReason.invokeOnCompletion { cause -> println("Close with cause: $cause")  }
            for (frame in incoming) {}
        }
    }
}

As far as I see the result server session is closed after close(). Could you verify these code samples or provide others?

dmitrievanthony avatar Mar 25 '20 11:03 dmitrievanthony

Please check the following ticket on YouTrack for follow-ups to this issue. GitHub issues will be closed in the coming weeks.

oleg-larshin avatar Aug 10 '20 15:08 oleg-larshin