Skip to content

Commit

Permalink
libs/db: close batch (tendermint#3397)
Browse files Browse the repository at this point in the history
ClevelDB requires closing when WriteBatch is no longer needed, https://godoc.org/github.com/jmhodges/levigo#WriteBatch.Close

Fixes the memory leak in cosmos/cosmos-sdk#3842
  • Loading branch information
Stumble authored and enlight committed Jun 16, 2019
1 parent 29bb7b1 commit 9d871e2
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 0 deletions.
5 changes: 5 additions & 0 deletions libs/db/c_level_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,11 @@ func (mBatch *cLevelDBBatch) WriteSync() {
}
}

// Implements Batch.
func (mBatch *cLevelDBBatch) Close() {
mBatch.batch.Close()
}

//----------------------------------------
// Iterator
// NOTE This is almost identical to db/go_level_db.Iterator
Expand Down
5 changes: 5 additions & 0 deletions libs/db/debug_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,8 @@ func (dbch debugBatch) WriteSync() {
fmt.Printf("%v.batch.WriteSync()\n", dbch.label)
dbch.bch.WriteSync()
}

// Implements Batch.
func (dbch debugBatch) Close() {
dbch.bch.Close()
}
4 changes: 4 additions & 0 deletions libs/db/go_level_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ func (mBatch *goLevelDBBatch) WriteSync() {
}
}

// Implements Batch.
// Close is no-op for goLevelDBBatch.
func (mBatch *goLevelDBBatch) Close() {}

//----------------------------------------
// Iterator
// NOTE This is almost identical to db/c_level_db.Iterator
Expand Down
4 changes: 4 additions & 0 deletions libs/db/mem_batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ func (mBatch *memBatch) WriteSync() {
mBatch.write(true)
}

func (mBatch *memBatch) Close() {
mBatch.ops = nil
}

func (mBatch *memBatch) write(doSync bool) {
if mtx := mBatch.db.Mutex(); mtx != nil {
mtx.Lock()
Expand Down
4 changes: 4 additions & 0 deletions libs/db/prefix_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,10 @@ func (pb prefixBatch) WriteSync() {
pb.source.WriteSync()
}

func (pb prefixBatch) Close() {
pb.source.Close()
}

//----------------------------------------
// prefixIterator

Expand Down
1 change: 1 addition & 0 deletions libs/db/remotedb/grpcdb/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ func (s *server) BatchWriteSync(c context.Context, b *protodb.Batch) (*protodb.N

func (s *server) batchWrite(c context.Context, b *protodb.Batch, sync bool) (*protodb.Nothing, error) {
bat := s.db.NewBatch()
defer bat.Close()
for _, op := range b.Ops {
switch op.Type {
case protodb.Operation_SET:
Expand Down
4 changes: 4 additions & 0 deletions libs/db/remotedb/remotedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,7 @@ func (bat *batch) WriteSync() {
panic(fmt.Sprintf("RemoteDB.BatchWriteSync: %v", err))
}
}

func (bat *batch) Close() {
bat.ops = nil
}
2 changes: 2 additions & 0 deletions libs/db/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,12 @@ type DB interface {
//----------------------------------------
// Batch

// Batch Close must be called when the program no longer needs the object.
type Batch interface {
SetDeleter
Write()
WriteSync()
Close()
}

type SetDeleter interface {
Expand Down
1 change: 1 addition & 0 deletions lite/dbprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func (dbp *DBProvider) SaveFullCommit(fc FullCommit) error {

dbp.logger.Info("DBProvider.SaveFullCommit()...", "fc", fc)
batch := dbp.db.NewBatch()
defer batch.Close()

// Save the fc.validators.
// We might be overwriting what we already have, but
Expand Down
2 changes: 2 additions & 0 deletions state/txindex/kv/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func (txi *TxIndex) Get(hash []byte) (*types.TxResult, error) {
// AddBatch indexes a batch of transactions using the given list of tags.
func (txi *TxIndex) AddBatch(b *txindex.Batch) error {
storeBatch := txi.store.NewBatch()
defer storeBatch.Close()

for _, result := range b.Ops {
hash := result.Tx.Hash()
Expand Down Expand Up @@ -109,6 +110,7 @@ func (txi *TxIndex) AddBatch(b *txindex.Batch) error {
// Index indexes a single transaction using the given list of tags.
func (txi *TxIndex) Index(result *types.TxResult) error {
b := txi.store.NewBatch()
defer b.Close()

hash := result.Tx.Hash()

Expand Down

0 comments on commit 9d871e2

Please sign in to comment.