JuliaPy / JuliaPy/PyCall.jl

`deepcopy(::PyObject)` can yield segfault

Abierto
#757 1 comentario 0 reacciones 0 asignados Ver en GitHub
Lenguaje dominante
Julia
Estrellas
1.5k
Forks
186
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

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

```julia
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
```

Guía de contribución

No hay ninguna guía de contribución indexada para este repositorio

Línea de trabajo

Reproduce the failure with the Julia/PyCall and Python REPL sequence in the issue, then inspect the deepcopy_internal(::PyObject, ...) path and PyObject reference-count handling. Verify that deepcopy keeps the Python object alive after the original is released and garbage collection runs without a segmentation fault.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
python
Área
api
Tipo de issue
Error
Dificultad
3/5
Tiempo estimado
1-2 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
35/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.