Skip to content

fix(explain): stop EXPLAIN ANALYZE running writes, and make timeouts real - #512

Merged
EVWorth merged 2 commits into
mainfrom
fix/explain-analyze-safety-and-timeout
Aug 28, 2026
Merged

EVWorth merged 2 commits into
mainfrom
fix/explain-analyze-safety-and-timeout

Conversation

@EVWorth

@EVWorth EVWorth commented Aug 28, 2026

Copy link
Copy Markdown
Owner

Closes #412, closes #418, closes #420.

First of three PRs bringing area/explain up to scratch. This one is the safety group; rendering correctness (#422, #423, #426) and polish (#425, #427) follow. #424 (FORMAT=JSON/TREE) stays open as a separate feature.

The problem

EXPLAIN ANALYZE executes the statement it measures. The UI offered it from the same split button as the inert EXPLAIN, so EXPLAIN ANALYZE DELETE FROM users WHERE 1=1 wiped the table — no confirmation, no read-only check, no way to stop it.

Separately, query_timeout_secs was persisted on the profile and enforced nowhere, and the Cancel button incremented a JS counter that made the client ignore the answer while the server kept working.

What changed

A backend home for EXPLAIN — a new explain_query command, so the safety decision can't be bypassed by a second call site (this is what #412 proposes, and closes the explain_query doc-drift noted as F11 in #411).

  • ANALYZE is gated on an allowlist of leading verbs (SELECT/WITH/TABLE/VALUES) rather than a blocklist of dangerous ones — an unrecognised verb downgrades to a plain EXPLAIN, which is the harmless direction. A blocklist fails the other way: anything it forgets gets executed.
  • A read-only profile refuses ANALYZE outright. read_only was stored and never enforced anywhere; it now reaches the executor via the live connection.
  • Refusals come back as a typed reason and render as a notice above the plan, so absent timings read as deliberate rather than broken.
  • A production connection confirms before ANALYZE regardless of the statement — a SELECT still really runs.
  • Statement normalization reuses the existing splitter, which drops the trailing ; that made EXPLAIN ANALYZE SELECT 1; a syntax error and rejects a multi-statement script with a sentence instead of ERROR 1064.

Real timeouts and cancellation — every statement batch now leads with SELECT CONNECTION_ID(). It rides in the same COM_QUERY (no extra round trip) and the pool holds one connection for the whole batch, so the executor knows which server thread to KILL QUERY from a second pool connection when the deadline passes or the user cancels.

This also fixes the root cause behind editor F1 (cancel is a no-op) and F3 (timeout unenforced) for all query paths, not just EXPLAIN.

Verification

Unit tests cover normalization and the verb allowlist. Because the interesting claims are about what the server does, there's also explain_integration.rs against MySQL 8:

  • the DELETE canary still has its 3 rows after EXPLAIN ANALYZE DELETE ... WHERE 1=1
  • EXPLAIN ANALYZE SELECT ...; with the trailing semicolon succeeds
  • a 2s timeout on SELECT SLEEP(30) returns promptly and leaves nothing in information_schema.PROCESSLIST — the part that separates a real timeout from dropping the future
  • cancel cuts SLEEP(30) short, and the session stays usable afterwards

All 1606 frontend tests and the Rust suite pass; clippy/tsc/dprint clean.

Notes for review

  • MySQL does not error an interrupted SLEEP() — it returns 1 early — so the cancel test asserts on elapsed time rather than an error. The client discards the result either way via the cancel generation.
  • Confirmation copy lives in AppLayout rather than the store; only the kind discriminator crosses the boundary.
  • One thing left alone deliberately: executor.rs appends LIMIT to anything starting with EXPLAIN, which would rewrite the statement being planned. It's dormant because the explain path passes no limit, and fixing it belongs with the row-limit code rather than here.

🤖 Generated with Claude Code

…real

EXPLAIN ANALYZE executes the statement it measures, so EXPLAIN ANALYZE
DELETE FROM users deleted the users. The UI offered it from the same split
button as the inert EXPLAIN, with no confirmation and no way to stop it.

Move EXPLAIN behind its own explain_query command so the safety decision
cannot be skipped by a second call site:

  * ANALYZE is allowed only for statements whose execution has no side
    effects, as an allowlist of leading verbs — an unrecognised verb
    downgrades to a plain EXPLAIN, which is the harmless direction. A
    read-only profile refuses ANALYZE outright. Either refusal comes back
    as a typed reason and renders as a notice above the plan, so the
    missing timings read as deliberate. (#412)

  * The statement is normalized server-side with the existing splitter,
    which drops the trailing semicolon that made EXPLAIN ANALYZE SELECT 1;
    a syntax error, and rejects a multi-statement script with a sentence
    instead of ERROR 1064. (#418)

  * A production connection confirms before ANALYZE regardless of the
    statement — a SELECT still really runs.

Timeouts and cancel were both cosmetic: query_timeout_secs was persisted
and never enforced, and cancel bumped a JS counter that only made the
client ignore the answer while the server kept working. Every statement
batch now leads with SELECT CONNECTION_ID() — same COM_QUERY, no extra
round trip — so the executor can KILL QUERY the thread from a second pool
connection when the deadline passes or the user cancels. (#420)

Verified against MySQL 8: the DELETE canary keeps its rows, a 2s timeout
on SELECT SLEEP(30) returns promptly and leaves nothing in the
processlist, and cancel cuts SLEEP(30) short.

Refs #412, #418, #420

Co-Authored-By: Claude Opus 5 <[email protected]>
Review of the safety work turned up a hole in the safety work. `WITH doomed
AS (SELECT id FROM t) DELETE FROM t WHERE id IN (SELECT id FROM doomed)` is
valid MySQL 8 and deletes the rows, but the ANALYZE allowlist matched on the
leading keyword, saw WITH, and let EXPLAIN ANALYZE execute it — the same
failure #412 exists to prevent, wearing a CTE.

The leading keyword is not what decides a statement's behaviour, and two
places were relying on it, so the reading now lives in one module. It walks
each `name [(cols)] AS ( body )` in turn while tracking quotes, comments and
nesting, so an AS or a comma inside a CTE body is not mistaken for structure,
and returns the statement the clause prefixes. WITH is gone from the
allowlist entirely: a WITH that still reaches the check is one the parser
could not see through, and an unknown statement must not be run.

Two more from the same review:

Cancellation tracked the in-flight thread id per connection, but a dozen
callers reach the executor without passing the editor's single-query gate,
so statements do overlap on one connection. The shorter one finishing
deregistered the longer one — leaving Cancel a silent no-op — and a timeout
killed whichever thread was registered last, which could be an unrelated
query. Keyed by thread id now, with each execution deregistering only its
own and a timeout killing only the thread it started. The frontend had the
same shape of bug in a module-level variable and is tagged by generation.

Now that cancelling really kills the statement, the in-flight call rejects
with the server's interrupt error. Both catch blocks reported it, replacing
"Query cancelled by user" with raw database text and writing the user's own
cancellation to query history as a failed query. They check the cancel
generation the way the success path already did.

A trailing `-- note` split off as its own statement, so EXPLAIN told anyone
with a commented query to go and split their single-statement script.
Comment-only fragments no longer count.

Verified against MySQL 8: the CTE-prefixed DELETE leaves its three rows in
place, and a CTE-prefixed SELECT is still analyzed.

Co-Authored-By: Claude Opus 5 <[email protected]>
@EVWorth
EVWorth merged commit fb0ad5f into main Aug 28, 2026
9 checks passed
@EVWorth
EVWorth deleted the fix/explain-analyze-safety-and-timeout branch August 28, 2026 23:21
EVWorth added a commit that referenced this pull request Sep 11, 2026
…ering what replaced it (#646)

FR-2.2.4 specifies Ctrl+. for cancel and nothing registered it, so the only
way to stop a running query was the toolbar button (#282). It is not gated on
Monaco having focus: a query gets cancelled because it is taking too long, and
by then the user may be anywhere — including still in the editor. The
shortcuts dialog lists it now, which it did not.

#287 is half right. Its scenario — run, cancel, run again, first response
lands last — does not lose the second query's result: the generation counter
is monotonic and each execution compares against its own snapshot, so only the
cancelled query's own response is dropped. Tests for both halves of that are
added, and they pass against the unmodified store.

What is real is one step further along. `cancelActiveQuery` captured no
generation, so when `cancelQuery` rejected it wrote "Query cancelled, but the
server did not confirm" over whatever was on screen — including a query the
user had started since. Telling the server to stop can outlast the query it
was about; the message now only appears while that cancel is still what the
user is looking at.

Also added: five live-server tests for the per-connection query timeout.
`query_timeout_secs` was on the profile, in the migration, and in the
connection dialog long before anything read it (#283). It was wired up in
#512 — but nothing tested it, which is what let it be cosmetic for as long as
it was. The tests cover the limit being hit, the statement being killed on the
server rather than abandoned, a statement inside the limit being untouched, a
batch being bounded rather than each statement, and no configured timeout
meaning none.

Co-authored-by: Claude Opus 5 <[email protected]>
@EVWorth EVWorth mentioned this pull request Sep 12, 2026
8 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment