Skip to content

Commit

Permalink
backend: getting lock before accessing db
Browse files Browse the repository at this point in the history
  • Loading branch information
xiang90 committed Apr 20, 2016
1 parent 29dfca8 commit f24b6a9
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
7 changes: 4 additions & 3 deletions storage/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,9 @@ func (b *backend) Defrag() error {
// lock batchTx to ensure nobody is using previous tx, and then
// close previous ongoing tx.
b.batchTx.Lock()
defer b.batchTx.Unlock()

// lock database after lock tx to avoid deadlock.
b.mu.Lock()
defer b.mu.Unlock()

b.batchTx.commit(true)
b.batchTx.tx = nil
Expand Down Expand Up @@ -251,8 +249,11 @@ func (b *backend) Defrag() error {
if err != nil {
log.Fatalf("backend: cannot begin tx (%s)", err)
}

b.batchTx.Unlock()
b.mu.Unlock()
// commit to update metadata like db.size
b.batchTx.commit(false)
b.ForceCommit()

return nil
}
Expand Down
2 changes: 2 additions & 0 deletions storage/backend/batch_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ func (t *batchTx) commit(stop bool) {
// commit the last tx
if t.tx != nil {
if t.pending == 0 && !stop {
t.backend.mu.RLock()
defer t.backend.mu.RUnlock()
atomic.StoreInt64(&t.backend.size, t.tx.Size())
return
}
Expand Down

0 comments on commit f24b6a9

Please sign in to comment.