Skip to content

Commit

Permalink
prevent deadlock when releasing the jl_unique_gcsafe_lock causes gc (#…
Browse files Browse the repository at this point in the history
…56563)

Caught this by running threads test repeatedly locally: the sweep needs
to acquire engine_lock, so we need to make sure to release that first
(the other jl_unique_gcsafe_lock users shouldn't care about this
ordering since they don't acquire their locks during sweeping)
  • Loading branch information
vtjnash authored Nov 14, 2024
1 parent d99d569 commit 100e305
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/julia_locks.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,16 @@ class jl_unique_gcsafe_lock {
explicit jl_unique_gcsafe_lock(std::mutex &native) JL_NOTSAFEPOINT_ENTER
{
jl_task_t *ct = jl_current_task;
gc_state = jl_gc_safe_enter(ct->ptls);
gc_state = jl_gc_safe_enter(ct->ptls); // contains jl_gc_safepoint after enter
this->native = std::unique_lock(native);
ct->ptls->engine_nqueued++; // disables finalizers until inference is finished on this method graph
}
jl_unique_gcsafe_lock(jl_unique_gcsafe_lock &&native) = delete;
jl_unique_gcsafe_lock(jl_unique_gcsafe_lock &native) = delete;
~jl_unique_gcsafe_lock() JL_NOTSAFEPOINT_LEAVE {
jl_task_t *ct = jl_current_task;
jl_gc_safe_leave(ct->ptls, gc_state);
native.unlock();
jl_gc_safe_leave(ct->ptls, gc_state); // contains jl_gc_safepoint after leave
ct->ptls->engine_nqueued--; // enable finalizers (but don't run them until the next gc)
}
void wait(std::condition_variable& cond) JL_NOTSAFEPOINT {
Expand Down

0 comments on commit 100e305

Please sign in to comment.