Skip to content

Commit e4b45f2

Browse files
committed
fix: Handle KeyboardInterrupt in _wait_or_cancel
1 parent 68e915f commit e4b45f2

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

google/cloud/bigquery/_job_helpers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -720,11 +720,11 @@ def _wait_or_cancel(
720720
)
721721
)
722722
return query_results
723-
except Exception:
723+
except (KeyboardInterrupt, Exception):
724724
# Attempt to cancel the job since we can't return the results.
725725
try:
726726
job.cancel(retry=retry, timeout=api_timeout)
727-
except Exception:
727+
except (KeyboardInterrupt, Exception):
728728
# Don't eat the original exception if cancel fails.
729729
pass
730730
raise

tests/unit/test__job_helpers.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,3 +1162,30 @@ def test_wait_or_cancel_exception_raises_original_exception():
11621162
timeout=123,
11631163
retry=retry,
11641164
)
1165+
1166+
1167+
def test_wait_or_cancel_keyboard_interrupt_cancels_job():
1168+
job = mock.create_autospec(job_query.QueryJob, instance=True)
1169+
job.result.side_effect = KeyboardInterrupt()
1170+
retry = retries.Retry()
1171+
1172+
with pytest.raises(KeyboardInterrupt):
1173+
_job_helpers._wait_or_cancel(
1174+
job,
1175+
api_timeout=123,
1176+
wait_timeout=456,
1177+
retry=retry,
1178+
page_size=789,
1179+
max_results=101112,
1180+
)
1181+
1182+
job.result.assert_called_once_with(
1183+
timeout=456,
1184+
retry=retry,
1185+
page_size=789,
1186+
max_results=101112,
1187+
)
1188+
job.cancel.assert_called_once_with(
1189+
timeout=123,
1190+
retry=retry,
1191+
)

0 commit comments

Comments
 (0)