Skip to content

Commit d43dfee

Browse files
committed
Cleanly shutdown the GC messages task
`start_gc_msgs_task()` previously launched a lingering task that could cause hangs when Julia is started with multiple threads.
1 parent 1a25fc7 commit d43dfee

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

src/cluster.jl

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1337,19 +1337,34 @@ end
13371337

13381338
using Random: randstring
13391339

1340+
# Exit handler state
1341+
const shutting_down = Threads.Atomic{Bool}(false)
1342+
1343+
function atexit_handler()
1344+
if inited[]
1345+
terminate_all_workers()
1346+
end
1347+
1348+
shutting_down[] = true
1349+
@lock any_gc_flag notify(any_gc_flag)
1350+
if !isnothing(gc_msgs_task)
1351+
wait(gc_msgs_task)
1352+
end
1353+
end
1354+
13401355
# do initialization that's only needed when there is more than 1 processor
13411356
const inited = Threads.Atomic{Bool}(false)
13421357
function init_multi()
13431358
if !Threads.atomic_cas!(inited, false, true)
13441359
push!(Base.package_callbacks, _require_callback)
1345-
atexit(terminate_all_workers)
13461360
init_bind_addr()
13471361
cluster_cookie(randstring(HDR_COOKIE_LEN))
13481362
end
13491363
return nothing
13501364
end
13511365

13521366
function init_parallel()
1367+
atexit(atexit_handler)
13531368
start_gc_msgs_task()
13541369

13551370
# start in "head node" mode, if worker, will override later.

src/remotecall.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,10 +272,12 @@ end
272272
# XXX: Is this worth the additional complexity?
273273
# `flush_gc_msgs` has to iterate over all connected workers.
274274
const any_gc_flag = Threads.Condition()
275+
gc_msgs_task::Union{Task, Nothing} = nothing
276+
275277
function start_gc_msgs_task()
276-
errormonitor(
278+
global gc_msgs_task = errormonitor(
277279
@async begin
278-
while true
280+
while !shutting_down[]
279281
lock(any_gc_flag) do
280282
# this might miss events
281283
wait(any_gc_flag)

0 commit comments

Comments
 (0)