ursus
05/02/2025, 11:42 PMvar webSocketSession = DefaultClientWebSocketSession? = null
try {
webSocketSession = ktorClient.webSocketSession(url)
....
} finally {
webSocketSession?.close() <----
}
Coroutines general question:
ktor calls close
in finally block as to do graceful exit.
However close
is a suspend function.
How can it work reliably? Aren't all suspend functions no-op after cancellation unless wrapped with NomCancellable
?
Am I missing something?rkechols
05/03/2025, 12:35 AMlaunch
to start a new coroutine in a scope that's been cancelled will do nothing, but you can still always call suspend
functions and it's just part of the current control flow.
I could be totally wrong thoughursus
05/03/2025, 12:38 AMursus
05/03/2025, 12:40 AMsend(close frame)
will throw; that exception is then eaten, so basically its a big no-op
so I've no idea what do the authors meant by itrkechols
05/03/2025, 2:02 AMwebSocketSession
is not null but its close
method is indeed not being called, it indeed may be that further calls to suspend functions don't work at all once the scope is cancelled.
In that case, the finally
block would be more relevant in the case of normal termination with no exception or for any exception other than a cancellation.ursus
05/03/2025, 11:34 AMwebSocketSession.stuff()
webSocketSession.close()
which makes me think im missing somethingCLOVIS
05/06/2025, 2:17 PMNomCancellable
?
After cancellation yes, but a non-cancellation error could be thrown, in which case it will be closed.hantsy
05/15/2025, 1:11 AM