Skip to content
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

Cancelled scopes can be reused #621

Closed
ZakTaccardi opened this issue Sep 26, 2018 · 2 comments
Closed

Cancelled scopes can be reused #621

ZakTaccardi opened this issue Sep 26, 2018 · 2 comments

Comments

@ZakTaccardi
Copy link

ZakTaccardi commented Sep 26, 2018

It's a bit odd that cancelled scopes can be reused.

Example

val scope = CoroutineScope(Dispatchers.Unconfined + Job())

scope.launch { println("hello!") }

scope.coroutineContext[Job]!!.cancel()

// I expect this not to print, because the scope has been cancelled
scope.launch { println("goodbye!") }

Expected

hello!

Actual

hello!
goodbye!

EDIT: It seems the issue is that before launching a coroutine, the launch does not check if the scope has been cancelled or not:

yield example

val scope = CoroutineScope(Dispatchers.Unconfined + Job())

scope.launch { println("hello!") }

scope.coroutineContext[Job]!!.cancel()

scope.launch {
    yield()
    println("goodbye!")
}

result

hello!

Is this intentional?

@elizarov
Copy link
Contributor

This happens because you are using Dispatchers.Unconfined. Please, don't use Dispatchers.Unconfined it may result is other weird and unexplicable behaviors, beyond the one you had demonstrated.

@elizarov
Copy link
Contributor

This is not intentional, though. Just a side-effect. We might be able to fix it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants