-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
go/consensus/tendermint: Make sure DBs are only closed during cleanup
Tendermint Core will close the block/state store DBs during Stop which can make certain in-flight queries trigger a panic due to the database being already closed. This commit introduces a DB wrapper that enables us to defer closing the DB until Cleanup is called (as at that point all of the services have already been stopped).
- Loading branch information
Showing
5 changed files
with
84 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package db | ||
|
||
import ( | ||
"sync" | ||
|
||
dbm "github.com/tendermint/tm-db" | ||
) | ||
|
||
// Closer manages closing of multiple Tendermint Core databases. | ||
type Closer struct { | ||
l sync.Mutex | ||
dbs []dbm.DB | ||
} | ||
|
||
// Close closes all the managed databases. | ||
func (c *Closer) Close() { | ||
c.l.Lock() | ||
defer c.l.Unlock() | ||
|
||
for _, db := range c.dbs { | ||
_ = db.Close() | ||
} | ||
} | ||
|
||
// NewCloser creates a new empty database closer. | ||
func NewCloser() *Closer { | ||
return &Closer{} | ||
} | ||
|
||
type dbWithCloser struct { | ||
dbm.DB | ||
} | ||
|
||
func (d *dbWithCloser) Close() error { | ||
// Do nothing unless explicitly closed via the closer. | ||
return nil | ||
} | ||
|
||
// WithCloser wraps a Tendermint Core database instance so that it can only be closed by the given | ||
// closer instance. Direct attempts to close the returned database instance will be ignored. | ||
func WithCloser(db dbm.DB, closer *Closer) dbm.DB { | ||
closer.l.Lock() | ||
defer closer.l.Unlock() | ||
|
||
closer.dbs = append(closer.dbs, db) | ||
|
||
return &dbWithCloser{db} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters