question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Use of runCatching and coroutines breaks structured concurrency

See original GitHub issue

Since runCatching catches all exceptions, it catches CancellationException which prevents any coroutines launched within the runCatching block from being cancelled when their enclosing CoroutineScope is cancelled.

This is not, strictly speaking, an issue with kotlin-result but it caught me by surprise so I thought it would probably be good to warn others of the potential gotcha.

I propose:

  1. A warning in the kdoc comment, pointing out the problem or,
  2. 1 + a new function in kotlin-result-coroutines called something like runSuspendCatching() which rethrows CancellationException.

I’ve already written a version of runSuspendCatching() and a test case that demonstrates the behaviour in my own project so I’m more than happy to submit a PR if you want.

The same issue has been raised for the stdlib runCatching at Kotlin/kotlinx.couroutines#1814 but it doesn’t seem like the Kotlin devs thinks it’s much of a problem. I disagree and thought maybe we could at least improve things here!

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
Munzeycommented, Oct 5, 2021

I also think a syntax like rethrow()<> for the specific result types where Err extends Exception reads better.

For cases where result Err is of some other type that does not extend exception, i think it would be difficult to make a more generic version of this function without throwing something, unless perhaps a third state was added to Result, something like Result.ExcludedErr where invoking things like onSuccess and onFailure on it do nothing.

1reaction
michaelbullcommented, Oct 7, 2021

@grodin separate PRs for each one would be best.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Resilient use cases with kotlin.Result, coroutines and ...
runCatching does not rethrow cancellation exceptions. This makes it a bad choice for coroutines as it breaks structured concurrency.
Read more >
Are You Handling Exceptions in Kotlin Coroutines Properly?
In this article I will try to show situations where you need to be more cautious about exceptions and show some best practices...
Read more >
Things you should know about Coroutines. Part 4: Exception ...
It enforces structured concurrency by creating parent-child relationships between coroutines and manages exception propagation and cancellation.
Read more >
Resilient use cases with kotlin.Result, coroutines ... - Droidcon
runCatching does not rethrow cancellation exceptions. This makes it a bad choice for coroutines as it breaks structured concurrency.
Read more >
Coroutines (Part III) – Structured Concurrency and Cancellation
Use yield() to let other work run. According to the docs: “Yields the thread (or thread pool) of the current coroutine dispatcher to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found