# Backtrace in Tasks

**URL:** <https://discourse.julialang.org/t/backtrace-in-tasks/40713>\
**Category:** Internals & Design\
**Tags:** question\
**Created:** [June 4, 2020, 8:13am UTC](https://discourse.julialang.org/t/backtrace-in-tasks/40713 "2020-06-04T08:13:33Z")\
**Posts on this page:** 8\
**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:** [June 4, 2020, 8:13am UTC](https://discourse.julialang.org/t/backtrace-in-tasks/40713/1 "2020-06-04T08:13:33Z")

</div>

At work I have to program concurrently. I really love programming in Julia but one thing that annoys me are the backtrace in tasks.

```julia
 t = @async begin
     for i in 1:10 end
     error()
 end
 
 t |> dump

```

```julia
 julia> 
 Task
   next: Nothing nothing
   queue: Nothing nothing
   storage: Nothing nothing
   state: Symbol failed
   donenotify: Base.GenericCondition{Base.Threads.SpinLock}
     waitq: Base.InvasiveLinkedList{Task}
       head: Nothing nothing
       tail: Nothing nothing
     lock: Base.Threads.SpinLock
       handle: Base.Threads.Atomic{Int64}
         value: Int64 0
   result: ErrorException
     msg: String ""
   exception: ErrorException
     msg: String ""
   backtrace: Array{Union{Ptr{Nothing}, Base.InterpreterIP}}((7,))
     1: Ptr{Nothing} @0x00000000668ae2e5
     2: Ptr{Nothing} @0x0000000066884d57
     3: Ptr{Nothing} @0x00000000320969f7
     4: Ptr{Nothing} @0x00000000320b0e84
     5: Ptr{Nothing} @0x00000000320b0ea4
     6: Ptr{Nothing} @0x00000000668675fd
     7: Ptr{Nothing} @0x0000000066884bd6
   logstate: Base.CoreLogging.LogState
     min_enabled_level: Base.CoreLogging.LogLevel
       level: Int32 -1
     logger: Atom.Progress.JunoProgressLogger Atom.Progress.JunoProgressLogger()
   code: #15 (function of type var"#15#16")
   sticky: Bool true

```

What does that mean?

```julia
backtrace: Array{Union{Ptr{Nothing}, Base.InterpreterIP}}((7,))
     1: Ptr{Nothing} @0x00000000668ae2e5
     2: Ptr{Nothing} @0x0000000066884d57
     3: Ptr{Nothing} @0x00000000320969f7
     4: Ptr{Nothing} @0x00000000320b0e84
     5: Ptr{Nothing} @0x00000000320b0ea4
     6: Ptr{Nothing} @0x00000000668675fd
     7: Ptr{Nothing} @0x0000000066884bd6

```

I need to now where the error happened (which line and file) and how it propagates.

Is there a better way or something to look forward to?

Cheers

---

<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:** [June 4, 2020, 8:24am UTC](https://discourse.julialang.org/t/backtrace-in-tasks/40713/2 "2020-06-04T08:24:41Z")

</div>

Try

```julia
julia> Base.display_error(t.exception, t.backtrace)
ERROR: nope
Stacktrace:
 [1] error(::String) at ./error.jl:33
 [2] macro expansion at ./REPL[2]:3 [inlined]
 [3] (::var"#13#14")() at ./task.jl:358

```

---

<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:** [June 4, 2020, 3:06pm UTC](https://discourse.julialang.org/t/backtrace-in-tasks/40713/4 "2020-06-04T15:06:59Z")

</div>

> [@Impressium](#):
>
> I really love programming in Julia but one thing that annoys me are the backtrace in tasks.

This is not how you retrieve the backtrace though. The error gets thrown when you `fetch` from the task:

```julia
julia> fetch(t)
ERROR: TaskFailedException:

Stacktrace:
 [1] error() at ./error.jl:42
 [2] macro expansion at ./REPL[1]:3 [inlined]
 [3] (::var"#3#4")() at ./task.jl:358
Stacktrace:
 [1] wait at ./task.jl:267 [inlined]
 [2] fetch(::Task) at ./task.jl:282
 [3] top-level scope at REPL[1]:1

```

Inspecting the internals of the `Task` object will be very unreliable.

---

<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:** [June 5, 2020, 8:28am UTC](https://discourse.julialang.org/t/backtrace-in-tasks/40713/5 "2020-06-05T08:28:44Z")

</div>

I don’t want to wait for it to fail. Just spin it of and if something goes wrong (mainly during the development face) I want to see the error.  
For me it seems like the tasks live in their own universe…

---

<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:** [June 5, 2020, 8:58am UTC](https://discourse.julialang.org/t/backtrace-in-tasks/40713/6 "2020-06-05T08:58:37Z")

</div>

You still need to synchronize the result somehow. Either via the return value (`fetch` / `wait`), or using `@sync`. Throwing tasks out into the ether will not be reliable.

If you want to print from the task itself you could do

```julia
julia> t = @async begin
           try
               for i in 1:10 end
               error()
           catch e
               @error "Task failed" exception=(e, catch_backtrace())
               rethrow()
           end
       end;
┌ Error: Task failed
│ exception =
│
│ Stacktrace:
│ [1] error() at ./error.jl:42
│ [2] macro expansion at ./REPL[6]:4 [inlined]
│ [3] (::var"#9#10")() at ./task.jl:358
└ @ Main REPL[6]:6245:

```

---

<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:** [June 5, 2020, 9:06am UTC](https://discourse.julialang.org/t/backtrace-in-tasks/40713/7 "2020-06-05T09:06:08Z")

</div>

> [@kristoffer.carlsson](#):
>
> Throwing tasks out into the ether will not be reliable.

What happens if I don’t fetch or sync them?

---

<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:** [June 5, 2020, 9:16am UTC](https://discourse.julialang.org/t/backtrace-in-tasks/40713/8 "2020-06-05T09:16:49Z")

</div>

> [@Impressium](#):
>
> What happens if I don’t fetch or sync them?

Then you code is racy since you won’t know about what progress the task has made (and as you’ve already seen, exceptions will just dissappear).

---

<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:** [June 5, 2020, 9:37am UTC](https://discourse.julialang.org/t/backtrace-in-tasks/40713/9 "2020-06-05T09:37:52Z")

</div>

I know it’s not the prettiest but here’s my solution with the help from @kristoffer.carlsson and @pfitzseb. Thank you!

```julia
macro my_async(ex)
    quote
        @async try 
            $(esc(ex))
        catch e
            @error "Task failed" exception=(e, catch_backtrace())
            rethrow()
        end
    end
end

```
