You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Mar 6, 2026. It is now read-only.
Repository navigation
This repository was archived by the owner on Mar 6, 2026. It is now read-only.
Closing a connection tries to close already closed cursors #497
When closing a connection the library will call .close() on every cursor created for that connection, including closed ones. While the code works, it produces a lot of logs of error level, polluting the logs.
Environment details
OS type and version: Mac OS 10.15.7
Python version: Python 3.8.2
pip version: pip 20.3.3
google-cloud-bigquery version: 2.7.0
Steps to reproduce
Close a cursor
Close the connection
Error log shows Exception closing connection <google.cloud.bigquery.dbapi.connection.Connection object at 0x...>
Code example
fromcontextlibimportclosing# using pybigquerywithclosing(engine.raw_connection()) asconn:
withclosing(conn.cursor()) ascursor:
cursor.execute(sql)
Stack trace
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool/base.py", line 270, in _close_connection
self._dialect.do_close(connection)
File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/default.py", line 549, in do_close
dbapi_connection.close()
File "/usr/local/lib/python3.7/site-packages/google/cloud/bigquery/dbapi/_helpers.py", line 258, in with_closed_check
return method(self, *args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/google/cloud/bigquery/dbapi/connection.py", line 79, in close
cursor_.close()
File "/usr/local/lib/python3.7/site-packages/google/cloud/bigquery/dbapi/_helpers.py", line 257, in with_closed_check
raise exc_class(exc_msg)
google.cloud.bigquery.dbapi.exceptions.ProgrammingError: Operating on a closed cursor.
changed the title [-]Closing a connection tries to close closed cursors[/-][+]Closing a connection tries to close already closed cursors[/+]on Jan 28, 2021
I can see where the issue is - the raise_on_closed() decorator makes all public methods throw if an instance is "closed", which includes the Cursor.close() method. Additionally, when closing a connection, the latter also attempts to automatically close all cursors that it created, thus it's not necessary to nest the closing() context managers:
However, there might be valid reasons to close a cursor early, and I agree that closing the connection at some later point should behave more gracefully, I'll fix that.
Edit: Ah, you already submitted a PR, even better, thanks!
Activity
[-]Closing a connection tries to close closed cursors[/-][+]Closing a connection tries to close already closed cursors[/+]plamut commentedon Jan 29, 2021
@betodealmeida Thanks for the report.
I can see where the issue is - the
raise_on_closed()decorator makes all public methods throw if an instance is "closed", which includes theCursor.close()method. Additionally, when closing a connection, the latter also attempts to automatically close all cursors that it created, thus it's not necessary to nest theclosing()context managers:However, there might be valid reasons to close a cursor early, and I agree that closing the connection at some later point should behave more gracefully, I'll fix that.
Edit: Ah, you already submitted a PR, even better, thanks!