# Threads are disabled if an error occurs

**URL:** https://discourse.julialang.org/t/threads-are-disabled-if-an-error-occurs/27738
**Category:** General Usage
**Tags:** question, multithreading
**Created:** [August 20, 2019, 5:12am UTC](https://discourse.julialang.org/t/threads-are-disabled-if-an-error-occurs/27738 "2019-08-20T05:12:50Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![ranocha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ranocha/32/35588_2.png) [@ranocha](https://discourse.julialang.org/u/ranocha)
#### Post date: [August 20, 2019, 5:12am UTC](https://discourse.julialang.org/t/threads-are-disabled-if-an-error-occurs/27738/1 "2019-08-20T05:12:50Z")

</div>

When an error is thrown in a threaded loop, only one thread is used in threaded loops thereafter.

```julia
julia> function f!(a)
           Threads.@threads for i in eachindex(a)
               a[i] = Threads.threadid()
           end
           a
       end
f! (generic function with 1 method)

julia> function g!(a)
           Threads.@threads for i in eachindex(a)
               sleep(Threads.threadid())
               a[i] = log(-Threads.threadid())
           end
           a
       end
g! (generic function with 1 method)

julia> a = zeros(Threads.nthreads())
6-element Array{Float64,1}:
 0.0
 0.0
 0.0
 0.0
 0.0
 0.0

julia> f!(a)
6-element Array{Float64,1}:
 1.0
 2.0
 3.0
 4.0
 5.0
 6.0

julia> g!(a)
ERROR: TaskFailedException:
DomainError with -1.0:
log will only return a complex result if called with a complex argument. Try log(Complex(x)).
Stacktrace:
 [1] throw_complex_domainerror(::Symbol, ::Float64) at ./math.jl:32
 [2] log(::Float64) at ./special/log.jl:285
 [3] macro expansion at ./special/log.jl:395 [inlined]
 [4] (::var"##19#threadsfor_fun#4"{Array{Float64,1},Base.OneTo{Int64}})(::Bool) at ./threadingconstructs.jl:64
 [5] (::var"##19#threadsfor_fun#4"{Array{Float64,1},Base.OneTo{Int64}})() at ./threadingconstructs.jl:31
Stacktrace:
 [1] wait(::Task) at ./task.jl:251
 [2] macro expansion at ./threadingconstructs.jl:75 [inlined]
 [3] g!(::Array{Float64,1}) at ./REPL[2]:2
 [4] top-level scope at REPL[5]:1

julia> a
6-element Array{Float64,1}:
 1.0
 2.0
 3.0
 4.0
 5.0
 6.0

julia> f!(a)
6-element Array{Float64,1}:
 1.0
 1.0
 1.0
 1.0
 1.0
 1.0

julia> Threads.nthreads()
6

```

This is on Julia v1.3.0-rc1.0. Julia v1.2.0-rc2.0 behaves similar (only a slightly different error message). I’m using the generic Linux x86\_64 binaries.

Is this behaviour expected? How can I enable all threads after such an error without restarting Julia?

---

<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: [August 20, 2019, 6:00am UTC](https://discourse.julialang.org/t/threads-are-disabled-if-an-error-occurs/27738/2 "2019-08-20T06:00:36Z")

</div>

This is a bug; it seems to occur because the C code `jl_threading_run` can throw, but the julia code doesn’t expect it to and omits to reset the variable `Threads.in_threaded_loop`. Arguably we should remove `in_threaded_loop` entirely and replace it with a julia implementation based on `@spawn`, though I’m not sure what the best approach is in the 1.3 timeframe.

If you could submit a bug report issue on github that would be great.

---

<div class="post-metadata">

### Author: ![ranocha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ranocha/32/35588_2.png) [@ranocha](https://discourse.julialang.org/u/ranocha)
#### Post date: [August 20, 2019, 6:12am UTC](https://discourse.julialang.org/t/threads-are-disabled-if-an-error-occurs/27738/3 "2019-08-20T06:12:08Z")

</div>

Thanks! I’ve opened an issue ([https://github.com/JuliaLang/julia/issues/32970](https://github.com/JuliaLang/julia/issues/32970)).
