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.

Can't join a canceled task

See original GitHub issue

Hi,

In the following code, I want to make sure my code blocks until the task is really finished:

        val job = async(CommonPool) {
            while (isActive);
            Thread.sleep(1000)
            System.out.println("end of async")
        }

        job.cancel()
        job.join()
        System.out.println("end of test")

The result is that I see “end of test” instantly, and “end of async” appears a second after, even though I asked to join() the task. Am I misusing the library or is this a bug?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:21 (15 by maintainers)

github_iconTop GitHub Comments

5reactions
elizarovcommented, Jun 26, 2017

I’ve pushed the corresponding changes to the develop branch. Now jobs (including Deferred) have an additional cancelling state. So, on invocation of cancel they go into cancelling state. isActive starts returning false, but isComplete is still false in this state. Both join and await wait for completion, that is they wait until the coroutine completes it execution.

It is important to note, that in current implementation a cancelled coroutine is doomed to complete exceptionally. Even if CancellationException is caught inside a coroutine and is replaced with any kind of result, it will still go into cancelled state on completion.

Note, that the naming of various states (see docs on Job and Deferred) are still under consideration and may change: https://github.com/Kotlin/kotlinx.coroutines/blob/develop/kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental/Job.kt

4reactions
elizarovcommented, Jul 18, 2017

The reason to change the implementation of join and await is two-fold. One is consistency with Thread.join as was mentioned above, but that would not be so compelling if not for the other. The other is to ensure that async(context) { doSomething() }.await() is really working just as run(context) { doSomething() } without any spurious concurrency on cancellation. This is what really bothers me in the current (version <= 0.16) implementation of join and await, it is that when I cancel the coroutine, then the other coroutine that was waiting for it is going to be executed concurrently with whatever code that was still running in the coroutine that was cancelled, but has not completed just yet. This concurrency would be so rare, that it would be a source of very hard-to-diagnose bugs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why the Task doesn't show canceled status - Stack Overflow
Cancellation is co-operative. Your task ignored your cancellation token and instead chose to run to completion. Therefore, it wasn't cancelled.
Read more >
If a task get cancelled no further task for the sa... - ServiceNow
If a task get cancelled no further task for the same assignment should be create or trigger.
Read more >
A task was canceled. all projects - Visual Studio Feedback
i have just updated visual studio to v15.8 and now i am getting the error >A task was canceled. everytime i try to...
Read more >
Cancel a task - AWS Snowcone User Guide
To send a cancel request for a specific task, use the cancel-task command. You can cancel only tasks in the QUEUED state that...
Read more >
Radarr 4.0.4.5922 stopped talking w/ Deluge - "A task ... - Reddit
Radarr 4.0.4.5922 stopped talking w/ Deluge - "A task was canceled". Logged into Radarr today, ... New comments cannot be posted and votes...
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