```var webSocketSession = DefaultClientWebSocketSe...
# coroutines
u
Copy code
var 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?
r
https://kotlinlang.org/docs/cancellation-and-timeouts.html#closing-resources-with-finally My understanding is that using
launch
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 though
u
not sure what you mean
im debugging it, control flow enters close, but doesnt do anything, as
send(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 it
r
Well if you know that
webSocketSession
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.
u
if it were designed to be a graceful termination, why put it in finally block? In that case it should look more like
Copy code
webSocketSession.stuff()
webSocketSession.close()
which makes me think im missing something
c
> Aren't all suspend functions no-op after cancellation unless wrapped with
NomCancellable
? After cancellation yes, but a non-cancellation error could be thrown, in which case it will be closed.
💯 1
h
Kotlin provides`<autoclosable resource>.use {}` to close the resources gracefully like Java try-catch-resources.