Fix: Prevent Connection Pollution by Closing/Resetting Connection on Context Cancellation During BeginTx - #13
Open
jamboriu wants to merge 4 commits into
Open
Conversation
…Context Cancellation During BeginTx
…dge cases from audit
…llation 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 #1
Description
This pull request prevents connection pollution in the database pool during transaction initialization (
BeginTx).When a transaction start (
START TRANSACTIONor setting transaction isolation level) is interrupted by context cancellation, the connection state becomes ambiguous and "dirty" (the server might have actually initiated the transaction, but the driver returns a context error to the client). If this connection is recycled back into the Godatabase/sqlconnection pool, subsequent queries will run inside an uncommitted, leaked transaction.Solution
ctx.Err() != nil) is detected immediately after executing isolation level configurations orSTART TRANSACTIONcommand roundtrips, the driver explicitly closes the underlying net connection.driver.ErrBadConnto the caller. This is the official signal to Go'sdatabase/sqlconnection pool to destroy the connection and evict it from the pool instead of recycling it.Verification & Testing
Implemented comprehensive unit tests in
connection_test.go:TestBeginTxContextCancellation: Validates that cancellation during transaction start closes the net connection and returnsdriver.ErrBadConn.TestBeginTxContextCancellationDuringIsolationSetting: Validates that cancellation during isolation level writes is handled safely.TestBeginTxSuccess: Confirms normal transaction initiation flow when context is not canceled.TestBeginTxAlreadyClosedConnection: Confirms error behavior on already closed driver connections.All tests passed successfully on Go 1.23.