diff --git a/src/gc.c b/src/gc.c index fbfb5f6f2573b..f3a57fffe09a8 100644 --- a/src/gc.c +++ b/src/gc.c @@ -526,12 +526,17 @@ void jl_gc_run_all_finalizers(jl_task_t *ct) jl_ptls_t* gc_all_tls_states; gc_n_threads = jl_atomic_load_acquire(&jl_n_threads); gc_all_tls_states = jl_atomic_load_relaxed(&jl_all_tls_states); + // this is called from `jl_atexit_hook`; threads could still be running + // so we have to guard the finalizers' lists + JL_LOCK_NOGC(&finalizers_lock); schedule_all_finalizers(&finalizer_list_marked); for (int i = 0; i < gc_n_threads; i++) { jl_ptls_t ptls2 = gc_all_tls_states[i]; if (ptls2 != NULL) schedule_all_finalizers(&ptls2->finalizers); } + // unlock here because `run_finalizers` locks this + JL_UNLOCK_NOGC(&finalizers_lock); gc_n_threads = 0; gc_all_tls_states = NULL; run_finalizers(ct);