Skip to content

Commit

Permalink
Merge pull request #708 from josephschorr/memdb-post-close
Browse files Browse the repository at this point in the history
Further fixes to memdb to not panic post-close
  • Loading branch information
josephschorr authored Jul 25, 2022
2 parents 20c7356 + 4cadf8c commit 694a76f
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions internal/datastore/memdb/memdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ func (mdb *memdbDatastore) SnapshotReader(revision datastore.Revision) datastore
mdb.RLock()
defer mdb.RUnlock()

if len(mdb.revisions) == 0 {
return &memdbReader{nil, nil, datastore.NoRevision, fmt.Errorf("memdb datastore is not ready")}
}

if err := mdb.checkRevisionLocal(revision); err != nil {
return &memdbReader{nil, nil, datastore.NoRevision, err}
}
Expand All @@ -108,8 +112,13 @@ func (mdb *memdbDatastore) SnapshotReader(revision datastore.Revision) datastore
revIndex = len(mdb.revisions) - 1
}

snapshotRevision := mdb.revisions[revIndex].revision
roTxn := mdb.revisions[revIndex].db.Txn(false)
rev := mdb.revisions[revIndex]
if rev.db == nil {
return &memdbReader{nil, nil, datastore.NoRevision, fmt.Errorf("memdb datastore is already closed")}
}

snapshotRevision := rev.revision
roTxn := rev.db.Txn(false)

txSrc := func() (*memdb.Txn, error) {
return roTxn, nil
Expand Down Expand Up @@ -237,12 +246,17 @@ func (mdb *memdbDatastore) Close() error {
defer mdb.Unlock()

// TODO Make this nil once we have removed all access to closed datastores
mdb.revisions = []snapshot{
{
revision: decimal.Zero,
db: mdb.db,
},
if db := mdb.db; db != nil {
mdb.revisions = []snapshot{
{
revision: decimal.Zero,
db: db,
},
}
} else {
mdb.revisions = []snapshot{}
}

mdb.db = nil

return nil
Expand Down

0 comments on commit 694a76f

Please sign in to comment.