Norbi
01/13/2024, 10:17 PMWebSocketSession
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...Norbi
01/13/2024, 10:18 PMAleksei Tirman [JB]
01/17/2024, 12:09 PMNorbi
01/17/2024, 2:21 PMwebSocket("/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.)Aleksei Tirman [JB]
01/17/2024, 4:11 PM