Skip to content

Commit bf44e7b

Browse files
fix: don't try to close closed cursors (#498)
1 parent d5735ea commit bf44e7b

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

google/cloud/bigquery/dbapi/connection.py

+2-1
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

+16
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,22 @@ 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+
self.assertTrue(cursor_1._closed)
188+
cursor_1.close = mock.MagicMock()
189+
190+
connection.close()
191+
192+
self.assertFalse(cursor_1.close.called)
193+
self.assertTrue(cursor_2._closed)
194+
179195
def test_does_not_keep_cursor_instances_alive(self):
180196
from google.cloud.bigquery.dbapi import Cursor
181197

0 commit comments

Comments
 (0)