Very simple question I suppose.
What are the implications of not canceling a
CoroutineScope
at the end of its lifecycle?
Example of scope:
Copy code
private val myOwnScope = CoroutineScope(Dispatchers.MY_DISPATCHER + SupervisorJob() + CoroutineName("..."))
If I'm not interested in canceling the children jobs, should I still cancel it?
s
Sam
08/31/2023, 10:34 AM
Cancelling the coroutine scope is just a shortcut for cancelling the job in the scope’s context. It doesn’t do anything else besides that.
Sam
08/31/2023, 10:34 AM
Failing to cancel jobs is a potential issue in itself, though. For example, if those jobs are holding resources like open network connections, the resources might not be released.
e
Edoardo Luppi
08/31/2023, 10:35 AM
Thanks Sam, makes sense! In my case the jobs are guaranteed to not hold any kind of resource, but I'll see how to abstract/wrap the cancellation so it's done anyway
👍 1
r
Robert Williams
08/31/2023, 10:43 AM
Yep, it's your scope so it's up to you to define what "end of its lifecycle" even means. If the lifecycle is "the entire lifetime of the process" then clearly there's no reason to ever cancel. If it's tied to some other component's lifetime then it's implied by the contract that any work started will not outlive the component