Trevor Stone
09/01/2022, 3:37 AMclose
on the websocket would cancel the coroutine scope and clean up children jobs. This doesn't seem to be the case, which I can work around, but I was wondering if this is the behavior for a specific reason?Aleksei Tirman [JB]
09/01/2022, 10:35 AMTrevor Stone
09/01/2022, 1:56 PMwebSocket("/ws") { // websocketSession
println("started")
outgoing.send(Frame.Text("Hello Friend!"))
launch {
try {
awaitCancellation()
} catch (_: Exception) {
println("complete")
}
}
close(CloseReason(CloseReason.Codes.NORMAL, "Client said BYE"))
}
In this situation I would expect that upon calling close that the launched block would be cancelled and complete is printing. Similarly, it appears that if close isn't called here and instead the client disconnects the launch block is also NOT canceledCasey Brooks
09/01/2022, 7:31 PMlaunch
is never actually started because you aren’t reading from the websocket. The whole scope is canceled before launch
is actually dispatched, and therefore never actually enters the launch
block. Since you’re not reading from incoming
, I think it would just immedately return from websocket { }
, thus cancelling the coroutineScope
instead of suspending
Have you tried putting a println()
inside launch
before try
to see if it even gets started? If you change it to launch(start = CoroutineStart.UNDISPATCHED) { }
do you see the same thing?Trevor Stone
09/01/2022, 7:34 PMrouting {
webSocket("/ws") { // websocketSession
println("started")
outgoing.send(Frame.Text("Hello Friend!"))
launch {
try {
println("entered")
awaitCancellation()
} catch (_: Exception) {
println("complete")
}
}
close(CloseReason(CloseReason.Codes.NORMAL, "Client said BYE"))
cancel()
}