Skip to content

Fix: Prevent Connection Leak to Pool on Context Cancellation during BeginTx() - #14

Open
jamboriu wants to merge 4 commits into
raimeecas:mainfrom
jamboriu:fix/connection-leak
Open

Fix: Prevent Connection Leak to Pool on Context Cancellation during BeginTx()#14
jamboriu wants to merge 4 commits into
raimeecas:mainfrom
jamboriu:fix/connection-leak

Conversation

@jamboriu

@jamboriu jamboriu commented Aug 2, 2026

Copy link
Copy Markdown

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

  1. Thread-Safe State & Eviction: Uses mutex synchronization (mc.mu) to safeguard all critical driver states (closed and netConn). Returns driver.ErrBadConn on context cancellation, signaling the Go database/sql database pool to immediately discard and destroy the connection instead of returning it.
  2. Context-Aware Writes (Deadline Propagation): Propagates the context deadline to the underlying connection via netConn.SetWriteDeadline() to abort blocked network writes during transaction start or isolation configuration.
  3. Cancel-Only Context Support: For contexts without explicitly set deadlines (cancel-only contexts via 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.
  4. Clean Goroutine Lifecycle: Avoids goroutine leaks by cleaning up the watcher immediately when the write finishes successfully.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🎯 Prevent Connection Leak to Pool on Context Cancellation during BeginTx()

1 participant