From 1253d48259cf66e50ac157b1f278440e7c783462 Mon Sep 17 00:00:00 2001 From: Jameson Nash Date: Thu, 10 Dec 2020 10:29:34 -0500 Subject: [PATCH] threads: avoid deadlock from recursive lock acquire (PR JuliaLang/julia#38487) (JuliaLang/julia#38753) Finalizers can't safely acquire many essential locks (such as the iolock, to cleanup libuv objects) if they are run inside another lock. Therefore, inhibit all finalizers on the thread until all locks are released (previously, this was only true for our internal locks). (cherry-picked from ec8466bee8ecc1c9064bdd1c93cd864075eda6a2) --- src/messages.jl | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/messages.jl b/src/messages.jl index bbfb13f..f8e1da1 100644 --- a/src/messages.jl +++ b/src/messages.jl @@ -147,6 +147,7 @@ function flush_gc_msgs(w::Worker) end # del_msgs gets populated by finalizers, so be very careful here about ordering of allocations + # XXX: threading requires this to be atomic new_array = Any[] msgs = w.del_msgs w.del_msgs = new_array @@ -178,7 +179,7 @@ function send_msg_(w::Worker, header, msg, now::Bool) wait(w.initialized) end io = w.w_stream - lock(io.lock) + lock(io) try reset_state(w.w_serializer) serialize_hdr_raw(io, header) @@ -191,7 +192,7 @@ function send_msg_(w::Worker, header, msg, now::Bool) flush(io) end finally - unlock(io.lock) + unlock(io) end end