FirebirdSQL / fdb

Firebird Driver for Python
https://www.firebirdsql.org/en/devel-python-driver/
Other
60 stars 26 forks source link

ReferenceError exception when closing weakly referenced cursor [PYFB81] #96

Closed firebird-automations closed 2 years ago

firebird-automations commented 4 years ago

Submitted by: Anders Munch (Flonidan) (ajm_flonidan)

Votes: 1

In have an application where I put database cursors in a weakref.WeakSet in order to track database activity. It works fine with other DBAPI modules such as pypyodbc. With fdb I sometimes get a traceback when closing a cursor that ends like this:

File "C:\flonidan\env\Python38-64\lib\site-packages\fdb\http://fbcore.py", line 3643, in close if is_dead_proxy(self._ps): File "C:\flonidan\env\Python38-64\lib\site-packages\fdb\http://fbcore.py", line 481, in is_dead_proxy return isinstance(obj, weakref.ProxyType) and not dir(obj) ReferenceError: weakly-referenced object no longer exists

The apparent cause is a the is_dead_proxy function (http://fbcore.py line 479), which mysteriously uses "not dir(obj)" to check for a dead proxy. Changing is_dead_proxy to catch ReferenceError seems to fix it:

def is_dead_proxy(obj): "Return True if object is a dead :func:`weakref.proxy`." try: return isinstance(obj, weakref.ProxyType) and not dir(obj) except ReferenceError: return True

firebird-automations commented 3 years ago

Commented by: Philipp Wojke (pw)

I have the same problem, done the same fix.

Maybe weakref.ProxyTypes instead of weakref.ProxyType should be used in the test, as there may be weakref proxies to callables.

The problem only occurs with python 3.8, not with 3.7 or 3.6. Seems that proxy handling has changed for 3.8, as this little test program shows: ------ import weakref class C: pass p = weakref.proxy(C()) print(dir(p)) ------ $ python3.7 http://test.py [] $ python3.8 http://test.py Traceback (most recent call last): File "http://test.py", line 5, in <module> print(dir(p)) ReferenceError: weakly-referenced object no longer exists

hmoffatt commented 2 years ago

I am having this problem with FDB used from SQL Alchemy now that I've upgraded to Python 3.9. Using @e-belair's fork fixes it.

pcisar commented 2 years ago

fix adopted.