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
Comments
It seems a problem similar to #198.
The `connection_summary` function is pretty complex and I wouldn't try to
create all the closures to make it work at destruction time because it's
untestable. If any I'd try to make repr work, in a degenerate way, on
AttributeError.
…On Tue, 4 Jul 2023, 13:24 Dominik Waurenschk, ***@***.***> wrote:
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)
—
Reply to this email directly, view it on GitHub
<#591>, or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AABQWBOTRUOGN4PH23X4SZ3XOP4N3ANCNFSM6AAAAAAZ5TC6SQ>
.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
|
But the problem does not seem to be in the |
Yes, what I meant is that, when the problem was encountered the first time,
for instance with an attribute error on `os.getpid`, the solution was "just
store the `getpid` function in a local var".
This works for simple atomic functions such as getpid, implemented in c and
executed in a single python opcode, but I doubt it would work for
connection_summary: it is likely, and not reliably testable, that an
attribute error might get raised in its body because of some module already
gc'ed.
|
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... |
No, not necessarily, we can make it more robust. We can manage an error on
repr() called within del() for instance, and provide a simplified error
message. For instance, untested:
```
try:
r = repr(self)
except Exception:
r = object.repr(self) # does it work?
warn(f"connection {r} was deleted...
```
If you have a somewhat repeatable test case, it would be actually nice if
you could test it and provide a MR for this and other occurrences of
messages raised in del.
|
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.
A fix for this error will be released in 3.1.10. |
Wow thank you! I will test this as soon as the release is out :) |
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:
The error message is:
Versions are:
The text was updated successfully, but these errors were encountered: