zak.taccardi
01/04/2023, 9:26 PM// an ongoing global scope that should never be cancelled
val scope = CoroutineScope(dispatchers.default)
However, a developer can easily call:
scope.cancel()
Is there any standard way to prevent this? I’d like to throw an exception if a dev inadvertently calls scope.cancel()
?jw
01/04/2023, 9:31 PMGlobalScope + dispatchers.default
instead?jw
01/04/2023, 9:33 PMCoroutineScope
and return dispatchers.default
from the contextjw
01/04/2023, 9:33 PMjw
01/04/2023, 9:34 PMJob
zak.taccardi
01/04/2023, 9:34 PMjw
01/04/2023, 9:38 PMscope.cancel()
while also allowing regular exception propagationzak.taccardi
01/04/2023, 9:41 PMwhile also allowing regular exception propagationyeah, this is exactly what I would want ideally. Technically, no dev has accidentally cancelled a scope yet so maybe I’m overthinking this, but it would be nice to be defensive. Something I struggle with is how far do we go with structured concurrency. For example, it would be nice to build an app with a root scope from a
@Test
and cancel it at the end and have cancellation propagate correctly - but maybe that’s overthinking too.streetsofboston
01/04/2023, 9:42 PMRiccardo Lippolis
01/04/2023, 9:44 PMCoroutineExceptionHandler
to the child coroutines...zak.taccardi
01/04/2023, 9:46 PMSupervisorScope
- I want failures to affect children.
Unlike coroutineScope, a failure of a child does not cause this scope to fail and does not affect its other children, so a custom policy for handling failures of its children can be implemented.
This is a global, ongoing scope that should never have a failure.
I guess this doesn’t matter because if a dev cancels the scope incorrectly the whole app will stop working, lolstreetsofboston
01/04/2023, 9:54 PMjw
01/04/2023, 9:56 PMfranztesca
01/04/2023, 11:07 PMclass NonCancellableJob(job: Job = Job()): Job by job {
override fun cancel(...) = error("Cannot cancel")
}
fun CoroutineScope.asNonCancellable(): CoroutineScope = CoroutineScope(NonCancellableJob(coroutineContext.job))