FirebirdSQL / python3-driver

Firebird driver for Python that uses new Firebird API
https://www.firebirdsql.org/en/devel-python-driver/
MIT License
27 stars 10 forks source link

Unable to delete Connection #40

Open ant-zuev opened 3 weeks ago

ant-zuev commented 3 weeks ago

When a connection is lost, for example as a result of a server shutdown, after several attempts to execute statements, we cannot delete the Connection object. Reproduced with an uncommitted cursor transaction.

from firebird.driver import connect
import os

con = connect('localhost:employee', user='sysdba', password='masterkey')

cur = con.cursor()
cur.execute("select 1 from rdb$database")

os.system('pkill firebird')

con.execute_immediate("insert into test values(1)")
con.execute_immediate("insert into test values(1)")

try:
    con.close()
except Exception:
    pass

exit()

The object is not deleted and on exit we get an exception to delete the cursor and statement:

Exception ignored in: <function Cursor.__del__ at 0x716f1160f2e0>
Traceback (most recent call last):
  File "env/lib/python3.12/site-packages/firebird/driver/core.py", line 3047, in __del__
  File "env/lib/python3.12/site-packages/firebird/driver/core.py", line 3791, in close
  File "env/lib/python3.12/site-packages/firebird/driver/core.py", line 2736, in free
  File "env/lib/python3.12/site-packages/firebird/driver/interfaces.py", line 832, in free
  File "env/lib/python3.12/site-packages/firebird/driver/interfaces.py", line 113, in _check
firebird.driver.types.DatabaseError: connection shutdown
Exception ignored in: <function Statement.__del__ at 0x716f1160d940>
Traceback (most recent call last):
  File "env/lib/python3.12/site-packages/firebird/driver/core.py", line 2712, in __del__
  File "env/lib/python3.12/site-packages/firebird/driver/core.py", line 2736, in free
  File "env/lib/python3.12/site-packages/firebird/driver/interfaces.py", line 832, in free
  File "env/lib/python3.12/site-packages/firebird/driver/interfaces.py", line 113, in _check
firebird.driver.types.DatabaseError: connection shutdown
pcisar commented 17 hours ago

What do you mean you cannot delete the Connection object? It's in fact deleted just fine, as the stack trace confirms. The exception is reported for __del__ finalizer, which was called when connection went out of scope (so it never reached the con.close() otherwise it would be reported from there). Also note that this exception is ignored, just reported as it doesn't does not mean anything.