Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception in Destructor #591

Closed
DasCapschen opened this issue Jul 4, 2023 · 7 comments
Closed

Exception in Destructor #591

DasCapschen opened this issue Jul 4, 2023 · 7 comments

Comments

@DasCapschen
Copy link

When a Postgres Connection is deleted, it encounters an Exception in its Constructor, which is fortunately ignored.
Yet it is a little concerning, that the destructor does not finish successfully.

Code is approximately:

connection = create_postgres_connection()
db_migrator = Migrator(connection=connection)
db_migrator.migrate()
connection.close()
# script ends and destructor is called

The error message is:

Exception ignored in: <function BaseConnection.__del__ at 0x7f6c249bdbc0>
Traceback (most recent call last):
  File "/opt/maddox/.venv/lib/python3.11/site-packages/psycopg/connection.py", line 157, in __del__
  File "/opt/maddox/.venv/lib/python3.11/site-packages/psycopg/connection.py", line 164, in __repr__
AttributeError: 'NoneType' object has no attribute 'connection_summary'

Versions are:

  • Python 3.11.4
  • Psycopg 3.1.9
  • libpg 15.3 (Debian)
  • Postgres 15.2 (Alpine Docker Container)
@dvarrazzo
Copy link
Member

dvarrazzo commented Jul 10, 2023 via email

@DasCapschen
Copy link
Author

But the problem does not seem to be in the connection_summary function. Instead it already fails when trying to call the connection_summary function. Whatever Object that function is called on, it was already destroyed and does not exist anymore.

@dvarrazzo
Copy link
Member

dvarrazzo commented Jul 13, 2023 via email

@DasCapschen
Copy link
Author

Ah, now I understand what you mean. Seems like it is a Python bug then, if it GC's things that are still used in destructors...
Well okay... guess we'll just keep ignoring this then 😅

@dvarrazzo
Copy link
Member

dvarrazzo commented Jul 13, 2023 via email

dvarrazzo added a commit that referenced this issue Jul 23, 2023
The errors get ignored but print a warning on program exit and eclipse a
genuine warning.

The error is caused by the `pq.misc` module getting gc'd on interpreter
shutdown before `connection_summary()` is called. The solution is to
import `connection_summary` in the module namespace, which is similar to
the solution that proved working for #198. It is less robust than the
solution used by the Python devs to import the function in the method
via an argument default, but it should work adequately (as nobody
complained about #198 anymore).

In #591 discussion, I proposed that connection_summary is too complex to
be considered safe to call on __del__. Actually, looking at it, it seems
innocent enough, as it only calls objects methods, no functions from
module namespaces. As a consequence we assume that this commit fixes the
issue. As I can't reproduce it, will ask the OP if this is the case.

Close #591.
@dvarrazzo
Copy link
Member

A fix for this error will be released in 3.1.10.

@DasCapschen
Copy link
Author

Wow thank you! I will test this as soon as the release is out :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants