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.

Deprecate and remove Job.cancel(cause)

See original GitHub issue

Job.cancel(any exception) is a cumbersome API:

  • It has unclear semantics (especially after #333 fix, when cancel(cause) can return true 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 return false, 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:closed
  • Created 5 years ago
  • Reactions:4
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
qwwdfsadcommented, Aug 9, 2018

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 on completion.await(), otherwise troubleshooting may be cumbersome.

Workaround without cancel(cause) requires to create one more Deferred and pass it as parent of the completion, but it makes code less readable

1reaction
qwwdfsadcommented, Aug 9, 2018

@elizarov note that completion is deferred returned by async call, it’s not a CompletableDeferred instance and has no completeWithException.

IMO having complete* family on Deferred is a bad idea as we have seen in CompletableFuture API

Read more comments on GitHub >

github_iconTop 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 >

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