# How to debug in coroutine(task)?

**URL:** <https://discourse.julialang.org/t/how-to-debug-in-coroutine-task/71299>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [November 11, 2021, 1:48am UTC](https://discourse.julialang.org/t/how-to-debug-in-coroutine-task/71299 "2021-11-11T01:48:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![HANHAOHAN](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hanhaohan/32/36366_2.png) [@HANHAOHAN](https://discourse.julialang.org/u/HANHAOHAN)\
**Post date:** [November 11, 2021, 1:48am UTC](https://discourse.julialang.org/t/how-to-debug-in-coroutine-task/71299/1 "2021-11-11T01:48:21Z")

</div>

Recently, when I develop, the process output nothing. Then with using `@show ` I found bug in the code of task(coroutine).  
Does anyone knows if there be some tools or methods to debug in coroutine(task)?

---

<div class="post-metadata">

**Author:** ![caleb-allen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/caleb-allen/32/14054_2.png) [@caleb-allen](https://discourse.julialang.org/u/caleb-allen)\
**Post date:** [November 11, 2021, 2:43am UTC](https://discourse.julialang.org/t/how-to-debug-in-coroutine-task/71299/2 "2021-11-11T02:43:17Z")

</div>

It may be that @show is what triggers the task to display its result.

You might also be able to achieve the same thing with `wait(t)`

---

<div class="post-metadata">

**Author:** ![HANHAOHAN](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hanhaohan/32/36366_2.png) [@HANHAOHAN](https://discourse.julialang.org/u/HANHAOHAN)\
**Post date:** [November 11, 2021, 6:45am UTC](https://discourse.julialang.org/t/how-to-debug-in-coroutine-task/71299/3 "2021-11-11T06:45:35Z")

</div>

Thanks for your answer!  
May be I described not clear enough.  
I mean,  
I wrote the program with Juilalang.  
And it have one routine, being started with `@async`.  
It run, but without correct output.  
Finally, with `@show`, I debugged it, step by step.  
Is there other tools or methods, to debug program like this easier?

Thanks again!

---

<div class="post-metadata">

**Author:** ![caleb-allen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/caleb-allen/32/14054_2.png) [@caleb-allen](https://discourse.julialang.org/u/caleb-allen)\
**Post date:** [November 11, 2021, 6:09pm UTC](https://discourse.julialang.org/t/how-to-debug-in-coroutine-task/71299/4 "2021-11-11T18:09:20Z")

</div>

Ah, I understand.

If you are using VS-Code, perhaps you can use the debugger? Documentation is here: [Debugging · Julia in VS Code](https://www.julia-vscode.org/docs/stable/userguide/debugging/)

It looks as if the debugger does not work inside async tasks, based on this issue: [https://github.com/JuliaDebug/JuliaInterpreter.jl/issues/413](https://github.com/JuliaDebug/JuliaInterpreter.jl/issues/413)

Is there a way you could extract your code into a function, and then call the function normally (without async) to use the debugger?

---

<div class="post-metadata">

**Author:** ![Janis\_Erdmanis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/janis_erdmanis/32/10869_2.png) [@Janis\_Erdmanis](https://discourse.julialang.org/u/Janis_Erdmanis)\
**Post date:** [November 11, 2021, 7:08pm UTC](https://discourse.julialang.org/t/how-to-debug-in-coroutine-task/71299/5 "2021-11-11T19:08:56Z")

</div>

Debugging tasks can be quite challenging. I used to have this prototype project where the tasks, spawned a task which spawned a task. It was a bad design, but for purposes to get it done, I found that redefining the `@async` macro like:

```julia
import Base.sync_varname
import Base.@async

macro async(expr)

    tryexpr = quote
        try
            $expr
        catch err
            @warn "error within async" exception=err # line $( __source__.line):
            @show stacktrace(catch_backtrace())
        end
    end

    thunk = esc(:(()->($tryexpr)))

    var = esc(sync_varname)
    quote
        local task = Task($thunk)
        if $(Expr(:isdefined, var))
            push!($var, task)
        end
        schedule(task)
        task
    end
end

```

works quite well while debugging.

From my limited experience, and this really is a matter of opinion, a good strategy is also to test the spawned function separately. To do that, it helps to use channels for IO. For instance, reading a string from a socket can be refactored into reading byte string from a `Channel`, which is easier to test. In addition to avoiding introducing more tasks, one can subtype `AbstractChannel`, and that way insert a code dealing with the `IO` object.

Another thing that helps in debugging tasks is reformulating the logic in communicating finite state machines. Something like:

```julia
newstate, newmsg = step(state, msg)

```

works quite well with Julia’s multiple dispatch mechanism. This design also goes hand in hand with the ability to notify condition `condition = Threads.Condition()` with a value like:

```julia
notify(condition, state)

```

enabling to debug the code within the task without using the `@show` method.

---

<div class="post-metadata">

**Author:** ![HANHAOHAN](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hanhaohan/32/36366_2.png) [@HANHAOHAN](https://discourse.julialang.org/u/HANHAOHAN)\
**Post date:** [November 12, 2021, 6:48am UTC](https://discourse.julialang.org/t/how-to-debug-in-coroutine-task/71299/6 "2021-11-12T06:48:32Z")

</div>

Thank you very much!

---

<div class="post-metadata">

**Author:** ![HANHAOHAN](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hanhaohan/32/36366_2.png) [@HANHAOHAN](https://discourse.julialang.org/u/HANHAOHAN)\
**Post date:** [November 12, 2021, 6:50am UTC](https://discourse.julialang.org/t/how-to-debug-in-coroutine-task/71299/7 "2021-11-12T06:50:31Z")

</div>

Thank you! You are the first julia master I have communicated with.
