Skip to content

Fix unsynchronized read of transaction in ExecuteOnConnection#499

Merged
jamesmoore merged 2 commits intofix-transaction-sharingfrom
copilot/sub-pr-497-again
Mar 11, 2026
Merged

Fix unsynchronized read of transaction in ExecuteOnConnection#499
jamesmoore merged 2 commits intofix-transaction-sharingfrom
copilot/sub-pr-497-again

Conversation

Copy link
Contributor

Copilot AI commented Mar 11, 2026

ExecuteOnConnection used a double-checked locking pattern where the initial if (this.transaction?.Connection != null) guard read this.transaction outside transactionLock. Since BeginTransaction/CommitTransaction write under the lock, this was a data race — a stale null read could cause operations to bypass an active transaction and open a new connection instead.

Changes

  • SqliteDataSource.ExecuteOnConnection: Removed the outer unsynchronized check. The lock now guards the first read of this.transaction; the captured value is used to branch.
// Before: unsynchronized outer read, then re-check inside lock
if (this.transaction?.Connection != null)       // ← data race
{
    lock (transactionLock)
    {
        var currentTransaction = this.transaction;
        if (currentTransaction?.Connection != null)
            return func(currentTransaction.Connection, currentTransaction);
    }
}

// After: single read, always under the lock
lock (transactionLock)
{
    var currentTransaction = this.transaction;
    if (currentTransaction?.Connection != null)
        return func(currentTransaction.Connection, currentTransaction);
}

🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

Co-authored-by: jamesmoore <6506748+jamesmoore@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix locks around transaction closing based on feedback Fix unsynchronized read of transaction in ExecuteOnConnection Mar 11, 2026
@jamesmoore jamesmoore marked this pull request as ready for review March 11, 2026 07:13
@jamesmoore jamesmoore merged commit f8b4be6 into fix-transaction-sharing Mar 11, 2026
@jamesmoore jamesmoore deleted the copilot/sub-pr-497-again branch March 11, 2026 07:33
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.

2 participants