Skip to content

Conversation

Copy link

Copilot AI commented Sep 20, 2025

This PR fixes a transaction leak that occurs when using Session within Scopes. The issue manifests as database connections that are never properly committed or rolled back, leading to connection pool exhaustion.

Problem

When using the following pattern:

DB.Scopes(func(s *gorm.DB) *gorm.DB {
    return s.Session(&gorm.Session{
        DryRun: true,
    })
}).Where("name = ?", "jinzhu").Delete(&User{})

Transactions would leak because:

  1. Session() sets clone=2 for non-NewDB configurations
  2. BeginTransaction calls InstanceSet("gorm:started_transaction", true) which creates a new cloned instance and stores the transaction flag there
  3. CommitOrRollbackTransaction calls InstanceGet("gorm:started_transaction") on the original instance, missing the transaction flag
  4. Without the flag, transactions are never committed/rolled back, causing connection leaks

Root Cause

The issue stems from the difference between InstanceSet and InstanceGet behavior with cloned instances:

  • InstanceSet calls getInstance() which can return a new cloned instance when clone > 0
  • InstanceGet reads from the current instance directly
  • This creates a mismatch where the flag is stored in one instance but retrieved from another

Solution

Modified the transaction callbacks to use direct Statement.Settings access instead of the InstanceSet/InstanceGet methods:

  • BeginTransaction: db.Statement.Settings.Store("gorm:started_transaction", true)
  • CommitOrRollbackTransaction: db.Statement.Settings.Load("gorm:started_transaction")

This ensures both callbacks operate on the same Statement instance, preventing the transaction leak.

Testing

Added comprehensive tests that:

  • Reproduce the transaction leak issue using connection pool statistics
  • Verify the fix resolves the leak
  • Confirm existing transaction and scopes functionality remains intact

Fixes #7592.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copy link
Author

Copilot AI commented Sep 20, 2025

@jinzhu 👋 This repository doesn't have Copilot instructions. With Copilot instructions, I can understand the repository better, work faster and produce higher quality PRs.

I can generate a .github/copilot-instructions.md file for you automatically. Click here to open a pre-filled issue and assign it to me. I'll write the instructions, and then tag you for review.

Copilot AI changed the title [WIP] Transaction leak using Session in Scopes Fix transaction leak when using Session in Scopes Sep 20, 2025
Copilot AI requested a review from jinzhu September 20, 2025 00:57
Copilot finished work on behalf of jinzhu September 20, 2025 00:57
@jinzhu jinzhu closed this Oct 26, 2025
@horror-proton
Copy link

@jinzhu any other plan or idea for fixing #7592

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.

Transaction leak using Session in Scopes

3 participants