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

deepcopy(::PyObject) can yield segfault #757

Open
tkf opened this issue Mar 26, 2020 · 1 comment
Open

deepcopy(::PyObject) can yield segfault #757

tkf opened this issue Mar 26, 2020 · 1 comment

Comments

@tkf
Copy link
Member

tkf commented Mar 26, 2020

I think we need to overload deepcopy_internal(::PyObject, ...) and increment the refcount in it?

julia> using PyCall

julia> a = pycall(pyimport("numpy").zeros, PyObject, 10000)
PyObject array([0., 0., 0., ..., 0., 0., 0.])

julia> pycall(pyimport("sys").getrefcount, Int, a)
2

julia> b = deepcopy(a)
PyObject array([0., 0., 0., ..., 0., 0., 0.])

julia> pycall(pyimport("sys").getrefcount, Int, a)
2

julia> pycall(pyimport("sys").getrefcount, Int, b)  # creating b did not incref
2

julia> pycall(pyimport("builtins").id, Int, a)
140671027744560

julia> pycall(pyimport("builtins").id, Int, b)  # just to show that b _is_ the same Python object
140671027744560

julia> a = nothing

julia> GC.gc(); GC.gc(); GC.gc(); GC.gc(); GC.gc(); GC.gc(); GC.gc()

julia> pycall(pyimport("builtins").id, Int, b)
140671027744560

julia> pycall(pyimport("sys").getrefcount, Int, b)  # now `PyPtr(b)` is pointing nowhere
140671027744561

julia> b  # sometime segfault doesn't happen immediately
PyObject ''uiltin_function_or_method'

julia> pyimport("gc").collect()

signal (11): Segmentation fault
in expression starting at REPL[14]:1
pymalloc_alloc at /tmp/python-build.20200119182925.141496/Python-3.7.6/Objects/obmalloc.c:1420 [inlined]
pymalloc_alloc at /tmp/python-build.20200119182925.141496/Python-3.7.6/Objects/obmalloc.c:1386 [inlined]
_PyObject_Malloc at /tmp/python-build.20200119182925.141496/Python-3.7.6/Objects/obmalloc.c:1578
_PyObject_GC_Alloc at /tmp/python-build.20200119182925.141496/Python-3.7.6/Modules/gcmodule.c:1693 [inlined]
_PyObject_GC_Malloc at /tmp/python-build.20200119182925.141496/Python-3.7.6/Modules/gcmodule.c:1715
_PyObject_GC_New at /tmp/python-build.20200119182925.141496/Python-3.7.6/Modules/gcmodule.c:1727
@stevengj
Copy link
Member

Sounds right.

jw3126 added a commit to jw3126/PyCall.jl that referenced this issue Jun 9, 2023
stevengj added a commit that referenced this issue Jun 21, 2023
* disable deepcopy on PyObject #757

* Update src/PyCall.jl

Co-authored-by: Steven G. Johnson <stevenj@mit.edu>

* fix deepcopy and add tests

* add another deepcopy test

* v1.95.2

* Update Project.toml

* Update test/runtests.jl

---------

Co-authored-by: Steven G. Johnson <stevenj@mit.edu>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants