fix crashes using multithreaded julia from python#758
Open
dpinol wants to merge 2 commits intoJuliaPy:mainfrom
Open
fix crashes using multithreaded julia from python#758dpinol wants to merge 2 commits intoJuliaPy:mainfrom
dpinol wants to merge 2 commits intoJuliaPy:mainfrom
Conversation
added 2 commits
April 16, 2026 17:11
The vectors PYJLVALUES and PYJLFREEVALUES are modified without any protection against GC-triggered re-entrancy.
_pyjl_dealloc(o1) or PyJuliaValue_SetValue
└→ push!(PYJLFREEVALUES, idx) # begins resize (sets internal flag)
└→ vector must grow → allocates → triggers Julia GC
└→ GC collects a Py object → runs py_finalizer
└→ enqueue(ptr) → PyGILState_Check()==1 (same thread holds GIL)
└→ Py_DecRef(ptr) → refcount hits 0
└→ _pyjl_dealloc(o2)
└→ push!(PYJLFREEVALUES, idx2) ← BOOM: flag already set
ConcurrencyViolationError!
1. push! needs to grow the vector (exceeds capacity), so it allocates
2. The allocation triggers Julia GC, which runs finalizers
3. A finalizer calls Py_DecRef (because enqueue sees the GIL is held on this same thread), dropping refcount to 0, which triggers _pyjl_dealloc re-entrantly on the same vector
This is especially likely during shutdown (at_jl_exit → jl_atexit_hook), because jl_gc_run_all_finalizers runs finalizers on the calling thread (the main thread, which holds the GIL).
Many Py objects are finalized at once, repeatedly pushing to PYJLFREEVALUES, and each push that triggers a reallocation can cause the re-entrant chain.
Disabled Julia's GC during the critical vector modifications, so that push!/pop! cannot trigger allocation-based GC,
which prevents the finalizer chain from re-entering.
Added a SpinLock as defense-in-depth against true multi-thread races.
After jl_atexit_hook(0) returns, Python continues destroying objects, calling _pyjl_dealloc. But Julia's GC subsystem is already torn down, so Base.GC.enable() and lock() crash. Now we add a flag that's set by a Julia atexit hook (runs before finalizers inside jl_atexit_hook). When the flag is set, _pyjl_dealloc skips all Julia runtime operations — the process is exiting anyway.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Solves #669
Two issues:
fix: rare crash due to memory race condition
The vectors PYJLVALUES and PYJLFREEVALUES are modified without any protection against GC-triggered re-entrancy.
This is especially likely during shutdown (at_jl_exit → jl_atexit_hook), because jl_gc_run_all_finalizers runs finalizers on the calling thread (the main thread, which holds the GIL).
Many Py objects are finalized at once, repeatedly pushing to PYJLFREEVALUES, and each push that triggers a reallocation can cause the re-entrant chain.
Disabled Julia's GC during the critical vector modifications, so that push!/pop! cannot trigger allocation-based GC,
which prevents the finalizer chain from re-entering.
Added a SpinLock as defense-in-depth against true multi-thread races.
fix: crash in atexit
After jl_atexit_hook(0) returns, Python continues destroying objects, calling _pyjl_dealloc.
But Julia's GC subsystem is already torn down, so Base.GC.enable() and lock() crash.
Now we add a flag that's set by a Julia atexit hook (runs before finalizers inside jl_atexit_hook).
When the flag is set, _pyjl_dealloc skips all Julia runtime operations — the process is exiting anyway.