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.

CoroutineScope cancellation requires a Job

See original GitHub issue

I expected a CoroutineScope to be cancellable on it’s own. But a Job must be specified for a CoroutineScope to be cancelled. This seems a bit odd and error-prone.

Example

The following fails with IllegalStateException: Scope should no longer be active:

val scope = CoroutineScope(dispatcher)
check(scope.isActive) { "Scope should be active" }

scope.coroutineContext.cancel()

check(!scope.coroutineContext.isActive) { "Dispatcher should no longer be active" }
check(!scope.isActive) { "Scope should no longer be active" } // fails here

But if you cancel via a job, no problems:

val job = Job()
val scope = CoroutineScope(dispatcher + job)
check(scope.isActive) { "Scope should be active" }

job.cancel()

check(!scope.coroutineContext.isActive) { "Dispatcher should no longer be active" }
check(!scope.isActive) { "Scope should no longer be active" }

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
elizarovcommented, Sep 25, 2018

Nope. That was bad idea. We’ll create Job() if it is missing. This way, we’ll need no change to .isActive documentation either

0reactions
fvascocommented, Sep 25, 2018

Should the documentation fixed?

isActive and coroutineContext.isActive have different behaviour, despite the documentation.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cancellation in coroutines - Medium
The coroutine work doesn't just stop when cancel is called. Rather, we need to modify our code and check if the coroutine is...
Read more >
Cancellation in Kotlin Coroutines - Kt. Academy
The coroutine can run as long as it needs to clean up all the resources. However, suspension is no longer allowed. The Job...
Read more >
CoroutineScope & job.cancel() issue - Kotlin Discussions
Cancels all children jobs of this coroutine using Job.cancel for all of them. Unlike Job.cancel on this job as a whole, the state...
Read more >
Jobs, Waiting, Cancellation in Kotlin Coroutines
A Job is a cancellable thing with a life-cycle that culminates in its completion.Coroutine job is created with launch coroutine builder. It runs ......
Read more >
After a coroutine scope is canceled, can it still be used again?
See the CoroutineScope() function implementation and notice a Job is added if none found in the context. When you call cancel() on the ......
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