# Channel hangs with \>8 threads but not 7 in REPL

**URL:** <https://discourse.julialang.org/t/channel-hangs-with-8-threads-but-not-7-in-repl/72274>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [November 30, 2021, 7:00am UTC](https://discourse.julialang.org/t/channel-hangs-with-8-threads-but-not-7-in-repl/72274 "2021-11-30T07:00:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![kykim0](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kykim0/32/30612_2.png) [@kykim0](https://discourse.julialang.org/u/kykim0)\
**Post date:** [November 30, 2021, 7:00am UTC](https://discourse.julialang.org/t/channel-hangs-with-8-threads-but-not-7-in-repl/72274/1 "2021-11-30T07:00:11Z")

</div>

Hello!

I have the following code that uses `Channel` to manage `Task`s created with `Threads.@spawn`. The idea is to be able to run a method (called `simulate()`) in parallel using multiple threads, wait for some timeout for the tasks to finish, and to surface any errors encountered.

```julia
function some_method()
    # ...
    println("Before sim start")
    timeout_us = CPUtime_us() + max_time * 1e6
    sim_channel = Channel{Task}(min(1000, n_iterations)) do channel
        for n in 1:n_iterations
            put!(channel, Threads.@spawn simulate(...))
            # println("Put ", n, ", ", length(channel.data))
        end
        println("Channel task finished ", channel)
    end

    for sim_task in sim_channel
        CPUtime_us() > timeout_us && break
        try
            fetch(sim_task) # Throws a TaskFailedException if failed.
        catch err
            throw(err.task.exception) # Throw the underlying exception.
        end
    end
    # ...
end

```

This code runs fine in REPL (i.e., `julia -t 8` and `julia> include("script.jl")`) with 7 threads but hangs with 8. By hanging I mean I see `Before sim start` getting printed at which point REPL hangs. The fact that it hangs with 8 threads but not 7 in REPL is already strange enough, but what’s even stranger is that if I uncomment the print statement after `put!` on channel, it doesn’t hang even with 8 threads. So my suspicion at that point was perhaps there is some random initialization of threads that’s affected by the code file causing things to somehow hang with 8 threads, as strange as that might sound.

Would really appreciate any help with the issue and curious to hear what’s the general strategy to debug this kind of issues in Julia.

Thanks!

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [November 30, 2021, 7:33am UTC](https://discourse.julialang.org/t/channel-hangs-with-8-threads-but-not-7-in-repl/72274/2 "2021-11-30T07:33:17Z")

</div>

Could you provide a complete minimal example to reproduce the problem?

(By the way to answer your methodology question: making a minimal example would be my first step to investigate the issue 🙂 )

---

<div class="post-metadata">

**Author:** ![c42f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c42f/32/52842_2.png) [@c42f](https://discourse.julialang.org/u/c42f)\
**Post date:** [November 30, 2021, 8:18am UTC](https://discourse.julialang.org/t/channel-hangs-with-8-threads-but-not-7-in-repl/72274/3 "2021-11-30T08:18:27Z")

</div>

> [@kykim0](#):
>
> The fact that it hangs with 8 threads but not 7 in REPL is already strange enough, but what’s even stranger is that if I uncomment the print statement after `put!` on channel, it doesn’t hang even with 8 threads.

This is a little odd. Though note that `println()` can yield control to the scheduler so enabling printing on that Task gives other tasks a chance to run. For example, simulation tasks might then interleave with your task which is adding to `sim_channel`. So if the bug you see is somehow dependent on scheduling order, it’s not entirely surprising that adding print debugging changes the behavior.

I guess you could try a low level call to `printf` in order to get some output without disturbing the scheduling order:

```julia
@ccall printf("Put %d\n"::Cstring; n::Cint)::Cint

```

In low level concurrent Julia programming it’s easy to get hangs when channel capacity is wrong for the ordering of `take!` and `put!`, especially if a task crashes and a `close(channel)` is forgotten. I can’t see any of the more obvious issues here though. A self-contained example would be best if you can make one.

---

<div class="post-metadata">

**Author:** ![hexaeder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hexaeder/32/24403_2.png) [@hexaeder](https://discourse.julialang.org/u/hexaeder)\
**Post date:** [November 30, 2021, 9:22am UTC](https://discourse.julialang.org/t/channel-hangs-with-8-threads-but-not-7-in-repl/72274/4 "2021-11-30T09:22:16Z")

</div>

What architecture and Julia Version do you use? People reported similar behaviour (7 vs 8 threads) on the new Macbooks with M1 Pro/Max [here](https://github.com/JuliaLang/julia/issues/41820#issuecomment-899175606) (both native and rosetta).

---

<div class="post-metadata">

**Author:** ![kykim0](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kykim0/32/30612_2.png) [@kykim0](https://discourse.julialang.org/u/kykim0)\
**Post date:** [December 1, 2021, 1:21am UTC](https://discourse.julialang.org/t/channel-hangs-with-8-threads-but-not-7-in-repl/72274/5 "2021-12-01T01:21:35Z")

</div>

Thanks for the suggestion. I tried to create a “minimal” example that would re-produce the issue but ended up creating a not-so-minimal one (but that still re-produces) 🙂

First of all, this is the script to run: [https://github.com/kykim0/MCTS.jl/blob/dev/test/dev.jl](https://github.com/kykim0/MCTS.jl/blob/dev/test/dev.jl). To run this, you need to install the MCTS.jl package (see [https://github.com/JuliaPOMDP/MCTS.jl](https://github.com/JuliaPOMDP/MCTS.jl)), and replace `src/vanilla.jl` with the version in my repo which is [https://github.com/kykim0/MCTS.jl/blob/dev/src/vanilla.jl](https://github.com/kykim0/MCTS.jl/blob/dev/src/vanilla.jl). Then do e.g., `julia --project -t 8 test/dev.jl`.

Please let me know if you can reproduce the issue on your machine. @hexaeder mentions below that similar issues have been observed on M1, and I was indeed using an M1 machine. Curious to see if this has to do with hardware compatibility.

---

<div class="post-metadata">

**Author:** ![kykim0](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kykim0/32/30612_2.png) [@kykim0](https://discourse.julialang.org/u/kykim0)\
**Post date:** [December 1, 2021, 1:27am UTC](https://discourse.julialang.org/t/channel-hangs-with-8-threads-but-not-7-in-repl/72274/6 "2021-12-01T01:27:55Z")

</div>

Thanks for the suggestion. Please see my response to @sijo for reproducing the issue (apologies for it being not so “minimal”).

I also tried your lower level `printf` in place of `println` with 8 threads, and I saw `n_iterations` (50 in my run) many put statements, but not the `println` statement after the for loop. I guess this means it did create all the `Task`s it was supposed to but then failed to exit out of the for loop somehow, which is more revealing but equally perplexing.

---

<div class="post-metadata">

**Author:** ![kykim0](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kykim0/32/30612_2.png) [@kykim0](https://discourse.julialang.org/u/kykim0)\
**Post date:** [December 1, 2021, 1:29am UTC](https://discourse.julialang.org/t/channel-hangs-with-8-threads-but-not-7-in-repl/72274/7 "2021-12-01T01:29:56Z")

</div>

Thanks for the pointer. I was indeed running things on an M1 machine with Julia 1.6.3. I’ll switch to a linux machine and see if I can reproduce the issue there.
