# When using channels, errors do not get raised, and computation stalls

**URL:** https://discourse.julialang.org/t/when-using-channels-errors-do-not-get-raised-and-computation-stalls/100291
**Category:** General Usage
**Created:** [June 13, 2023, 5:14pm UTC](https://discourse.julialang.org/t/when-using-channels-errors-do-not-get-raised-and-computation-stalls/100291 "2023-06-13T17:14:15Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [June 13, 2023, 5:14pm UTC](https://discourse.julialang.org/t/when-using-channels-errors-do-not-get-raised-and-computation-stalls/100291/1 "2023-06-13T17:14:15Z")

</div>

This is intended behavior, a known issue, or an unknown bug?

```julia
julia> function test()     
           buff = Channel{Int}(3)
           for _ in 1:3
              put!(buff, 0)
           end
           @sync for _ in 1:30
               Threads.@spawn begin
                   take!(buff)
                   s = 0.0
                   for j in 1:10^6 
                       s += log(j)*exp(j)*s 
                   end
                   error()
                   put!(buff, 1)
               end
           end
       end
test (generic function with 2 methods)

julia> test() # stalls, but does not raise any error.
^CERROR: InterruptException:

```

(edited post, localizing better the problem)

---

<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: [June 13, 2023, 7:34pm UTC](https://discourse.julialang.org/t/when-using-channels-errors-do-not-get-raised-and-computation-stalls/100291/2 "2023-06-13T19:34:29Z")

</div>

I think this is an unfortunate behavior, which is more or less intended due to the way [`@sync`](https://docs.julialang.org/en/v1/base/parallel/#Base.@sync) works:

> `@sync`
> 
> Wait until all lexically-enclosed uses of `@async`, `@spawn`, `@spawnat` and `@distributed` are complete. All exceptions thrown by enclosed async operations are collected and thrown as a `CompositeException`.

In your case, the first 3 tasks error out and are the only ones to complete because all other tasks wait for input. This means `@sync` can’t return and show you the errors.

With [`errormonitor`](https://docs.julialang.org/en/v1/base/parallel/#Base.errormonitor), you can at least see the errors thrown by the failing tasks in real time:

```julia
julia> function test()     
                  buff = Channel{Int}(3)
                  for _ in 1:3
                     put!(buff, 0)
                  end
                  @sync for _ in 1:30
                      t = Threads.@spawn begin
                          take!(buff)
                          s = 0.0
                          for j in 1:10^6 
                              s += log(j)*exp(j)*s 
                          end
                          error()
                          put!(buff, 1)
                      end
                      errormonitor(t)
                  end
              end
test (generic function with 1 method)

```

```julia
julia> test()
Unhandled Task ERROR: 
Stacktrace:
 [1] error()
   @ Base ./error.jl:44
 [2] macro expansion
   @ ./REPL[11]:13 [inlined]
 [3] (::var"#13#14"{Channel{Int64}})()
   @ Main ./threadingconstructs.jl:373
Unhandled Task ERROR: 
Stacktrace:
 [1] error()
   @ Base ./error.jl:44
 [2] macro expansion
   @ ./REPL[11]:13 [inlined]
 [3] (::var"#13#14"{Channel{Int64}})()
   @ Main ./threadingconstructs.jl:373
Unhandled Task ERROR: 
Stacktrace:
 [1] error()
   @ Base ./error.jl:44
 [2] macro expansion
   @ ./REPL[11]:13 [inlined]
 [3] (::var"#13#14"{Channel{Int64}})()
   @ Main ./threadingconstructs.jl:373
^CERROR: InterruptException:

```

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [June 13, 2023, 7:37pm UTC](https://discourse.julialang.org/t/when-using-channels-errors-do-not-get-raised-and-computation-stalls/100291/3 "2023-06-13T19:37:58Z")

</div>

Good to know, thanks, hard to debug something like that, though, it would be better if `errormirror()` was the default behavior, and possibly allowing its suppression by some `@sync` option.

---

<div class="post-metadata">

### Author: ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)
#### Post date: [June 13, 2023, 8:18pm UTC](https://discourse.julialang.org/t/when-using-channels-errors-do-not-get-raised-and-computation-stalls/100291/4 "2023-06-13T20:18:54Z")

</div>

There is also [`Experimental.@sync`](https://github.com/JuliaLang/julia/blob/84fc130f7a51fcd67feb9b4ece4c725772e92c6d/base/experimental.jl#L87), which errors directly. This is pretty convenient when developing at least.
