Skip to content

source: Fix source finalization in some corner cases

Benjamin Berg requested to merge benzea/gsource-fix2 into master

This is the simpler alternative to !221 (closed).

GLib will call the finalize handler when the GSource is destroyed. We already enforce destruction from del, however, the final reference might only be dropped later on because the main context (or something else) is holding another reference. The main case where this happens is if the source is pending at the time, e.g. if it is being destroyed while it is dispatching.

After destroy is called on the source, the only callback that can still be called is finalize. As such, we can simply call finalize immediately and rely on GLib cleaning up the underlying GSource at a later point.

Note that it can also happen that del is not called at all if the interpreter quits. In this case, neither del nor finalize will be called even though the underlying GSource may be finalized correctly. Compared to the previous behaviour this is a big improvement, as it would crash previously.

Merge request reports