Repository navigation
fix(query): give a timed-out statement's connection back to the pool - #659
Merged
Merged
Conversation
The timeout dropped the result stream and then killed the statement. Dropping it leaves sqlx to drain the connection's remaining packets before it can return to the pool, and on MariaDB that drain never finishes — the connection is checked out for good. The consequence is worse than a leak. With pool_max connections lost, the next timeout has nothing to acquire, so it waits for a statement to end naturally and reports "exceeded the 1s timeout" after five seconds; `disconnect` then hangs for ever on `pool.close()`. Measured on MariaDB 11.8 with pool_max=4 and a 1s timeout against SELECT SLEEP(5): the first three attempts returned at 1s, the fourth and fifth at 5s. MySQL happened to recover, which is why nothing caught it: every integration test in the tree ran against MySQL only. Killing first and then reading the stream to its end gives sqlx the server's "query interrupted" error, which ends the stream properly and returns the connection. The drain is bounded at five seconds, so a server that says nothing costs one grace period rather than a connection that never comes back. The new test runs ten timeouts through a pool of two and asserts each returns at its deadline rather than at the statement's, then that `disconnect` returns. It fails against the old code on MariaDB from the third attempt. `query_timeout.rs` takes MAS_TEST_PORT now, as the other integration files do, so both servers can be run. Closes #658.
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 #658. Found by running the EXPLAIN integration suite against MariaDB for the first time.
What happens
The timeout dropped the result stream and then killed the statement. Dropping a partially-consumed stream leaves sqlx to drain the connection's remaining packets before it can go back to the pool — and on MariaDB that drain never finishes. The connection is checked out for good.
The consequence is worse than a leak. Measured on MariaDB 11.8,
pool_max = 4,query_timeout_secs = 1,SELECT SLEEP(5)five times:After
pool_maxtimeouts there is nothing left to acquire, so the next one waits for a statement to end naturally and then reports "exceeded the 1s timeout" — the setting silently stops doing what it says.disconnectnever returns either, becausepool.close()waits for connections that are still out.MySQL happened to recover, which is why nothing caught it: every integration test in the tree ran against MySQL only.
What was ruled out first
PROCESSLISTis clean immediately after the timeout, on both.SELECT CONNECTION_ID()decodes asu64on MariaDB, so the thread id is not lost.KILL QUERYworks through the prepared-statement protocol on MariaDB.The fix
Kill first, then read the stream to its end. That gives sqlx the server's "query interrupted" error, which ends the stream properly and returns the connection. The drain is bounded at five seconds, so a server that says nothing costs one grace period rather than a connection that never comes back.
Test
Ten timeouts through a pool of two, asserting each returns at its deadline rather than at the statement's, then that
disconnectreturns within ten seconds. Against the old code on MariaDB it fails from the third attempt.query_timeout.rsnow takesMAS_TEST_PORTlike the other integration files, so both servers can be run. Passing on MySQL 8.0.46 and MariaDB 11.8; the EXPLAIN suite now passes on both too.