Skip to content

Commit dcbc9ee

Browse files
committed
always pydecref on the main thread
1 parent 5d227fc commit dcbc9ee

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

src/PyCall.jl

+13-2
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,19 @@ mutable struct PyObject
7676
o::PyPtr # the actual PyObject*
7777
function PyObject(o::PyPtr)
7878
po = new(o)
79-
finalizer(pydecref, po)
80-
return po
79+
finalizer(_pydecref_eventually, po)
80+
end
81+
end
82+
83+
# we can only call pydecref from the main thread. if we happen to hit
84+
# GC from a different thread, add the finalizer back to the finalization
85+
# queue to try again later when we'll eventually be on the main thread.
86+
function _pydecref_eventually(po)
87+
if Threads.threadid()==1
88+
return pydecref(po)
89+
else
90+
finalizer(_pydecref_eventually, po)
91+
return nothing
8192
end
8293
end
8394

src/pybuffer.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ mutable struct PyBuffer
3232
b = new(Py_buffer(C_NULL, PyPtr_NULL, 0, 0,
3333
0, 0, C_NULL, C_NULL, C_NULL, C_NULL,
3434
C_NULL, C_NULL, C_NULL))
35-
finalizer(pydecref, b)
35+
finalizer(_pydecref_eventually, b)
3636
return b
3737
end
3838
end

0 commit comments

Comments
 (0)