New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Don't allow runCatching
with supend functions inside
#5468
Comments
What's the solution to a violation like this? |
I'm also a bit hesitant on forbidding |
This is similar to forbid the usage of
Don't use |
Nicola, I agree with the report, I just didn't know how to fix it. I think there's no question here whether cc @julia-martemianova @Zeliret as far as I remember you reach a similar conclusion before, right? It's just that most projects don't know they doing it wrong, because it only matters in the resource usage patterns / edge cases / error cases. So it's really hard to monitor/track or even observe the bad effects directly. The bad behavior will come as a weird side-effect that's hard to trace back to this issue, so better nip it in the bud. I agree that it needs to be very clear why it's problematic, and the rule needs strong guidance of good patterns how to solve this problem. 1 As usual, exceptions apply (no puns intended): if one actually does checks the failure in the |
If we go for this rule, then the solution for user would be to either:
|
I wonder if there's an open issue for Kotlin/Coroutines library to address this in some way. |
That's my solution. Adding a |
I have myself been hit by this issue a few times(and it's really hard to pinpoint. Although I started using a similar function like
But shouldn't we handle |
I would start with |
I agree with Atul, considering the problem is not runCatching itself, but rather how it hides the catching of CancellationException (or any other special exception), I would propose to simply add this handling to the Since there needs to be a specific handling for TooGenericExceptionCaught:
exceptionNames: [...]
functionNames: []
suspendFunctionNames: ['runCatching'] because in the end your OP is essentially describing a false negative for |
I think that a rule for this case is a better option. We can then add logic to that rule to check the paths of the try catch and ensure that it doesn't catch the exception or it rethrow it directly. I would not expand the current rule because that would make it way too complex to use. |
Can I try this issue? I implemented one POC with few passing TCs |
Expected Behavior of the rule
Raise any usage of
runCatching
that calls asuspend
function.Example:
Context
From the documentation:
Extracted from: https://kotlinlang.org/docs/cancellation-and-timeouts.html#cancellation-is-cooperative
If you catch and don't rethrow all the
CancellationException
your coroutines are not cancelled even if you cancel theirCoroutineScope
. And that can create huge issues that are really difficult to fix because the errors and exceptions that you get are not related with thatrunCatching
.At work we had this issue for several months until we realised what was going on. We already have
TooGenericExceptionCaught
that works withtry/catch
but we don't have anything to spot this issue. At work we are considering to forbidrunCatching
all together but I think that at detekt we should have a rule to spot this.It's true that you can be careful and check the type of the caught exception and rethrow it in case of
CancellationException
but that's far to easy to forget.I think that this rule should be inside
coroutines
butpotential-bugs
could be also a good fit.The text was updated successfully, but these errors were encountered: