# Task termination

**URL:** https://discourse.julialang.org/t/task-termination/25231
**Category:** New to Julia
**Tags:** task, async
**Created:** [June 13, 2019, 8:38am UTC](https://discourse.julialang.org/t/task-termination/25231 "2019-06-13T08:38:43Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![tomhaber](https://avatars.discourse-cdn.com/v4/letter/t/41988e/32.png) [@tomhaber](https://discourse.julialang.org/u/tomhaber)
#### Post date: [June 13, 2019, 8:38am UTC](https://discourse.julialang.org/t/task-termination/25231/1 "2019-06-13T08:38:43Z")

</div>

I’m trying to get the following to work (or something like it)

```julia
function execute(f::Function)
    exec = Executor()

    t = schedule(@task try
            while true
                run!(exec)
                yield()
            end
        catch e
            shutdown!(exec)
            println("done")
            if !isa(e, InterruptException)
                println("Shutdown Executor: $e")
                rethrow(e)
            end
        end
    )

    try
        f(exec)
    finally
        println("shutting down")
        #schedule(@task Base.throwto(t, InterruptException()))
        Base.throwto(t, InterruptException())
        wait(t)
    end
end

```

But I’m getting strange error I cannot make sense of:  
“WARNING: Workqueue inconsistency detected: popfirst!(Workqueue).state != :queued”  
or everything hangs.

I get the feeling that maybe the main task not a Task or its acting differently? I’ve read on discourse that you should schedule the throwto, but this also does not work.

What would be a good way to achieve something like this?

---

<div class="post-metadata">

### Author: ![jpsamaroo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jpsamaroo/32/46804_2.png) [@jpsamaroo](https://discourse.julialang.org/u/jpsamaroo)
#### Post date: [June 13, 2019, 7:42pm UTC](https://discourse.julialang.org/t/task-termination/25231/2 "2019-06-13T19:42:55Z")

</div>

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

---

<div class="post-metadata">

### Author: ![tomhaber](https://avatars.discourse-cdn.com/v4/letter/t/41988e/32.png) [@tomhaber](https://discourse.julialang.org/u/tomhaber)
#### Post date: [June 13, 2019, 8:21pm UTC](https://discourse.julialang.org/t/task-termination/25231/3 "2019-06-13T20:21:44Z")

</div>

```julia
function execute(f::Function)
    t = schedule(@task try
            while true
                yield()
            end
        catch e
            println("done")
            if !isa(e, InterruptException)
                println("Shutdown Executor: $e")
                rethrow(e)
            end
        end
    )

    try
        f()
    finally
        println("shutting down")
        #schedule(@task Base.throwto(t, InterruptException()))
        Base.throwto(t, InterruptException())
        wait(t)
    end
end

execute(()->println("hello"))

```

This will give the warning “WARNING: Workqueue inconsistency detected: popfirst!(Workqueue).state != :queued” and then hangs.

I’m guessing that `yield` does not do what I expect it to do. Changing `yield` to `sleep(0)` and using the `schedule(@task throwto)` method seems to do the what I want, but I cannot explain why it needs to be that complicated. Also I don’t want to sleep…

Thanks in advance

---

<div class="post-metadata">

### Author: ![jpsamaroo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jpsamaroo/32/46804_2.png) [@jpsamaroo](https://discourse.julialang.org/u/jpsamaroo)
#### Post date: [June 13, 2019, 8:27pm UTC](https://discourse.julialang.org/t/task-termination/25231/4 "2019-06-13T20:27:40Z")

</div>

Thanks for the update, I can reproduce this behavior. I also think I found the explanation, although it may not be totally gratifying: [https://github.com/JuliaLang/julia/issues/25353#issuecomment-354802975](https://github.com/JuliaLang/julia/issues/25353#issuecomment-354802975)

---

<div class="post-metadata">

### Author: ![tlienart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlienart/32/7640_2.png) [@tlienart](https://discourse.julialang.org/u/tlienart)
#### Post date: [June 14, 2019, 2:10am UTC](https://discourse.julialang.org/t/task-termination/25231/5 "2019-06-14T02:10:41Z")

</div>

replacing the `throwto` with

```julia
schedule(t, InterruptException(), error=true)

```

gives

```julia
julia> execute(()->println("hello"))
hello
shutting down
done

```

I use this `schedule(...)` a fair bit in some of my code and it “mostly works” though sometimes there can be synchronization issues in which case adding a short `sleep(...)` can help iron out issues (it can be a bit messy but I think there is the project of having a more robust system a la trio in the future, see also [this discussion](https://github.com/JuliaLang/julia/issues/6283)).

---

<div class="post-metadata">

### Author: ![world-peace](https://avatars.discourse-cdn.com/v4/letter/w/9f8e36/32.png) [@world-peace](https://discourse.julialang.org/u/world-peace)
#### Post date: [December 9, 2024, 9:15am UTC](https://discourse.julialang.org/t/task-termination/25231/6 "2024-12-09T09:15:41Z")

</div>

Random comment: FYI it should be `rethrow()` not `rethrow(e)`.

See here

- [Essentials · The Julia Language](https://docs.julialang.org/en/v1/base/base/#Base.rethrow)

---

<div class="post-metadata">

### Author: ![world-peace](https://avatars.discourse-cdn.com/v4/letter/w/9f8e36/32.png) [@world-peace](https://discourse.julialang.org/u/world-peace)
#### Post date: [December 9, 2024, 9:17am UTC](https://discourse.julialang.org/t/task-termination/25231/7 "2024-12-09T09:17:54Z")

</div>

Does anyone have any good references to this subject area in Julia? Documentation on tasks and async is quite minimal.

It’s hard to build a mental model of how async works at a low level, so therefore it’s hard to figure out exactly how to program using these ideas.

---

<div class="post-metadata">

### Author: ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)
#### Post date: [December 9, 2024, 1:53pm UTC](https://discourse.julialang.org/t/task-termination/25231/8 "2024-12-09T13:53:23Z")

</div>

Please open a new topic if you have a related question, the last post on this thread was over 5 years ago and any new reply pings everyone who took part back then.
