Provide a way to cancel a running coroutine immediately
See original GitHub issueWhen using blocking IO or computations the coroutine won’t be cancelled until the operation is complete meaning finally
and invokeOnCompletion
blocks can be called long after the parent job is cancelled.
Here’s an example:
class MyPresenter {
fun loadStuff() {
scope.launch {
view.showProgress()
try {
performNetworkRequest()
} catch(e: Exception) {
view.hideProgress()
}
}
}
}
The example above might crash if the performNetworkRequest
isn’t cancelled before the view is detached from the presenter.
Currently the only way around this is to either use suspendCancellableCoroutine
and perform a context switch to suspend the coroutine or to use invokeOnCompletion(onCancelling = true)
(which is marked internal).
Perhaps a special context could be added to indicate that the result of an operation should be ignored when cancelling.
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (6 by maintainers)
Top Results From Across the Web
How To Stop Or Cancel A Kotlin Coroutine ... - Stack Overflow
A kotlin coroutine must cooperate to allow cancellation. That means it has some check points calling one suspend function.
Read more >Cancellation in coroutines - Medium
If we just call cancel , it doesn't mean that the coroutine work will just stop. If you're performing some relatively heavy computation,...
Read more >Cancellation and timeouts | Kotlin
This section covers coroutine cancellation and timeouts. Cancelling coroutine execution. In a long-running application you might need fine- ...
Read more >Cancellation in Kotlin Coroutines - Kt. Academy
The first one is to use the yield() function from time to time. This function suspends and immediately resumes a coroutine. This gives...
Read more >Cancelling coroutines - Lanky Dan Blog
We have looked at how to cancel a job and how to write a coroutine that can be cancelled. A job can be...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
On the JVM, there’s no generic way to force cancellation for any blocking operation. Some APIs have the ability to cancel, others you can work around with thread interruptions. But it’s up to the integration with your particular blocking API to handle actually cancelling the blocking operation. If you’re using Retrofit’s coroutine integration, network requests will automatically be cancelled when the coroutine is cancelled.
Even if you can’t actually cancel the operation, as long as the network request suspends the current coroutine (which it will if it changes context), the cancellation exception should be getting thrown from that function when it finishes at least. Catch and rethrow
CancellationException
, or check ife is CancellationException
, before updating view state again. In general, swallowingCancellationException
s (or genericException
s) is usually not a great idea unless you’re really sure that’s what you want to do.I created this function now that would fix this. Thanks for the suggestion.