Skip to content

source: Fix destruction in certain corner cases

Benjamin Berg requested to merge benzea/gsource-fix into master
The GSource destructor assumed that __del__ would clean up the object
and also cause finalize to be called. However, this may not be true in
two scenarios:
 1. On abnormal exit, __del__ might not be called
 2. The source is pending (or being dispatched)
    as the context holds a reference

The fix used here is to make sure to prevent calling the finalize
handler if the object is already destroyed or is being destroyed.

In the abnormal exit case (1), this is reasonable to do. The behaviour
is more problematic for the case where destruction happens while the
source is pending (2). In that case, a custom finalize handler will not
be called as the corresponding python object will be destroyed already.

It is a funny caveat that finalize will not be called. But fixing that is more complicated unfortunately.

i.e. one would need to make sure the python object is not destroyed until the finalize handler was called. I suppose, the easiest way would be to take a reference in __del__ (i.e. resurrect the object) if finalize did not happen. Then, if it does happen later, destroy the object again. But, that would likely require untangling GSource more from the boxed types.

Edited by Benjamin Berg

Merge request reports