I have just run into an issue with WebSockets: alt...
# ktor
n
I have just run into an issue with WebSockets: although
WebSocketSession
implements
CoroutineScope
, it is not cancelled when the WebSocket connection terminates. Is this a bug? The issue is present both in the client and the server WebSocket implementations...
a
Can you please share a code snippet to troubleshoot this issue?
n
Thanks @Aleksei Tirman [JB] for taking a look - maybe I do something wrong... It is something like the following trivial code on the server:
Copy code
webSocket("/websocket") {
    launch {
        while (true) {
            println("Connected...")
            delay(1000)
        }
    }

    try {
        for (m in incoming) {
            // Process incoming messages
        }
    } finally {
        println("Disconnected")
        // cancel() // This is needed to cancel the DefaultWebSocketServerSession's coroutine scope
    }
}
When a client connects, the coroutine starts printing "Connected..." to the console. But when the client disconnects, only "Disconnected" is printed, the coroutine is not stopped automatically, an explicit
cancel()
call is needed to cancel the scope. (I have just upgraded from Ktor 2 to Ktor 3 but the behaviour is the same, the coroutine scope is not cancelled.)
a
This is a bug. Here is a similar issue.
1
🙏 1