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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't try to close closed DB API cursors #498

Merged
merged 1 commit into from Jan 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion google/cloud/bigquery/dbapi/connection.py
Expand Up @@ -76,7 +76,8 @@ def close(self):
self._bqstorage_client._transport.grpc_channel.close()

for cursor_ in self._cursors_created:
cursor_.close()
if not cursor_._closed:
cursor_.close()

def commit(self):
"""No-op, but for consistency raise an error if connection is closed."""
Expand Down
16 changes: 16 additions & 0 deletions tests/unit/test_dbapi_connection.py
Expand Up @@ -176,6 +176,22 @@ def test_close_closes_all_created_cursors(self):
self.assertTrue(cursor_1._closed)
self.assertTrue(cursor_2._closed)

def test_close_closes_only_open_created_cursors(self):
connection = self._make_one(client=self._mock_client())
cursor_1 = connection.cursor()
cursor_2 = connection.cursor()
self.assertFalse(cursor_1._closed)
self.assertFalse(cursor_2._closed)

cursor_1.close()
self.assertTrue(cursor_1._closed)
cursor_1.close = mock.MagicMock()

connection.close()

self.assertFalse(cursor_1.close.called)
self.assertTrue(cursor_2._closed)

def test_does_not_keep_cursor_instances_alive(self):
from google.cloud.bigquery.dbapi import Cursor

Expand Down