Skip to content

Commit

Permalink
fix(txn): discard empty transactions on CommitWith (#2031)
Browse files Browse the repository at this point in the history
## Problems
* Transactions with empty `pendingWrites` were never discarded(and
marked as done) for `CommitWith`
* This is similar to #2018,
which solved the same problem for `Commit`, but not for `CommitWith`
* The `CommitWith` is used by `WriteBatch`, so flushing an empty batch
never discarded the inner shadowed transaction, causing a multitude of
issues.

## Solution
Make sure `Discard` is called for `CommitWith` when `pendingWrites` are
empty. The existing unit test is updated to assert that.
  • Loading branch information
Wondertan committed Dec 18, 2023
1 parent fb1b009 commit 09b73f7
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
5 changes: 5 additions & 0 deletions batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,12 @@ func TestEmptyWriteBatch(t *testing.T) {
wb = db.NewWriteBatch()
require.NoError(t, wb.Flush())
wb = db.NewWriteBatch()
// Flush commits inner txn and sets a new one instead.
// Thus we need to save it to check if it was discarded.
txn := wb.txn
require.NoError(t, wb.Flush())
// check that flushed txn was discarded and marked as read.
require.True(t, txn.discarded)
})
})
t.Run("managed mode", func(t *testing.T) {
Expand Down
2 changes: 2 additions & 0 deletions txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,8 @@ func (txn *Txn) CommitWith(cb func(error)) {
// callback might be acquiring the same locks. Instead run the callback
// from another goroutine.
go runTxnCallback(&txnCb{user: cb, err: nil})
// Discard the transaction so that the read is marked done.
txn.Discard()
return
}

Expand Down

0 comments on commit 09b73f7

Please sign in to comment.