Skip to content

Cancel DefaultTransactionTimeout context on transaction completion - #7876

Open
00200200 wants to merge 1 commit into
go-gorm:masterfrom
00200200:fix/cancel-default-transaction-timeout
Open

00200200 wants to merge 1 commit into
go-gorm:masterfrom
00200200:fix/cancel-default-transaction-timeout

Conversation

@00200200

@00200200 00200200 commented Oct 7, 2026

Copy link
Copy Markdown
  • Do only one thing
  • Non breaking API changes
  • Tested

What did this pull request do?

When DefaultTransactionTimeout is set, db.Begin() creates a timeout context with context.WithTimeout, but the cancellation function was previously discarded. This left runtime timers and contexts active until the full timeout duration expired, even after the transaction finished.

This PR stores the cancellation function on the transaction statement so that:

  • It is canceled when Commit() or Rollback() completes.
  • It is canceled immediately if BeginTx fails with an error.
  • The context timeout is preserved across clones and sessions of the transaction.
  • Tests are added covering commit cancellation, rollback cancellation, and ensuring caller-supplied contexts with existing deadlines are not prematurely canceled.

Fixes #7875

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.

DefaultTransactionTimeout discards the cancel func, so each transaction's timer outlives Commit

1 participant