fix(memory): roll back pooled MySQL connections on release (follow-up to #296)#303
Merged
Merged
Conversation
The MySQL connection pool returned connections to the pool without ending their transaction. Under the default autocommit=False, every read (load/exists/list_threads/count/get_metadata/query_by_metadata/ search_data) and the SELECT 1 health check opens a transaction that holds a SHARED_READ metadata lock on the tables it touched. A pooled connection left in that state sits idle-in-transaction: - a later DROP/ALTER blocks indefinitely on the pending EXCLUSIVE MDL (proved against MySQL 9.6: DROP TABLE hung behind GRANTED SHARED_READ locks held by sleeping locus connections), and - the next borrower reuses the pinned REPEATABLE READ snapshot and can read stale data. Roll back on return so every pooled connection is handed out clean, and discard it if the rollback fails. Factor the connector error tuple into a shared _connection_errors() helper used by both _is_healthy and the new _release. Also fix the cold-start adapter integration test, which mutated a frozen AgentState (state.agent_id = ...) and raised before exercising the pool; use the immutable model_copy(update=...) pattern. This is the test that surfaced the hang once it could actually run. Follow-up to #296. Signed-off-by: Federico Kamelhar <federico.kamelhar@oracle.com>
59c6f4c to
5578b88
Compare
Merged
This was referenced Jun 4, 2026
Contributor
|
Nice fix. Rolling back on pool release is the right boundary: it protects reads, health checks, and any future code path that borrows a connection without having to remember transaction cleanup locally. |
luigisaetta
approved these changes
Jun 4, 2026
Contributor
luigisaetta
left a comment
There was a problem hiding this comment.
ok approved, great catch!
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.
Summary
Follow-up to #296. Fixes a connection-pool transaction-management bug in the MySQL checkpointer that causes an idle-in-transaction → metadata-lock deadlock (and latent stale reads), and fixes the cold-start adapter integration test that was hiding it.
The bug
_MySQLConnectionPoolreturned connections to the pool without ending their transaction. With the defaultautocommit=False, every read (load,exists,list_threads,count,get_metadata,query_by_metadata,search_data) — and theSELECT 1health check added in #296 — opens a transaction that holds aSHARED_READmetadata lock on the tables it touches. A connection pooled in that state sits idle in transaction, which means:DROP/ALTERblocks indefinitely behind the pendingEXCLUSIVEMDL.REPEATABLE READsnapshot and can read stale data.How it was found
The new
test_mysql_adapter_concurrent_same_thread_cold_startfrom #296 never actually ran the pool: it mutated a frozenAgentState(state.agent_id = ...), raisingValidationErrorbefore touching MySQL — so CI (unit-only) stayed green and the integration suite errored out early. Once that test is corrected to use the immutablemodel_copy(update=...)pattern, it reaches the pool and hangs.Reproduced against MySQL 9.6:
The fix
_release), so every pooled connection is handed out clean; discard it if the rollback fails. This is standard pool hygiene and fixes both the MDL deadlock and the stale-read window regardless of theautocommitsetting._connection_errors()helper used by both_is_healthyand_release(a dead-connectionrollback()raisesmysql.connector.Error, which the previous narrowexceptwould have let escape thefinally).model_copy(update=...)instead of mutating a frozen model.Validation
LOCUS_MYSQL_INTEGRATION=1) against MySQL 9.6: 11 passed, stable across repeated runs (previously: 1 hang + 1 error)._release(rollback-on-return, discard-on-rollback-failure).ruff,ruff format --check, andhatch run typecheckall clean.