Is there any meaningful difference between manually cancelling a coroutine and throwing CancellationException? #3790
kevincianfarini
started this conversation in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I have a flow that, when it encounters a certain value, should terminate exceptionally. For specific reasons this flow should be cancelled and not be a specific exception that should be otherwise handled.
This raised a certain question for me: Is there any meaningful difference between cancelling a coroutine manually versus throwing cancellation exception? Eg.
My suspicion is that these will differ in the following ways:
Flow.explicitWithInternalScope
This will create a new job internal to the flow and cancel that job. If this job gets cancelled it will bubble upwards as you'd expect, but won't cancel sibling coroutines in the event that a parent is a supervisor.
Flow.explicitCancelsDirectParent
Similar to the above, however in the event that the scope this coroutine is running in is a supervisor it will be cancelled. This might come as a surprise to consumers of this flow.
Flow.implicit
I am unsure about this one and it's what prompted this discussion. Will throwing CancellationException still progress a job through its expected state machine? Eg:
Active
->Cancelling
->Cancelled
. Is this potentially dangerous in any way? Are theJob.cancel
functions mostly meant to cancel work in a top-down approach, which either the implicit or explicit approached can be used to cancel from the bottom up?Beta Was this translation helpful? Give feedback.
All reactions