Skip to content

Commit

Permalink
fix(txn): discard empty transactions on CommitWith (dgraph-io#2031)
Browse files Browse the repository at this point in the history
  • Loading branch information
mYmNeo committed Jan 24, 2024
1 parent beb1fcd commit 65d2676
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
41 changes: 41 additions & 0 deletions batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,44 @@ func TestBatchErrDeadlock(t *testing.T) {
require.Error(t, wb.Flush())
require.NoError(t, db.Close())
}

// This test ensures we don't end up in deadlock in case of empty writebatch.
func TestEmptyWriteBatch(t *testing.T) {
t.Run("normal mode", func(t *testing.T) {
runBadgerTest(t, nil, func(t *testing.T, db *DB) {
wb := db.NewWriteBatch()
require.NoError(t, wb.Flush())
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) {
opt := getTestOptions("")
opt.managedTxns = true
runBadgerTest(t, &opt, func(t *testing.T, db *DB) {
t.Run("WriteBatchAt", func(t *testing.T) {
wb := db.NewWriteBatchAt(2)
require.NoError(t, wb.Flush())
wb = db.NewWriteBatchAt(208)
require.NoError(t, wb.Flush())
wb = db.NewWriteBatchAt(31)
require.NoError(t, wb.Flush())
})
t.Run("ManagedWriteBatch", func(t *testing.T) {
wb := db.NewManagedWriteBatch()
require.NoError(t, wb.Flush())
wb = db.NewManagedWriteBatch()
require.NoError(t, wb.Flush())
wb = db.NewManagedWriteBatch()
require.NoError(t, wb.Flush())
})
})
})
}
2 changes: 2 additions & 0 deletions txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,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 65d2676

Please sign in to comment.