Skip to content

Commit

Permalink
fix: add flush mutex for nodebufferlist
Browse files Browse the repository at this point in the history
  • Loading branch information
joeylichang committed Apr 7, 2024
1 parent 58481d8 commit 3f89824
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions trie/triedb/pathdb/nodebufferlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type nodebufferlist struct {
base *multiDifflayer // Collect the nodes of nodebufferlist and write to disk.
persistID uint64 // The last state id that have written to disk.
baseMux sync.RWMutex // The mutex of base multiDifflayer and persistID.
flushMux sync.RWMutex // The mutex of flush base multiDifflayer for reorg corner case.

isFlushing atomic.Bool // Flag indicates writing disk under background.
stopFlushing atomic.Bool // Flag stops writing disk under background.
Expand Down Expand Up @@ -197,8 +198,10 @@ func (nf *nodebufferlist) revert(db ethdb.KeyValueReader, nodes map[common.Hash]
// hang user read/write and background write,
nf.mux.Lock()
nf.baseMux.Lock()
nf.flushMux.Lock()
defer nf.mux.Unlock()
defer nf.baseMux.Unlock()
defer nf.flushMux.Unlock()

merge := func(buffer *multiDifflayer) bool {
if err := nf.base.commit(buffer.root, buffer.id, buffer.block, buffer.layers, buffer.nodes); err != nil {
Expand Down Expand Up @@ -230,8 +233,10 @@ func (nf *nodebufferlist) flush(db ethdb.KeyValueStore, clean *fastcache.Cache,
// hang user read/write and background write
nf.mux.Lock()
nf.baseMux.Lock()
nf.flushMux.Lock()
defer nf.mux.Unlock()
defer nf.baseMux.Unlock()
defer nf.flushMux.Unlock()

nf.stopFlushing.Store(true)
defer nf.stopFlushing.Store(false)
Expand Down Expand Up @@ -482,6 +487,8 @@ func (nf *nodebufferlist) diffToBase() {

// backgroundFlush flush base node buffer to disk.
func (nf *nodebufferlist) backgroundFlush() {
nf.flushMux.Lock()
defer nf.flushMux.Unlock()
nf.baseMux.RLock()
persistID := nf.persistID + nf.base.layers
nf.baseMux.RUnlock()
Expand Down

0 comments on commit 3f89824

Please sign in to comment.