# I don't get an error messages after a task/ thread failed

**URL:** https://discourse.julialang.org/t/i-dont-get-an-error-messages-after-a-task-thread-failed/36255
**Category:** Internals & Design
**Created:** [March 20, 2020, 12:12pm UTC](https://discourse.julialang.org/t/i-dont-get-an-error-messages-after-a-task-thread-failed/36255 "2020-03-20T12:12:22Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)
#### Post date: [March 20, 2020, 12:12pm UTC](https://discourse.julialang.org/t/i-dont-get-an-error-messages-after-a-task-thread-failed/36255/1 "2020-03-20T12:12:22Z")

</div>

Shouldn’t I get notified it a inner task fails?

---

<div class="post-metadata">

### Author: ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)
#### Post date: [March 20, 2020, 3:06pm UTC](https://discourse.julialang.org/t/i-dont-get-an-error-messages-after-a-task-thread-failed/36255/2 "2020-03-20T15:06:01Z")

</div>

Yes you should:

```julia
julia> t = @async begin
           sleep(1)
           error("FAIL")
       end
Task (runnable) @0x00007f0fd9203820

julia> fetch(t)
ERROR: TaskFailedException:
FAIL
Stacktrace:
 [1] error(::String) at ./error.jl:33
 [2] macro expansion at ./REPL[1]:3 [inlined]
 [3] (::var"#5#6")() at ./task.jl:333
Stacktrace:
 [1] wait at ./task.jl:251 [inlined]
 [2] fetch(::Task) at ./task.jl:266
 [3] top-level scope at REPL[2]:1

```

```julia
julia> t = Threads.@spawn begin
           sleep(1)
           error("FAIL")
       end
Task (runnable) @0x00007f0fd92004f0

julia> fetch(t)
ERROR: TaskFailedException:
FAIL
Stacktrace:
 [1] error(::String) at ./error.jl:33
 [2] macro expansion at ./REPL[3]:3 [inlined]
 [3] (::var"#7#8")() at ./threadingconstructs.jl:113
Stacktrace:
 [1] wait at ./task.jl:251 [inlined]
 [2] fetch(::Task) at ./task.jl:266
 [3] top-level scope at REPL[4]:1

```

Or are we not speaking of the same thing?

---

<div class="post-metadata">

### Author: ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)
#### Post date: [March 20, 2020, 3:47pm UTC](https://discourse.julialang.org/t/i-dont-get-an-error-messages-after-a-task-thread-failed/36255/3 "2020-03-20T15:47:18Z")

</div>

Thank you for the quick anwser!

```julia
julia> Threads.@spawn @async error("Fail")
Task (done) @0x0000000010821fb0

```

I did this.

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [March 20, 2020, 3:56pm UTC](https://discourse.julialang.org/t/i-dont-get-an-error-messages-after-a-task-thread-failed/36255/4 "2020-03-20T15:56:42Z")

</div>

You need to fetch the result.

---

<div class="post-metadata">

### Author: ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)
#### Post date: [March 20, 2020, 4:41pm UTC](https://discourse.julialang.org/t/i-dont-get-an-error-messages-after-a-task-thread-failed/36255/5 "2020-03-20T16:41:22Z")

</div>

Shouldn’t this fire anyway?

---

<div class="post-metadata">

### Author: ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)
#### Post date: [March 20, 2020, 8:04pm UTC](https://discourse.julialang.org/t/i-dont-get-an-error-messages-after-a-task-thread-failed/36255/6 "2020-03-20T20:04:32Z")

</div>

I think it should. We need structured concurrency for this:

[https://github.com/JuliaLang/julia/issues/33248](https://github.com/JuliaLang/julia/issues/33248)

Until we get structured concurrency, making sure to wrap `@async` and `@spawn` always with `@sync` is the closest thing you can do.

---

<div class="post-metadata">

### Author: ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)
#### Post date: [March 23, 2020, 10:27am UTC](https://discourse.julialang.org/t/i-dont-get-an-error-messages-after-a-task-thread-failed/36255/7 "2020-03-23T10:27:03Z")

</div>

How would I use `@sync` here?

```julia
function t()
    @async error("")
    return 1
end

t()

```

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [March 23, 2020, 10:55am UTC](https://discourse.julialang.org/t/i-dont-get-an-error-messages-after-a-task-thread-failed/36255/8 "2020-03-23T10:55:00Z")

</div>

Either return the task and wait for it or put `@sync` in `t`. `@sync` syncs the tasks that are lexically enclosed.

---

<div class="post-metadata">

### Author: ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)
#### Post date: [March 23, 2020, 7:50pm UTC](https://discourse.julialang.org/t/i-dont-get-an-error-messages-after-a-task-thread-failed/36255/9 "2020-03-23T19:50:14Z")

</div>

Treating something like this safely requires so-called nursery (also known as task group and a few other names). ATM, if you want a task to live longer than the function creating it, you need to manually pass it back to the caller. It’s not safe (you may forget doing so). That’s why I think we need structured concurrency.

---

<div class="post-metadata">

### Author: ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)
#### Post date: [March 24, 2020, 4:43pm UTC](https://discourse.julialang.org/t/i-dont-get-an-error-messages-after-a-task-thread-failed/36255/10 "2020-03-24T16:43:56Z")

</div>

I think I don’t mind the unstructured concurrency. But it’s very important to have a stacktrace if something fails! Where is the right place to say that?

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [March 24, 2020, 6:24pm UTC](https://discourse.julialang.org/t/i-dont-get-an-error-messages-after-a-task-thread-failed/36255/11 "2020-03-24T18:24:03Z")

</div>

In what task should the error be thrown?

---

<div class="post-metadata">

### Author: ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)
#### Post date: [March 24, 2020, 9:14pm UTC](https://discourse.julialang.org/t/i-dont-get-an-error-messages-after-a-task-thread-failed/36255/12 "2020-03-24T21:14:45Z")

</div>

I think the whole program should stop running unless you started the task in a try-catch-block.

I think the error should be thrown in the most inner task and propagate out until it reaches a try-catch-block or crashes the program.

The stack trace should go from the occurred error through the started task and so on.

These are my first thoughts 🙂

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [March 24, 2020, 9:15pm UTC](https://discourse.julialang.org/t/i-dont-get-an-error-messages-after-a-task-thread-failed/36255/13 "2020-03-24T21:15:43Z")

</div>

That is structured concurrency.

---

<div class="post-metadata">

### Author: ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)
#### Post date: [March 24, 2020, 9:23pm UTC](https://discourse.julialang.org/t/i-dont-get-an-error-messages-after-a-task-thread-failed/36255/14 "2020-03-24T21:23:38Z")

</div>

Would this

```julia
function f()
    @async sleep(10)
    return 1
end
try f() |> println catch end
dont_have_to_wait_and_do_the_next_thing()

```

still work with structured concurrency?

---

<div class="post-metadata">

### Author: ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)
#### Post date: [March 24, 2020, 9:41pm UTC](https://discourse.julialang.org/t/i-dont-get-an-error-messages-after-a-task-thread-failed/36255/15 "2020-03-24T21:41:11Z")

</div>

You’d write something like

```julia
function f(nursery)
    @async nursery sleep(10)
    return 1
end

try
    withnursery() do nursery
        f(nursery) |> println
        dont_have_to_wait_and_do_the_next_thing()
    end
catch
end

```

---

<div class="post-metadata">

### Author: ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)
#### Post date: [March 24, 2020, 9:49pm UTC](https://discourse.julialang.org/t/i-dont-get-an-error-messages-after-a-task-thread-failed/36255/16 "2020-03-24T21:49:27Z")

</div>

I don’t understand the syntax. What is `withnursery()` and where is it defined?

The syntax gets more complicated. What’s the point against:

> [@Impressium](#):
>
> I think the whole program should stop running unless you started the task in a try-catch-block.
> 
> I think the error should be thrown in the most inner task and propagate out until it reaches a try-catch-block or crashes the program.
> 
> The stack trace should go from the occurred error through the started task and so on.
> 
> These are my first thoughts 🙂

---

<div class="post-metadata">

### Author: ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)
#### Post date: [March 24, 2020, 10:04pm UTC](https://discourse.julialang.org/t/i-dont-get-an-error-messages-after-a-task-thread-failed/36255/17 "2020-03-24T22:04:21Z")

</div>

It’s bad because the caller can’t tell if your function or _any_ function that might exist in the call stack starting from your function may leak un-synchronized tasks; i.e., it violates so-called “black box rule”.

For more information, please have a look at Julep [Juleps/StructuredConcurrency.md at master · JuliaLang/Juleps · GitHub](https://github.com/JuliaLang/Juleps/blob/master/StructuredConcurrency.md) and the discussion in the PR. Your point is related to this discussion on effect system which is effectively a dynamically-scoped nursery: [Taking Structured Concurrency Seriously · Issue #33248 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/33248#issuecomment-573365469)

---

<div class="post-metadata">

### Author: ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)
#### Post date: [March 24, 2020, 10:09pm UTC](https://discourse.julialang.org/t/i-dont-get-an-error-messages-after-a-task-thread-failed/36255/18 "2020-03-24T22:09:37Z")

</div>

I’m not sure, what I want. Both sites have their benefits…

---

<div class="post-metadata">

### Author: ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)
#### Post date: [March 27, 2020, 8:29am UTC](https://discourse.julialang.org/t/i-dont-get-an-error-messages-after-a-task-thread-failed/36255/19 "2020-03-27T08:29:17Z")

</div>

This is my solution as far:

```julia
macro my_async(ex)
    quote
        @async try $(esc(ex))
        catch e
           println("IN @my_async") 
           # Here I would like to get the definition line number of `ex`
           @show e
        end
    end
end

```

So now I get feedback if a task fails.

---

<div class="post-metadata">

### Author: ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)
#### Post date: [March 27, 2020, 8:50am UTC](https://discourse.julialang.org/t/i-dont-get-an-error-messages-after-a-task-thread-failed/36255/20 "2020-03-27T08:50:52Z")

</div>

FWIW, you can improve the error printing with

```julia
macro my_async(ex)
    quote
        @async try 
            $(esc(ex))
        catch e
            println("In task:") 
            @error(exception = (e, catch_backtrace()))
        end
    end
end

```

[Next page](https://discourse.julialang.org/t/i-dont-get-an-error-messages-after-a-task-thread-failed/36255.md?page=2)
