[issue46256] Objects __del__ called after module have been removed

Pablo Galindo Salgado report at bugs.python.org
Thu Jan 6 08:55:40 EST 2022


Pablo Galindo Salgado <pablogsal at gmail.com> added the comment:

This doesn't look like a bug to me and is documented behavior:

https://docs.python.org/3/reference/datamodel.html#object.__del__

Check this paragraph:

Warning Due to the precarious circumstances under which __del__() methods are invoked, exceptions that occur during their execution are ignored, and a warning is printed to sys.stderr instead. In particular:
__del__() can be invoked when arbitrary code is being executed, including from any arbitrary thread. If __del__() needs to take a lock or invoke any other blocking resource, it may deadlock as the resource may already be taken by the code that gets interrupted to execute __del__().

__del__() can be executed during interpreter shutdown. As a consequence, the global variables it needs to access (including other modules) may already have been deleted or set to None. Python guarantees that globals whose name begins with a single underscore are deleted from their module before other globals are deleted; if no other references to such globals exist, this may help in assuring that imported modules are still available at the time when the __del__() method is called.

----

Here, the object __del__ should take s strong reference to whatever is going to use to ensure is available when needed. Indeed, the standard library does this regularly, for example:

https://github.com/python/cpython/blob/f4c03484da59049eb62a9bf7777b963e2267d187/Lib/asyncio/windows_utils.py#L110


I am closing it as "not a bug" but feel free to reopen if we think we missed something or I misinterpret the bug report.

----------
nosy: +pablogsal
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue46256>
_______________________________________


More information about the Python-bugs-list mailing list