Repository navigation
fix(explain): stop EXPLAIN ANALYZE running writes, and make timeouts real - #512
Merged
Merged
Conversation
…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]>
This was referenced Aug 28, 2026
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]>
This was referenced Aug 28, 2026
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]>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Closes #412, closes #418, closes #420.
First of three PRs bringing
area/explainup 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 ANALYZEexecutes the statement it measures. The UI offered it from the same split button as the inertEXPLAIN, soEXPLAIN ANALYZE DELETE FROM users WHERE 1=1wiped the table — no confirmation, no read-only check, no way to stop it.Separately,
query_timeout_secswas 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_querycommand, so the safety decision can't be bypassed by a second call site (this is what #412 proposes, and closes theexplain_querydoc-drift noted as F11 in #411).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.read_onlywas stored and never enforced anywhere; it now reaches the executor via the live connection.;that madeEXPLAIN ANALYZE SELECT 1;a syntax error and rejects a multi-statement script with a sentence instead ofERROR 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 toKILL QUERYfrom 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.rsagainst MySQL 8:DELETEcanary still has its 3 rows afterEXPLAIN ANALYZE DELETE ... WHERE 1=1EXPLAIN ANALYZE SELECT ...;with the trailing semicolon succeedsSELECT SLEEP(30)returns promptly and leaves nothing ininformation_schema.PROCESSLIST— the part that separates a real timeout from dropping the futureSLEEP(30)short, and the session stays usable afterwardsAll 1606 frontend tests and the Rust suite pass; clippy/tsc/dprint clean.
Notes for review
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.AppLayoutrather than the store; only thekinddiscriminator crosses the boundary.executor.rsappendsLIMITto anything starting withEXPLAIN, 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