Skip to content

Commit 5904b52

Browse files
committed
fix: don't try to close closed cursors
1 parent d5735ea commit 5904b52

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

google/cloud/bigquery/dbapi/connection.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ def close(self):
7676
self._bqstorage_client._transport.grpc_channel.close()
7777

7878
for cursor_ in self._cursors_created:
79-
cursor_.close()
79+
if not cursor_._closed:
80+
cursor_.close()
8081

8182
def commit(self):
8283
"""No-op, but for consistency raise an error if connection is closed."""

tests/unit/test_dbapi_connection.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,19 @@ def test_close_closes_all_created_cursors(self):
176176
self.assertTrue(cursor_1._closed)
177177
self.assertTrue(cursor_2._closed)
178178

179+
def test_close_closes_only_open_created_cursors(self):
180+
connection = self._make_one(client=self._mock_client())
181+
cursor_1 = connection.cursor()
182+
cursor_2 = connection.cursor()
183+
self.assertFalse(cursor_1._closed)
184+
self.assertFalse(cursor_2._closed)
185+
186+
cursor_1.close()
187+
connection.close()
188+
189+
self.assertTrue(cursor_1._closed)
190+
self.assertTrue(cursor_2._closed)
191+
179192
def test_does_not_keep_cursor_instances_alive(self):
180193
from google.cloud.bigquery.dbapi import Cursor
181194

0 commit comments

Comments
 (0)