Fix: Prevent Connection Leak to Pool on Context Cancellation during BeginTx() - #14
Open
jamboriu wants to merge 4 commits into
Open
Fix: Prevent Connection Leak to Pool on Context Cancellation during BeginTx()#14jamboriu wants to merge 4 commits into
jamboriu wants to merge 4 commits into
Conversation
…ts and cancel-only watchers
…ion and package main conflict resolution
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.
Resolves #2
Description
This pull request prevents database connection leaks in the pool under context cancellation or timeouts during transaction initialization (
BeginTx).When a transaction setup process is interrupted by context cancellation, the connection state can be left in an active transaction state on the MySQL server. If this connection is returned to the pool, it will lead to both connection pollution (dirty state) and potential connection leaks or synchronization lockups.
Solution
mc.mu) to safeguard all critical driver states (closedandnetConn). Returnsdriver.ErrBadConnon context cancellation, signaling the Godatabase/sqldatabase pool to immediately discard and destroy the connection instead of returning it.netConn.SetWriteDeadline()to abort blocked network writes during transaction start or isolation configuration.context.WithCancel), it spawns a lightweight watcher goroutine that closes the connection if context cancellation is triggered during a pending network write, avoiding infinite blocking issues.Verification & Testing
Added comprehensive testing in
connection_test.go:TestBeginTxContextCancellation: Confirms connections are marked closed and evicted under timeout during start query.TestBeginTxContextCancellationDuringIsolationSetting: Confirms eviction when context expires during isolation level settings.TestBeginTxSuccess: Normal transaction path without timeouts.TestBeginTxAlreadyClosedConnection: Error handling for closed connection objects.TestBeginTxReadOnly: Successful START TRANSACTION READ ONLY path.TestBeginTxInvalidIsolation: Error detection on invalid isolation configurations.TestBeginTxNetworkErrorWithoutCancellation: Checks that normal network errors are not mistakenly masked as ErrBadConn.TestBeginLegacy: Verification of classic Begin interface.TestBeginTxCancelOnlyContextDuringWrite: Tests that cancel-only contexts successfully close the net socket during write delay.All 9 tests passed successfully with Go Race Detector enabled on Go 1.23.