-
Notifications
You must be signed in to change notification settings - Fork 3
fix(trino): Monkeypatch TrinoQuery to cancel on KeyboardInterrupt #63
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
fix(trino): Monkeypatch TrinoQuery to cancel on KeyboardInterrupt #63
Conversation
📝 WalkthroughWalkthroughA new private monkeypatch was added to intercept Trino query execution and fetching. The wrappers call the original TrinoQuery.execute/fetch, and if any exception (including KeyboardInterrupt) occurs they invoke the query's cancel() before re-raising the original exception. The patch is applied during runtime initialization alongside the BigQuery patch. Unit tests validate cancellation behavior and that cancel failures do not suppress the original exception. Sequence Diagram(s)sequenceDiagram
participant Caller
participant Runtime as RuntimePatches
participant TrinoQuery
participant TrinoClient as HTTPClient
participant TrinoServer
Caller->>TrinoQuery: call execute()/fetch()
Note right of TrinoQuery: monkeypatched wrapper installed by RuntimePatches
TrinoQuery->>TrinoQuery: call original execute/fetch
TrinoQuery--xCaller: exception (e.g., KeyboardInterrupt)
Note right of TrinoQuery: wrapper catches exception
TrinoQuery->>TrinoQuery: invoke cancel()
TrinoQuery->>TrinoClient: send cancel request
TrinoClient->>TrinoServer: HTTP cancel
TrinoServer-->>TrinoClient: cancel response
TrinoClient-->>TrinoQuery: cancel result
TrinoQuery-->>Caller: re-raise original exception
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Comment |
|
📦 Python package built successfully!
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 3
🤖 Fix all issues with AI agents
In `@deepnote_toolkit/runtime_patches.py`:
- Line 8: The function _monkeypatch_trino_cancel_on_error lacks an explicit
return type; update its signature to include the proper return type annotation
(e.g., -> None if it returns nothing) so it follows the coding guidelines;
locate the function definition named _monkeypatch_trino_cancel_on_error and
append the correct return type annotation and adjust any internal logic/return
statements to match that type.
- Line 1: Replace legacy typing imports and annotations: remove Any, Dict, List,
Optional, Union from the import line and update all type annotations in
deepnote_toolkit/runtime_patches.py to use modern Python 3.9+ syntax (use
dict[...] instead of Dict, list[...] instead of List, X | Y instead of Union[X,
Y], and T | None instead of Optional[T]); search for usages of Any, Dict, List,
Optional, Union in the file and convert each annotation (including function
signatures and variable annotations) to the built-in generic types and PEP 604
union syntax, leaving runtime behavior unchanged.
- Around line 27-28: The except block "except (KeyboardInterrupt, Exception):
pass" in runtime_patches.py silently swallows cancel failures; replace the pass
with a debug log call that records the exception details (e.g., logger.debug or
appropriate module logger) including the exception message/stack (use
exc_info=True or equivalent) so cancel errors are visible for troubleshooting
while still not raising them; update the except handling where "except
(KeyboardInterrupt, Exception):" appears to log and move on.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #63 +/- ##
==========================================
+ Coverage 73.28% 73.36% +0.08%
==========================================
Files 93 93
Lines 5199 5227 +28
Branches 757 758 +1
==========================================
+ Hits 3810 3835 +25
- Misses 1147 1149 +2
- Partials 242 243 +1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
|
🚀 Review App Deployment Started
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
🤖 Fix all issues with AI agents
In `@deepnote_toolkit/runtime_patches.py`:
- Line 67: Add a docstring to the function _monkeypatch_bigquery_wait_or_cancel
describing its purpose (monkey-patch BigQuery client to modify wait_or_cancel
behavior), noting that it takes no parameters and returns None, and include
brief details about side-effects (which module/class it patches and why) so it
follows project docstring guidelines for deepnote_toolkit functions.
In `@tests/unit/test_sql_execution_internal.py`:
- Around line 33-99: Add a one-line docstring and an explicit return type hint
"-> None" to each test function:
test_trino_execute_cancels_on_keyboard_interrupt,
test_trino_fetch_cancels_on_keyboard_interrupt, and
test_trino_handles_cancel_failure_gracefully. Update each function signature to
include "-> None" and add a brief docstring describing the test's intent
immediately after the def line (e.g., "Verify TrinoQuery cancels on
KeyboardInterrupt"). Ensure no behavior changes beyond the added docstrings and
type hints.
|
Closing in favor of #64 |
This patch adds monkeypatching of
TrinoQueryobject to cancel query if exception (ExceptionorKeyboardInterrupt) raised during waiting for results. This allows for cancelling queries when cell execution is cancelled, in line with what we do for BigQuery.Here is example query that takes long time to execute. You can run it in RA using "Trino Basic Auth" integration and check query status by visiting address from the integration settings in browser.
Example query
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.