Skip to content

Commit 71041f7

Browse files
committed
Take profile lock after suspending thread
1 parent c75c7bb commit 71041f7

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/signals-unix.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,6 @@ static void *signal_listener(void *arg)
750750
// sample each thread, round-robin style in reverse order
751751
// (so that thread zero gets notified last)
752752
if (critical || profile) {
753-
jl_lock_profile();
754753
if (!critical)
755754
jl_shuffle_int_array_inplace(profile_round_robin_thread_order, jl_n_threads, &profile_cong_rng_seed);
756755
for (int idx = jl_n_threads; idx-- > 0; ) {
@@ -759,6 +758,10 @@ static void *signal_listener(void *arg)
759758
// notify thread to stop
760759
jl_thread_suspend_and_get_state(i, &signal_context);
761760

761+
// Taking profile lock after to avoid deadlock in case the i-th thread is
762+
// trying to take the lock when signalled.
763+
jl_lock_profile();
764+
762765
// do backtrace on thread contexts for critical signals
763766
// this part must be signal-handler safe
764767
if (critical) {
@@ -810,10 +813,11 @@ static void *signal_listener(void *arg)
810813
}
811814
}
812815

816+
jl_unlock_profile();
817+
813818
// notify thread to resume
814819
jl_thread_resume(i, sig);
815820
}
816-
jl_unlock_profile();
817821
}
818822
#ifndef HAVE_MACH
819823
if (profile && running) {

0 commit comments

Comments
 (0)