Make cancelling a running query actually work #9

Merged
claude-bot merged 1 commit from feat/cancel-running-query into main 2026-11-20 13:49:25 +00:00
Collaborator

Problem

The toolbar Cancel button sent EngineCommand::ResetSession into the command channel. The worker thread is blocked in run_sql while a query runs, so the command only ran after the query had finished. The UI marked the query as stopped, the full result still arrived later, and then the session was reset for nothing.

Fix

  • Direct cancel: the worker publishes the running process in a shared RunningQuery { child, cancelled }. DuckDbClient::cancel_query() kills it from the UI thread, which unblocks run_sql. Only a query that is actually running can be cancelled. Queries still waiting in the queue are unaffected.
  • No soft interrupt: when its stdin is a pipe, the DuckDB CLI exits on Ctrl+C / SIGINT rather than interrupting just the query (tested against v2.0.0). So cancelling always restarts the session. The cancelled query reports CANCELLED_MESSAGE, which says temp tables, SET options and :memory: data are lost, and that data committed to a database file is kept.
  • Partial results: run_sql drops the partial chunk of the interrupted statement and keeps the results of statements that finished.
  • Respawn: the worker starts a fresh process right after a cancel, so the schema stays valid. It also respawns before the next query if the process died for any other reason (ActiveProcess::is_alive).
  • UI: a working toolbar Cancel, a Cancel button next to a new elapsed timer in the results pane ("Running query in DuckDB... 12.3 s"), and :cancel / :stop. Both buttons have a tooltip explaining the session restart.
  • Documented in AGENTS.md.

Testing

  • cargo test passes (32 tests).
  • New live test test_cancel_long_running_query_keeps_finished_statements runs against the real DuckDB CLI. It starts a minutes-long query after a quick statement, cancels it, and checks that the cancel finishes within 5s (about 0.3s in practice), that CANCELLED_MESSAGE is reported, that the finished statement's result is kept, and that the next query runs in the new session. It passed 8 of 8 repeated runs.
  • New test for the :cancel / :stop mapping.
  • Tested on Linux. On Windows, Child::kill uses TerminateProcess. Not yet checked by hand in the Windows GUI.

🤖 Generated with Claude Code

## Problem The toolbar **Cancel** button sent `EngineCommand::ResetSession` into the command channel. The worker thread is blocked in `run_sql` while a query runs, so the command only ran after the query had finished. The UI marked the query as stopped, the full result still arrived later, and then the session was reset for nothing. ## Fix - **Direct cancel:** the worker publishes the running process in a shared `RunningQuery { child, cancelled }`. `DuckDbClient::cancel_query()` kills it from the UI thread, which unblocks `run_sql`. Only a query that is actually running can be cancelled. Queries still waiting in the queue are unaffected. - **No soft interrupt:** when its stdin is a pipe, the DuckDB CLI exits on Ctrl+C / SIGINT rather than interrupting just the query (tested against v2.0.0). So cancelling always restarts the session. The cancelled query reports `CANCELLED_MESSAGE`, which says temp tables, `SET` options and `:memory:` data are lost, and that data committed to a database file is kept. - **Partial results:** `run_sql` drops the partial chunk of the interrupted statement and keeps the results of statements that finished. - **Respawn:** the worker starts a fresh process right after a cancel, so the schema stays valid. It also respawns before the next query if the process died for any other reason (`ActiveProcess::is_alive`). - **UI:** a working toolbar **Cancel**, a **Cancel** button next to a new elapsed timer in the results pane ("Running query in DuckDB... 12.3 s"), and `:cancel` / `:stop`. Both buttons have a tooltip explaining the session restart. - Documented in `AGENTS.md`. ## Testing - `cargo test` passes (32 tests). - New live test `test_cancel_long_running_query_keeps_finished_statements` runs against the real DuckDB CLI. It starts a minutes-long query after a quick statement, cancels it, and checks that the cancel finishes within 5s (about 0.3s in practice), that `CANCELLED_MESSAGE` is reported, that the finished statement's result is kept, and that the next query runs in the new session. It passed 8 of 8 repeated runs. - New test for the `:cancel` / `:stop` mapping. - Tested on Linux. On Windows, `Child::kill` uses `TerminateProcess`. Not yet checked by hand in the Windows GUI. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
The existing Cancel button queued a ResetSession command behind the
running query, so it did nothing until the query finished, then showed
the full result and reset the session anyway.

The worker now publishes the running DuckDB process, and the UI thread
kills it directly via DuckDbClient::cancel_query. The DuckDB CLI exits
on Ctrl+C when reading from a pipe, so a soft interrupt that keeps the
session isn't possible: cancelling restarts the session, and the
message says what is lost. Results of statements that finished before
the cancel are kept and the partial one is dropped. The worker starts a
fresh process right away, and also respawns before a query if the
process died for any other reason.

Cancel is available from the toolbar, a new button next to an elapsed
timer in the results pane, and :cancel / :stop.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude-bot deleted branch feat/cancel-running-query 2026-11-20 13:49:25 +00:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
daniel/qry-duck!9
No description provided.