-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Consider deprecating or changing the behaviour of CoroutineContext.isActive #3300
Comments
This is why I believe it's often more correct to use
Why, though? Isn't I would also be reluctant in suggesting the
Without better-looking replacement than the body of the current property getter, users could "fix" their code by wrapping the body of their function with Whether we should deprecate TL;DR: I like both the deprecation option and the default change option, but if we deprecate I find the replacement |
… contexts with no job in it. Otherwise, the API is becoming an error prone for being called from jobless entrypoints (i.e. 'suspend fun main' or Ktor handlers), as 'if (ctx.isActive)' is a well-established pattern for busy-wait or synchronous job. It is now aligned with CoroutineScope.isActive behaviour. Fixes #3300
According to the doc,
isActive
has the following property:It means that, if the
Job
is not present,isActive
always returnsfalse
.We have multiple reports that such behaviour can be error-prone when used with non-
kotlinx.coroutines
entry points, such as Ktor andsuspend fun main
, because it is inconsistent with the overall contract:CoroutineContext.isActive
predates bothCoroutineScope
(which should always have aJob
in it, if it's notGlobalScope
) andjob
extension, so it may be the case that it can be safely deprecated.Basically, we have three options:
CoroutineContext.isActive
. Such change has multiple potential downsidesthis.job.isActive
, but this replacement is not equivalent to the original method --.job
throws an exception for contexts without aJob
. An absence of replacement can be too disturbing as a lot of code rely on a perfectly finectxWithJob.isActive
.job.isActive
no longer can be called from such entry points safelytrue
. It also "fixes" such patterns asGlobalScope.isActive
but basically is a breaking changeThe text was updated successfully, but these errors were encountered: