Hi,

Recently I've been facing a really weird bug where a Python program
was randomly segfaulting during the finalization, the program was
using some C extensions via Cython.

Debugging the issue I realized that during the deallocation of one of
the Python objects the deallocation function was trying to release a
pointer that was surprisingly assigned to  NULL. The pointer was at
the same time held by another Python object that was an attribute of
the Python object that had the deallocation function, something like
this:

class Foo:
    my_type * value

class Bar
    def __cinit__:
        self._foo = Foo()
        self._foo->value = initialize()

    def __dealloc__:
        destroy(self._foo->value)

Seems that randomly the instance of the object Foo held by the Bar
object was deallocated by the CPython interpreter before the Foo
deallocation, so after being deallocated - and zeroing the memory
space of the instance of Foo - the execution of the
`destroy(self._foo->value)` was in fact given as a parameter a NULL
address and raising a segfault.

It was a surprise for me, If I'm not missing something the
deallocation of the Foo instance happened even though there was still
an active reference held by the Bar object.

As a kind of double-checking I changed the program for making an
explicit `gc.collect()` before the last line of the Python program. As
a result, I couldn't reproduce the segfault, which theoretically would
mean that objects were deallocated "in order".

So my question would be, could CPython deallocate the objects during
the finalization step without considering the dependencies between
objects?

If this is not the right list to make this kind of questions, just let
me know what would be the best place for making this kind of questions

Thanks in advance,
-- 
--pau
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/EQMJ2EZRI3QMGHOOWN7JVYVK7ZMIM2IP/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to