I've just found an interesting article: <https://b...
# coroutines
n
I've just found an interesting article: https://betterprogramming.pub/the-silent-killer-thats-crashing-your-coroutines-9171d1e8f79b What do the experts say about the proposed "double checked" cancellation? Thanks.
K 1
s
tl;dr: The experts improved slightly on my suggestion, and added it to the docs for
await()
โค๏ธ๐Ÿ™‡
๐Ÿ™Œ๐Ÿป 2
I'm glad you found it interesting! If I was writing it again I think I'd probably tone down the drama ๐Ÿ˜„ it's a bit over the top
๐Ÿ™‚ 1
๐Ÿคฃ 1
n
Could you explain the difference between
Copy code
try {
  // do anything
} catch (e: Throwable) {
  if (e is CancellationException) currentCoroutineContext().ensureActive()
  handleException(e)
}
described in the last comment and the one currently in the kdoc:
Copy code
try {
   deferred.await()
} catch (e: CancellationException) {
   currentCoroutineContext().ensureActive() // throws if the current coroutine was cancelled
   processException(e) // if this line executes, the exception is the result of `await` itself
}
Thanks.
s
Sure! The one in the kdoc actually uses
catch (e: CancellationException
, so it's similar to the one with
if (e is CancellationException)
. If you catch all throwables and omit the
is
check, as I did in my article, cancellations will be able to take precedence over real errors. Narrowing the catch clause or including the
is
check are both ways to avoid that.
๐Ÿ‘๐Ÿป 1
n
OMG, I somehow missed the different catched exception types of the two
catch
clauses. I need a rest, reading through the above issue you linked and checking all related things has really drained my brain ๐Ÿ˜„ (Just for reference, I edited my comment above to make it clear which two "variants" I have referred to...)
I ended up with the following (slightly different) implementation: https://github.com/kotlinw/kotlinw/blob/05dde5f77d400bc51e49c19ec89bea25f8d7575a/k[โ€ฆ]mp/src/commonMain/kotlin/kotlinw/util/coroutine/DeferredUtil.kt I hope it is correct based on the plethora of things I have learnt today :) Thanks for your help.