Skip to content

Commit

Permalink
fix(txn): properly discard transactions during commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Wondertan committed Nov 25, 2023
1 parent fb1b009 commit 2b9e133
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 5 deletions.
6 changes: 2 additions & 4 deletions txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -658,18 +658,17 @@ func (txn *Txn) commitPrecheck() error {
// If error is nil, the transaction is successfully committed. In case of a non-nil error, the LSM
// tree won't be updated, so there's no need for any rollback.
func (txn *Txn) Commit() error {
defer txn.Discard()
// txn.conflictKeys can be zero if conflict detection is turned off. So we
// should check txn.pendingWrites.
if len(txn.pendingWrites) == 0 {
// Discard the transaction so that the read is marked done.
txn.Discard()
return nil
}
// Precheck before discarding txn.
if err := txn.commitPrecheck(); err != nil {
return err
}
defer txn.Discard()

txnCb, err := txn.commitAndSend()
if err != nil {
Expand Down Expand Up @@ -712,6 +711,7 @@ func (txn *Txn) CommitWith(cb func(error)) {
if cb == nil {
panic("Nil callback provided to CommitWith")
}
defer txn.Discard()

if len(txn.pendingWrites) == 0 {
// Do not run these callbacks from here, because the CommitWith and the
Expand All @@ -727,8 +727,6 @@ func (txn *Txn) CommitWith(cb func(error)) {
return
}

defer txn.Discard()

commitCb, err := txn.commitAndSend()
if err != nil {
go runTxnCallback(&txnCb{user: cb, err: err})
Expand Down
1 change: 0 additions & 1 deletion txn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,6 @@ func TestManagedDB(t *testing.T) {
for i := 0; i <= 3; i++ {
require.NoError(t, txn.SetEntry(NewEntry(key(i), val(i))))
}
require.Error(t, txn.Commit())
require.NoError(t, txn.CommitAt(3, nil))

// Read data at t=2.
Expand Down

0 comments on commit 2b9e133

Please sign in to comment.