This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Objects __del__ called after module have been removed
Type: Stage: resolved
Components: Interpreter Core Versions: Python 3.10
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Dennis Sweeney, cvickery, pablogsal, piro
Priority: normal Keywords:

Created on 2022-01-04 14:41 by piro, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
format_rules.py cvickery, 2022-01-04 15:15 Module causes the problem with Python 3.10, but not 3.9
format_rules_annotated.py cvickery, 2022-01-05 23:11 If lines 66-69 are omitted, the problem does not occur.
Messages (7)
msg409684 - (view) Author: Daniele Varrazzo (piro) * Date: 2022-01-04 14:41
The following bug has been reported to Psycopg:

https://github.com/psycopg/psycopg/issues/198

At the end of the program, errors such as the following are dumped:

    Exception ignored in: <function PGconn.__del__ at 0x1021bbb50>
    Traceback (most recent call last):
      File "/opt/homebrew/lib/python3.10/site-packages/psycopg/pq/pq_ctypes.py", line 91, in __del__
    TypeError: 'NoneType' object is not callable

Where `None` is some module-level objects, such as `os.getpid`.

Unfortunately I don't have a full repro. The error seems only happening in Python 3.10.
msg409687 - (view) Author: Christopher Vickery (cvickery) Date: 2022-01-04 15:15
If the attached module is run with no command line arguments, it will reproduce the problem without doing anything else. When run using Python 3.9 it does not cause the problem. (This module comes from https://github.com/cvickery/transfer-app, with an unnecessary import commented out.)
msg409800 - (view) Author: Dennis Sweeney (Dennis Sweeney) * (Python committer) Date: 2022-01-05 19:26
I'm getting this:

Traceback (most recent call last):
  File "/mnt/c/Users/sween/Source/Repos/cpython2/cpython/delbug.py", line 65, in <module>
    conn = psycopg.connect('dbname=cuny_curriculum')
  File "/home/sween/.local/lib/python3.10/site-packages/psycopg/connection.py", line 572, in connect
    raise ex.with_traceback(None)
psycopg.OperationalError: connection is bad: No such file or directory
        Is the server running locally and accepting connections on that socket?

Is there a self-contained way to reproduce the issue without a DB up and running? Or to somehow shrink the reproducer down to its cpython-relevant bare essentials?
msg409810 - (view) Author: Christopher Vickery (cvickery) Date: 2022-01-05 23:11
The app has to open a db connection during module initialization to trigger the problem, so I can't provide a self-contained way to reproduce the problem.

The closest I can come is the attached file, format_rules_annotated.py.

With lines 66-69 in place, the problem occurs, even if there are no command line arguments, with Python 3.10, but not with Python 3.9. If I comment out lines 66-69 there is no problem with either 3.10 or 3.9, even if I add a command line argument that causes connections to be opened within functions defined within the module. (I added the explicit initialization of the institution_names dict so I could test those function calls without accessing the db during module initialization.)

FWIW, here is an execution under 3.10 with lines 66-69 commented out:

$ python -m format_rules_annotated -r QCC01:QNS01:PH:35
QCC01:QNS01:PH:35
C- or above in PH 101 at Queensborough, 4.0 credits, transfers to Queens as PHYS-11 and PHYS-14, 4.0 credits.
[No "Exception ignored" message appears here because db was not accessed during module initialization]
$
msg409847 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2022-01-06 13:55
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.
msg409849 - (view) Author: Christopher Vickery (cvickery) Date: 2022-01-06 14:24
This it very clear and totally consistent with the observed behavior. I can think of a couple of workarounds for my app (move the db access from module initialization to a function that gets invoked "on demand", or redirect sys.stderr to /dev/null just before exiting).

Perhaps the Psycopg team can deal with it more directly.

Many thanks to all for the time and effort taken to make clear what's happening!
msg409864 - (view) Author: Daniele Varrazzo (piro) * Date: 2022-01-06 15:49
Thank you @pablogsal. For me it was surprising as never seen before 3.10 instance.

If this is the expected behaviour (and the stdlib expects it) we can try and take the same type of care in psycopg.
History
Date User Action Args
2022-04-11 14:59:54adminsetgithub: 90414
2022-01-06 15:49:36pirosetmessages: + msg409864
2022-01-06 14:24:34cvickerysetmessages: + msg409849
2022-01-06 13:55:40pablogsalsetstatus: open -> closed

nosy: + pablogsal
messages: + msg409847

resolution: not a bug
stage: resolved
2022-01-05 23:11:47cvickerysetfiles: + format_rules_annotated.py

messages: + msg409810
2022-01-05 19:26:27Dennis Sweeneysetnosy: + Dennis Sweeney
messages: + msg409800
2022-01-04 15:15:05cvickerysetfiles: + format_rules.py
nosy: + cvickery
messages: + msg409687

2022-01-04 14:41:43pirocreate