Skip to content

Commit

Permalink
mvcc: fix panic on *bolt.Tx.Size after commit
Browse files Browse the repository at this point in the history
Newly created *batchTx from 'newBatchTx' returns after
*batchTx.Commit() where boltdb internally initializes
*bolt.Tx.meta and *bolt.Tx.db as nil. So subsequent *bolt.Tx.Size
call would panic with nil pointer reference.

Fix etcd-io/etcdlabs#30.
  • Loading branch information
gyuho committed Oct 18, 2016
1 parent 5c60478 commit 1e70190
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
4 changes: 4 additions & 0 deletions mvcc/backend/batch_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ func (t *batchTx) commit(stop bool) {
if t.pending == 0 && !stop {
t.backend.mu.RLock()
defer t.backend.mu.RUnlock()
t.tx, err = t.backend.db.Begin(true)
if err != nil {
plog.Fatalf("cannot begin tx (%s)", err)
}
atomic.StoreInt64(&t.backend.size, t.tx.Size())
return
}
Expand Down
12 changes: 12 additions & 0 deletions mvcc/backend/batch_tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,15 @@ func TestBatchTxBatchLimitCommit(t *testing.T) {
return nil
})
}

// TestBatchTxCommitSize tests *batchTx.tx.Size call after *batchTx.Commit does not panic.
// *bolt.Tx commit initializes *bolt.Tx.meta and *bolt.Tx.db as nil. And if *batchTx is not
// properly initialized, it panics with nil pointer reference.
func TestBatchTxCommitSize(t *testing.T) {
b, tmpPath := NewTmpBackend(time.Hour, 100)
defer cleanup(b, tmpPath)

if _, err := b.Hash(make(map[IgnoreKey]struct{})); err != nil {
t.Fatal(err)
}
}

0 comments on commit 1e70190

Please sign in to comment.