[fix](fe) Stop stalled MySQL result writes on query cancellation #70607
All Vouch review confidence checks passed
Residual risk 0.22 = Change Risk (0.52) × (1 − Review Confidence (0.57)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.52 · 650 lines diff across 8 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.388
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #70607"
general
weight: 0.35
morrySnow
What problem does this PR solve?
When a MySQL client stops consuming a large result, the FE can block writing a result packet.
KILL QUERYcancels the query's backend execution and result fetching, but the original FE worker remains in the network write untilnet_write_timeoutexpires. The query therefore remains visible even though backend execution has already been cancelled or finished.The normal flow is:
Cancellation reaches
StmtExecutor, the coordinator and result receivers. It does not interrupt an already-runningChannels.writeBlockingorflushBlockinginMysqlChannel. Adding only a cancellation check between rows cannot release a worker already waiting inside that write.Fix
finally.awaitWritablewaits. A stalled write checks cancellation at intervals of at most 100 ms. The original network-write deadline is retained rather than renewed on each check; the writable path introduces no waiting.KILL QUERYconnection reuse: a cancellation error that can be written normally is still sent without disconnecting the client.The bounded cancellation response applies to stalled MySQL writes. This PR does not change coordinator-initialization cancellation races or promise the same bound at every query execution stage.
Release note
Cancelled queries now stop promptly when blocked sending MySQL results. Ordinary query cancellation continues to allow connection reuse; cancelling a stalled result stream closes that connection.
Check List (For Author)
- [x] Regression test
- [x] Unit Test
- [x] Manual test (details below)
- [ ] No need to test or manual test. Explain why:
- [ ] No.
- [x] Yes. Cancellation interrupts a stalled MySQL result write instead of waiting for its network timeout; that stalled connection is closed.
- [x] No.
- [ ] Yes.
Unit tests
MysqlChannelTest,MysqlResultSenderTest,MysqlWriteCancellationTest,MysqlConnectProcessorCursorFetchTestandMysqlPacketGoldenTestcover cancellation during write/flush waits, partial writes, original timeout deadlines, writable cancellation responses, command cleanup, EOF/OK/NOOP handling and the existing packet golden cases.Current PR head
367c2788f97passed all 39 tests in the five named classes plusEncryptSQLTest(0 failures, 0 errors), usingrun-fe-ut.sh. Checkstyle reported zero violations. The unchanged production implementation passed the complete FE package build withbuild.sh --featacad76189b6; this follow-up only changes four test mock initializers. All six selected test suites completed successfully; the script was then stopped during slow unpacking of a subsequent unrelated Java extension dependency. This is not a full-script or full-FEUT success claim.The FEUT failure in all four
EncryptSQLTestmethods was reproduced locally before this follow-up. These audit tests use constructor mocks withCALLS_REAL_METHODS, bypassing the initialization of the executor's atomic cancellation state. Statement finalization now reads that state, exposing the incomplete mock setup. Each initializer now explicitly stubsgetPendingCancelReason()to return null, representing uncancelled execution. The existing SQL masking assertions and production initialization invariant remain unchanged.Two focused protocol reproductions failed on the previous implementation. The new tests verify one terminal ERR across intermediate-statement and command finalization, followed by a usable next response. A prepared-handler test simulates the Command exception wrapper after a partial write, then makes the sink writable and clears/reset buffers: the connection is killed and closed, and neither finalization nor another write can append bytes.
JDBC regression
Added
test_mysql_kill_query_reuse. It cancels a long query twice on the same JDBC connection, checks the server connection ID after each cancellation, and then executesSELECT 42. The output was generated withrun-regression-test.shand a subsequent comparison run passed: 1 suite, 0 failures. This JDBC/socket validation predates the follow-up protocol fixes; current-head coverage of the newly reported cases is provided by the unit tests above.Local socket reproduction
The following functional checks ran against a locally built FE before rebasing onto current master; the BE used the existing local binary. They are local samples, not throughput or percentile benchmarks.
1. Connect a MySQL client, set
net_write_timeout=20, reduce its receive buffer to 4 KiB, send the following query and stop reading the response:2. Confirm the FE is waiting in
MysqlChannel.realNetSend/awaitWritableusing a thread dump, then cancel from another connection.3. Before the fix, the FE waited for the full 20-second write timeout (about 17 seconds remained after cancellation). After the fix, three
KILL QUERYsamples removed the session in approximately 29 / 62 / 85 ms, and the client observed disconnection.KILL CONNECTIONcontrols completed in approximately 3 ms. Session-list sampling used a 20 ms interval.4. For ordinary cancellation while fetching results, cancel three successive queries on one TCP connection. Each cancellation returned an error, and subsequent queries and PING succeeded with both the server connection ID and client socket unchanged. Three additional early-cancellation checks also retained their original connection, although one waited for the current five-second BE operation to finish.
5. With normal client consumption, all 128 rows / 128 MiB of the result were received.
The current implementation also passed a complete FE package build with
build.sh --feand Checkstyle with zero violations. Remote current-head build and review are requested separately.Check List (For Reviewer who merge this PR)