Skip to content

[P0][bug] ai_cancel only aborts the SDK session — in-flight MySQL query on the pool keeps running #311

Closed

Description

@EVWorth

Context

ai_service.cancel(conversation_id) calls entry.session.abort() which signals the Copilot SDK event loop. The session's event loop subscribes to SDK events but the query dispatched via executor.execute(...) inside handle_run_select_query / handle_run_query is on the connection pool — it's not in the SDK session's await tree, so aborting the SDK session does nothing to it. Cross-cites editor.md F1 (server-side cancellation is broken across the app). Audit finding F4 in docs/audits/ai.md.

Problem

For a SELECT SLEEP(120) invoked through AI, the user clicks Cancel, the SDK session aborts immediately, the spinner disappears, the chat finalizes — but the pool connection is still running the query for 119 more seconds. For run_query mutating transactions (Agent mode), an aborted session doesn't roll back an in-progress multi-statement transaction. The user-visible UX is better than the editor's bug because the SDK abort hides the symptom, but the server still pays the cost.

Files

  • src-tauri/crates/mas-ai/src/service.rs:559-572 (cancel)
  • src-tauri/src/commands/ai.rs:99-113 (ai_cancel)
  • src-tauri/crates/mas-ai/src/tools.rs:handle_run_select_query, handle_run_query

Repro

Static analysis + dynamic: dispatch a tool call that triggers a long SELECT SLEEP(60) via the SDK, click the Cancel button. SHOW PROCESSLIST from a separate connection still shows the SELECT SLEEP query running for ~60s after the spinner disappears.

Expected

Clicking Cancel issues KILL QUERY <conn_id> on the SQL pool connection within 1-2 seconds. The MySQL query is gone; the chat finalizes with an error.

Proposed fix

Scope M. (a) thread a tokio_util::sync::CancellationToken from ai_chat Tauri command down through service.rs::chat into the tool handlers (replace executor.execute(...) with a cancellable variant). (b) On ai_cancel, look up the active token per conversation_id and call token.cancel(), also issuing a KILL QUERY on a separate connection (per editor.md F1). (c) Tool handlers check the token and short-circuit before issuing SQL.

Acceptance

Start an Agent-mode query SELECT SLEEP(120), click Cancel within 2s; SELECT ID FROM information_schema.processlist WHERE INFO LIKE '%SLEEP%' returns no row; the pool connection isn't stuck; the ToolCallBlock surfaces "Cancelled by user" as the result.

Needs human verify

yes

Activity

  1. added
    audit
    severity/p0
    kind/bug
    area/ai
    on Jul 31, 2026
  2. EVWorth commented on Sep 12, 2026

    @EVWorth
    OwnerAuthor

    Closing as obsolete rather than fixed: this describes the implementation that ADR-011 replaces.

    The AI feature is being rebuilt around a decision made in #673 — SQLPilot does not call a model. It exposes the database, under its own policy, to whatever harness the user already runs and pays for. The mas-ai crate, its Copilot SDK session, its system prompt and its tool guards are all going.

    What that does to this issue specifically: still the right requirement, now met by the session model rather than by ai_cancel: cancelling a session issues KILL QUERY for anything in flight. Abandoning the future while the server keeps working is exactly what #658 turned out to be.

    The design is in docs/design/AI_INTEGRATION.md and ADR-011. If any part of this issue survives the redesign in a form I have missed, reopen it — I would rather re-argue it than lose it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/aiauditkind/bugseverity/p0

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions