|
11 | 11 | # when po is deallocated, and this callback function removes |
12 | 12 | # jo from pycall_gc so that Julia can garbage-collect it. |
13 | 13 |
|
| 14 | +using Base.Threads: atomic_add!, atomic_sub!, Atomic, SpinLock |
| 15 | + |
14 | 16 | const pycall_gc = Dict{PyPtr,Any}() |
15 | 17 |
|
16 | 18 | function weakref_callback(callback::PyPtr, wo::PyPtr) |
@@ -43,3 +45,74 @@ function pyembed(po::PyObject, jo::Any) |
43 | 45 | pycall_gc[wo] = jo |
44 | 46 | return po |
45 | 47 | end |
| 48 | + |
| 49 | +# Deferred `finalizer` / destructor queue(s): |
| 50 | +# |
| 51 | +# * In a `finalizer` context, it is unsafe to take the GIL since it |
| 52 | +# can cause deadlocks such as: |
| 53 | +# |
| 54 | +# 1. Task A holds the GIL and waits for Task B to finish something |
| 55 | +# 2. Task B enters GC and tries runs finalizers |
| 56 | +# 3. Task B cannot acquire the GIL and Task A is stuck waiting on B |
| 57 | +# |
| 58 | +# * To work around this, we defer any GIL-requiring operations to run |
| 59 | +# at the end of next `@with_GIL` block. |
| 60 | + |
| 61 | +const _deferred_count = Atomic{Int}(0) |
| 62 | +const _deferred_Py_DecRef = Vector{PyPtr}() |
| 63 | +const _deferred_PyBuffer_Release = Vector{PyBuffer}() |
| 64 | + |
| 65 | +# Since it is illegal for finalizers to `yield()` to the Julia scheduler, this |
| 66 | +# lock MUST be a `SpinLock` or similar. Most other locks in `Base` implicitly |
| 67 | +# yield. |
| 68 | +const _deferred_queue_lock = SpinLock() |
| 69 | + |
| 70 | +# Defers a `Py_DecRef(o)` call to be performed later, when the GIL is available |
| 71 | +function _defer_Py_DecRef(o::PyObject) |
| 72 | + lock(_deferred_queue_lock) |
| 73 | + try |
| 74 | + push!(_deferred_Py_DecRef, getfield(o, :o)) |
| 75 | + atomic_add!(_deferred_count, 1) |
| 76 | + finally |
| 77 | + unlock(_deferred_queue_lock) |
| 78 | + end |
| 79 | + return |
| 80 | +end |
| 81 | + |
| 82 | +# Defers a `PyBuffer_Release(o)` call to be performed later, when the GIL is available |
| 83 | +function _defer_PyBuffer_Release(o::PyBuffer) |
| 84 | + lock(_deferred_queue_lock) |
| 85 | + try |
| 86 | + push!(_deferred_PyBuffer_Release, o) |
| 87 | + atomic_add!(_deferred_count, 1) |
| 88 | + finally |
| 89 | + unlock(_deferred_queue_lock) |
| 90 | + end |
| 91 | + return |
| 92 | +end |
| 93 | + |
| 94 | +# Called at the end of every `@with_GIL` block, this performs any deferred destruction |
| 95 | +# operations from finalizers that could not be done immediately due to not holding the GIL |
| 96 | +@noinline function _drain_release_queues() |
| 97 | + lock(_deferred_queue_lock) |
| 98 | + |
| 99 | + atomic_sub!(_deferred_count, length(_deferred_Py_DecRef)) |
| 100 | + atomic_sub!(_deferred_count, length(_deferred_PyBuffer_Release)) |
| 101 | + |
| 102 | + Py_DecRefs = copy(_deferred_Py_DecRef) |
| 103 | + PyBuffer_Releases = copy(_deferred_PyBuffer_Release) |
| 104 | + |
| 105 | + empty!(_deferred_Py_DecRef) |
| 106 | + empty!(_deferred_PyBuffer_Release) |
| 107 | + |
| 108 | + unlock(_deferred_queue_lock) |
| 109 | + |
| 110 | + for o in Py_DecRefs |
| 111 | + ccall(@pysym(:Py_DecRef), Cvoid, (PyPtr,), o) |
| 112 | + end |
| 113 | + for o in PyBuffer_Releases |
| 114 | + ccall(@pysym(:PyBuffer_Release), Cvoid, (Ref{PyBuffer},), o) |
| 115 | + end |
| 116 | + |
| 117 | + return nothing |
| 118 | +end |
0 commit comments