Skip to content

Commit

Permalink
SpinLock causes deadlock :/
Browse files Browse the repository at this point in the history
  • Loading branch information
vchuravy committed Mar 24, 2021
1 parent ffb3034 commit 300ed29
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
4 changes: 2 additions & 2 deletions stdlib/Distributed/src/cluster.jl
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ end
@enum WorkerState W_CREATED W_CONNECTED W_TERMINATING W_TERMINATED
mutable struct Worker
id::Int
msg_lock::Threads.SpinLock # Lock for del_msgs, add_msgs, and gcflag, SpinLock since it needs to be used from finalizers
msg_lock::Threads.ReentrantLock # Lock for del_msgs, add_msgs, and gcflag
del_msgs::Array{Any,1}
add_msgs::Array{Any,1}
gcflag::Bool
Expand Down Expand Up @@ -134,7 +134,7 @@ mutable struct Worker
if haskey(map_pid_wrkr, id)
return map_pid_wrkr[id]
end
w=new(id, Threads.SpinLock(), [], [], false, W_CREATED, Threads.Condition(), time(), conn_func)
w=new(id, Threads.ReentrantLock(), [], [], false, W_CREATED, Threads.Condition(), time(), conn_func)
w.initialized = Event()
register_worker(w)
w
Expand Down
25 changes: 16 additions & 9 deletions stdlib/Distributed/src/remotecall.jl
Original file line number Diff line number Diff line change
Expand Up @@ -247,13 +247,18 @@ function del_clients(pairs::Vector)
end
end

# The task below is coalescing the `flush_gc_msgs` call
# across multiple producers, see `send_del_client`,
# and `send_add_client`.
# XXX: Is this worth the additional complexity?
# `flush_gc_msgs` has to iterate over all connected workers.
const any_gc_flag = Threads.Condition()
function start_gc_msgs_task()
@async begin
Threads.@spawn begin
while true
lock(any_gc_flag) do
wait(any_gc_flag)
flush_gc_msgs()
flush_gc_msgs() # handle's throws internally
end
end
end
Expand All @@ -266,13 +271,15 @@ function send_del_client(rr)
elseif id_in_procs(rr.where) # process only if a valid worker
w = worker_from_id(rr.where)
msg = (remoteref_id(rr), myid())
# Lock is SpinLock an can thus be acquired from finalizer
lock(w.msg_lock) do
push!(w.del_msgs, msg)
w.gcflag = true
end
lock(any_gc_flag) do
notify(any_gc_flag)
# We cannot acquire locks from finalizers
@async begin
lock(w.msg_lock) do
push!(w.del_msgs, msg)
w.gcflag = true
end
lock(any_gc_flag) do
notify(any_gc_flag)
end
end
end
end
Expand Down

0 comments on commit 300ed29

Please sign in to comment.