-
Notifications
You must be signed in to change notification settings - Fork 2
fix: Monkeypatch BigQuery library so KeyboardInterrupt cancels active query #34
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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
OlegWock
merged 6 commits into
main
from
oleh/blu-5131-moon-active-cancelling-query-in-deepnote-does-not-cancel
Nov 19, 2025
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
13fd0ee
fix: Monkeypatch BigQuery library so KeyboardInterrupt cancels active…
OlegWock 6c748d4
Formatting
OlegWock d5779b0
Update deepnote_toolkit/sql/sql_execution.py
OlegWock 098b161
Address PR review comments
OlegWock 8137ef0
Merge branch 'oleh/blu-5131-moon-active-cancelling-query-in-deepnote-…
OlegWock c896b7b
Rename fixture
OlegWock File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| from typing import Any, Optional, Union | ||
|
|
||
| from deepnote_toolkit.logging import LoggerManager | ||
|
|
||
| logger = LoggerManager().get_logger() | ||
|
|
||
|
|
||
| # TODO(BLU-5171): Temporary hack to allow cancelling BigQuery jobs on KeyboardInterrupt (e.g. when user cancels cell execution) | ||
| # Can be removed once | ||
| # 1. https://github.com/googleapis/python-bigquery/pull/2331 is merged and released | ||
| # 2. Dependencies updated for the toolkit. We don't depend on google-cloud-bigquery directly, but it's transitive | ||
| # dependency through sqlalchemy-bigquery | ||
| def _monkeypatch_bigquery_wait_or_cancel(): | ||
| try: | ||
| import google.cloud.bigquery._job_helpers as _job_helpers | ||
| from google.cloud.bigquery import job, table | ||
|
|
||
| def _wait_or_cancel( | ||
| job_obj: job.QueryJob, | ||
| api_timeout: Optional[float], | ||
| wait_timeout: Optional[Union[object, float]], | ||
| retry: Optional[Any], | ||
| page_size: Optional[int], | ||
| max_results: Optional[int], | ||
| ) -> table.RowIterator: | ||
| try: | ||
| return job_obj.result( | ||
| page_size=page_size, | ||
| max_results=max_results, | ||
| retry=retry, | ||
| timeout=wait_timeout, | ||
| ) | ||
| except (KeyboardInterrupt, Exception): | ||
| try: | ||
| job_obj.cancel(retry=retry, timeout=api_timeout) | ||
| except (KeyboardInterrupt, Exception): | ||
| pass | ||
| raise | ||
|
|
||
| _job_helpers._wait_or_cancel = _wait_or_cancel | ||
| logger.debug( | ||
| "Successfully monkeypatched google.cloud.bigquery._job_helpers._wait_or_cancel" | ||
| ) | ||
| except ImportError: | ||
| logger.warning( | ||
| "Could not monkeypatch BigQuery _wait_or_cancel: google.cloud.bigquery not available" | ||
| ) | ||
| except Exception as e: | ||
| logger.warning("Failed to monkeypatch BigQuery _wait_or_cancel: %s", repr(e)) | ||
|
|
||
|
|
||
| def apply_runtime_patches(): | ||
| _monkeypatch_bigquery_wait_or_cancel() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.