Deprecate and remove Job.cancel(cause)
See original GitHub issueJob.cancel(any exception)
is a cumbersome API:
- It has unclear semantics (especially after #333 fix, when
cancel(cause)
can returntrue
more than once for the sake of exception handling) - It shares a given exception between multiple coroutines which can mutate it
- It doesn’t provide any significant value from the user-facing API standpoint, because
cancel
can returnfalse
, forcing a user to handle an exception on the callsite anyway
As for implementation, it complicates Job
exception handling a lot and has a lot of maintenance burden as opposed to Job.cancel()
or Job.cancel(message: String)
We can remove cancel(cause)
from public API and move it to attachChild
machinery (e.g. by introducing ParentHandle
instead of DisposableHandle
).
It should make parent-child communication (-> cancellation and exception handling) much simpler and open a door for state machine optimizations.
If someone has a use-case which cannot be reasonably implemented without cancellation with a custom exception, let’s discuss it here.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:4
- Comments:5 (5 by maintainers)
Top Results From Across the Web
Cancel a job that was created using the deprecated master ...
This article explains how to cancel an active planning job that uses the deprecated master planning engine.
Read more >Replace deprecated `only/except` with `rules` from ... - GitLab
Another reason I never pushed to remove them is that it seemed like only/except might stick around forever. Without a removal milestone, ...
Read more >How to cancel with and without interruption using Coroutines ...
I've tried adapting the source of runInterruptible to not interrupt under a certain condition: passing an instance of a custom exception class ...
Read more >Change log for kotlinx.coroutines
The old API is deprecated for removal, but the new API is based on the similar ... Job.cancel(cause) and ReceiveChannel.cancel(cause) are deprecated, ...
Read more >Jobs | Kubernetes
Suspending a Job will delete its active Pods until the Job is resumed again. A simple case is to create one Job object...
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 FreeTop 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
Top GitHub Comments
After discussion with @LouisCAD I’ve found at least one use-case which is hard to workaround: external callback-based API working as parent/owner of the job.
An example may be found here. In this example,
CameraDevice
(CameraDevice.StateCallback
) actually owns the job and cancels it any (asynchronous) error occurred with that error. And it’s essential for a user to get this error oncompletion.await()
, otherwise troubleshooting may be cumbersome.Workaround without
cancel(cause)
requires to create one moreDeferred
and pass it as parent of thecompletion
, but it makes code less readable@elizarov note that
completion
is deferred returned byasync
call, it’s not aCompletableDeferred
instance and has nocompleteWithException
.IMO having
complete*
family onDeferred
is a bad idea as we have seen inCompletableFuture
API