I've just found an interesting article: <https://b...
# coroutines
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
tl;dr: The experts improved slightly on my suggestion, and added it to the docs for
🙌🏻 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
Could you explain the difference between
Copy code
try {
  // do anything
} catch (e: Throwable) {
  if (e is CancellationException) currentCoroutineContext().ensureActive()
described in the last comment and the one currently in the kdoc:
Copy code
try {
} 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
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
check, as I did in my article, cancellations will be able to take precedence over real errors. Narrowing the catch clause or including the
check are both ways to avoid that.
👍🏻 1
OMG, I somehow missed the different catched exception types of the two
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.