Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make GC more thread-safe #883

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/PyCall.jl
Expand Up @@ -76,11 +76,22 @@ 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
pydecref(po)
else
finalizer(_pydecref_eventually, po)
stevengj marked this conversation as resolved.
Show resolved Hide resolved
end
return nothing
end

PyPtr(o::PyObject) = getfield(o, :o)

"""
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