Skip to content

Commit 54f7e61

Browse files
committed
fix: close unfetched async commands
Signed-off-by: Nanook <nanookclaw@users.noreply.github.com>
1 parent c3cae63 commit 54f7e61

2 files changed

Lines changed: 34 additions & 3 deletions

File tree

src/databricks/sql/client.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1713,9 +1713,13 @@ def cancel(self) -> None:
17131713
def close(self) -> None:
17141714
"""Close cursor"""
17151715
self.open = False
1716-
self.active_command_id = None
1717-
if self.active_result_set:
1718-
self._close_and_clear_active_result_set()
1716+
try:
1717+
if self.active_result_set:
1718+
self._close_and_clear_active_result_set()
1719+
elif self.active_command_id is not None:
1720+
self.backend.close_command(self.active_command_id)
1721+
finally:
1722+
self.active_command_id = None
17191723

17201724
@property
17211725
def query_id(self) -> Optional[str]:

tests/unit/test_client.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,33 @@ def test_context_manager_closes_cursor(self):
280280
cursor.close = mock_close
281281
mock_close.assert_called_once_with()
282282

283+
def test_close_closes_active_command_without_result_set(self):
284+
mock_backend = Mock()
285+
mock_command_id = Mock(spec=CommandId)
286+
cursor = client.Cursor(Mock(), mock_backend)
287+
cursor.active_command_id = mock_command_id
288+
289+
cursor.close()
290+
291+
mock_backend.close_command.assert_called_once_with(mock_command_id)
292+
self.assertIsNone(cursor.active_command_id)
293+
self.assertIsNone(cursor.active_result_set)
294+
295+
def test_close_delegates_to_active_result_set_without_double_closing_command(self):
296+
mock_backend = Mock()
297+
mock_result_set = Mock()
298+
mock_command_id = Mock(spec=CommandId)
299+
cursor = client.Cursor(Mock(), mock_backend)
300+
cursor.active_result_set = mock_result_set
301+
cursor.active_command_id = mock_command_id
302+
303+
cursor.close()
304+
305+
mock_result_set.close.assert_called_once_with()
306+
mock_backend.close_command.assert_not_called()
307+
self.assertIsNone(cursor.active_command_id)
308+
self.assertIsNone(cursor.active_result_set)
309+
283310
def dict_product(self, dicts):
284311
"""
285312
Generate cartesion product of values in input dictionary, outputting a dictionary

0 commit comments

Comments
 (0)