Skip to content

Commit

Permalink
always pydecref on the main thread
Browse files Browse the repository at this point in the history
  • Loading branch information
marius311 committed Feb 26, 2021
1 parent 5d227fc commit dcbc9ee
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
15 changes: 13 additions & 2 deletions src/PyCall.jl
Expand Up @@ -76,8 +76,19 @@ mutable struct PyObject
o::PyPtr # the actual PyObject*
function PyObject(o::PyPtr)
po = new(o)
finalizer(pydecref, po)
return po
finalizer(_pydecref_eventually, po)
end
end

# we can only call pydecref from the main thread. if we happen to hit
# GC from a different thread, add the finalizer back to the finalization
# queue to try again later when we'll eventually be on the main thread.
function _pydecref_eventually(po)
if Threads.threadid()==1
return pydecref(po)
else
finalizer(_pydecref_eventually, po)
return nothing
end
end

Expand Down
2 changes: 1 addition & 1 deletion src/pybuffer.jl
Expand Up @@ -32,7 +32,7 @@ mutable struct PyBuffer
b = new(Py_buffer(C_NULL, PyPtr_NULL, 0, 0,
0, 0, C_NULL, C_NULL, C_NULL, C_NULL,
C_NULL, C_NULL, C_NULL))
finalizer(pydecref, b)
finalizer(_pydecref_eventually, b)
return b
end
end
Expand Down

0 comments on commit dcbc9ee

Please sign in to comment.