Skip to content

add locks around transaction closing#497

Merged
jamesmoore merged 8 commits intomainfrom
fix-transaction-sharing
Mar 11, 2026
Merged

add locks around transaction closing#497
jamesmoore merged 8 commits intomainfrom
fix-transaction-sharing

Conversation

@jamesmoore
Copy link
Owner

No description provided.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds locking around the shared transaction field in SqliteDataSource to prevent race conditions when the transaction is committed or disposed while concurrent database operations are still in flight (e.g., during Task.WhenAll in Rescan). The double-checked locking pattern in ExecuteOnConnection ensures safe access to the transaction's connection, and CommitTransaction uses a "capture-under-lock, operate-outside-lock" pattern.

Changes:

  • Added a transactionLock object and applied lock blocks in BeginTransaction, CommitTransaction, and ExecuteOnConnection to synchronize access to the transaction field.
  • Refactored ExecuteOnConnection to use a double-checked locking pattern that safely falls through to a new connection if the transaction becomes unavailable.
  • Refactored CommitTransaction to capture the transaction reference under lock, then commit and dispose outside the lock with proper try/finally cleanup.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Contributor

Copilot AI commented Mar 8, 2026

@jamesmoore I've opened a new pull request, #498, to work on those changes. Once the pull request is ready, I'll request review from you.

Copilot AI and others added 3 commits March 8, 2026 08:26
…OnConnection callback

Co-authored-by: jamesmoore <6506748+jamesmoore@users.noreply.github.com>
Pass captured transaction to ExecuteOnConnection callback instead of re-reading field
@jamesmoore
Copy link
Owner Author

@codex review

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +78 to +81
if (this.transaction?.Connection != null)
{
return func(transaction.Connection);
}
else
{
using var connection = GetConnection();
return func(connection);
lock (transactionLock)
{
Copy link

Copilot AI Mar 11, 2026

Choose a reason for hiding this comment

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

ExecuteOnConnection reads this.transaction in the initial if (this.transaction?.Connection != null) check without holding transactionLock. Since BeginTransaction/CommitTransaction now write this.transaction under that lock, an unsynchronized read can observe a stale null and incorrectly fall back to opening a new connection, causing operations to run outside the intended transaction. Consider taking transactionLock before any read of this.transaction (or using Volatile.Read/Volatile.Write) and then branching based on the captured value.

Copilot uses AI. Check for mistakes.
Copy link
Owner Author

Choose a reason for hiding this comment

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

@copilot open a new pull request to apply changes based on this feedback

Copy link
Contributor

Copilot AI commented Mar 11, 2026

@jamesmoore I've opened a new pull request, #499, to work on those changes. Once the pull request is ready, I'll request review from you.

Copilot AI and others added 2 commits March 11, 2026 07:12
Co-authored-by: jamesmoore <6506748+jamesmoore@users.noreply.github.com>
Fix unsynchronized read of `transaction` in `ExecuteOnConnection`
@jamesmoore jamesmoore merged commit 57c723a into main Mar 11, 2026
5 checks passed
@jamesmoore jamesmoore deleted the fix-transaction-sharing 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.

3 participants