Can't join a canceled task
See original GitHub issueHi,
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:
- Created 6 years ago
- Reactions:2
- Comments:21 (15 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’ve pushed the corresponding changes to the
develop
branch. Now jobs (includingDeferred
) have an additional cancelling state. So, on invocation ofcancel
they go into cancelling state.isActive
starts returningfalse
, butisComplete
is stillfalse
in this state. Bothjoin
andawait
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
andDeferred
) 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.ktThe reason to change the implementation of
join
andawait
is two-fold. One is consistency withThread.join
as was mentioned above, but that would not be so compelling if not for the other. The other is to ensure thatasync(context) { doSomething() }.await()
is really working just asrun(context) { doSomething() }
without any spurious concurrency on cancellation. This is what really bothers me in the current (version <= 0.16) implementation ofjoin
andawait
, 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.