Skip to content

refactor: bind transaction close callbacks to active transactions - #954

Open
olavloite wants to merge 2 commits into
mainfrom
bind-close-callback-to-tx
Open

olavloite wants to merge 2 commits into
mainfrom
bind-close-callback-to-tx

Conversation

@olavloite

Copy link
Copy Markdown
Collaborator

Previously, custom transaction close callbacks (such as returning a checked-out connection to the pool when a transaction ends) were temporarily stored as a field on the connection struct before the transaction was initiated. This transient state risked lingering across connection pool reuses and required coordinating state across separate begin steps. In addition, if starting a transaction failed early, checked-out connections could occasionally leak.

This change refactors transaction cleanup by registering close callbacks directly onto active transactions after they are created.

Key improvements:

  • Scopes close callbacks strictly to the active transaction lifecycle, preventing stray cleanup state from leaking across pooled connections.
  • Fixes connection leaks on failed transaction starts and canceled contexts by ensuring connections are immediately returned to the pool on error.
  • Supports dynamically registered cleanup hooks so that callbacks added after a transaction begins executing are reliably invoked on commit or rollback.
  • Simplifies the transaction begin helpers across read-write, read-only, and batch read-only workflows.

@olavloite
olavloite requested a review from a team as a code owner November 12, 2026 10:16
@olavloite
olavloite requested a review from rahul2393 November 12, 2026 10:17

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the transaction close callback registration mechanism by replacing the temporary withTransactionCloseFunc with a chained registerTransactionCloseFunc on the active transaction. It also improves robustness by checking for canceled contexts early in beginTx and ensuring proper cleanup (rollback and connection closing) on transaction initialization errors to prevent connection leaks. Additionally, comprehensive tests are added to verify transaction lifecycles, canceled contexts, and late registration of close functions. Feedback was provided on a potential panic in the test helper cancelOnBeginContext.Err() due to non-thread-safe channel closing, suggesting the use of sync.Once.

Comment thread driver_with_mockserver_test.go Resolved
Previously, custom transaction close callbacks (such as returning a checked-out
connection to the pool when a transaction ends) were temporarily stored as a
field on the connection struct before the transaction was initiated. This
transient state risked lingering across connection pool reuses and required
coordinating state across separate begin steps. In addition, if starting a
transaction failed early, checked-out connections could occasionally leak.

This change refactors transaction cleanup by registering close callbacks
directly onto active transactions after they are created.

Key improvements:
- Scopes close callbacks strictly to the active transaction lifecycle,
  preventing stray cleanup state from leaking across pooled connections.
- Fixes connection leaks on failed transaction starts and canceled contexts
  by ensuring connections are immediately returned to the pool on error.
- Supports dynamically registered cleanup hooks so that callbacks added after
  a transaction begins executing are reliably invoked on commit or rollback.
- Simplifies the transaction begin helpers across read-write, read-only, and
  batch read-only workflows.
@olavloite
olavloite force-pushed the bind-close-callback-to-tx branch from 6163e46 to 4748e50 Compare November 12, 2026 10:26
@olavloite

Copy link
Copy Markdown
Collaborator Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the transaction close callback registration mechanism in the Spanner driver by replacing the pre-transaction close function storage with a dynamic registration method (registerTransactionCloseFunc) that chains close callbacks directly on the active transaction. This change ensures robust cleanup and prevents connection leaks, particularly when transactions are initiated with canceled contexts or when setting transaction options fails. Additionally, comprehensive unit, mock server, and lifecycle tests have been added to validate the new registration behavior, error handling, and connection leak prevention. I have no feedback to provide as the changes are well-implemented and adhere to the repository's style guidelines.

This branch has not been deployed

No deployments
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.

1 participant