Skip to content

Commit 5c42f10

Browse files
authored
fix #32970, at-threads disabled after a loop errors (#33034)
1 parent e6dd72f commit 5c42f10

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

base/threadingconstructs.jl

+2-9
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ on `threadid()`.
1818
"""
1919
nthreads() = Int(unsafe_load(cglobal(:jl_n_threads, Cint)))
2020

21-
# Only read/written by the main thread
22-
const in_threaded_loop = Ref(false)
23-
2421
function _threadsfor(iter,lbody)
2522
lidx = iter.args[1] # index
2623
range = iter.args[2]
@@ -65,15 +62,11 @@ function _threadsfor(iter,lbody)
6562
end
6663
end
6764
end
68-
# Hack to make nested threaded loops kinda work
69-
if threadid() != 1 || in_threaded_loop[]
70-
# We are in a nested threaded loop
65+
if threadid() != 1
66+
# only thread 1 can enter/exit _threadedregion
7167
Base.invokelatest(threadsfor_fun, true)
7268
else
73-
in_threaded_loop[] = true
74-
# the ccall is not expected to throw
7569
ccall(:jl_threading_run, Cvoid, (Any,), threadsfor_fun)
76-
in_threaded_loop[] = false
7770
end
7871
nothing
7972
end

test/threads_exec.jl

+16
Original file line numberDiff line numberDiff line change
@@ -670,3 +670,19 @@ let ch = Channel{Char}(0), t
670670
schedule(t)
671671
@test String(collect(ch)) == "hello"
672672
end
673+
674+
# errors inside @threads
675+
function _atthreads_with_error(a, err)
676+
Threads.@threads for i in eachindex(a)
677+
if err
678+
error("failed")
679+
end
680+
a[i] = Threads.threadid()
681+
end
682+
a
683+
end
684+
@test_throws TaskFailedException _atthreads_with_error(zeros(nthreads()), true)
685+
let a = zeros(nthreads())
686+
_atthreads_with_error(a, false)
687+
@test a == [1:nthreads();]
688+
end

0 commit comments

Comments
 (0)