From 58f65e4bdecd97f4099caf28441713ee99b2adf0 Mon Sep 17 00:00:00 2001 From: "will@2012" Date: Mon, 22 Apr 2024 12:02:09 +0800 Subject: [PATCH 1/2] fix: fix base buffer concurrent read/write race --- trie/triedb/pathdb/nodebufferlist.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/trie/triedb/pathdb/nodebufferlist.go b/trie/triedb/pathdb/nodebufferlist.go index 2a344003be..fbc0d394a3 100644 --- a/trie/triedb/pathdb/nodebufferlist.go +++ b/trie/triedb/pathdb/nodebufferlist.go @@ -122,6 +122,7 @@ func newNodeBufferList( // node retrieves the trie node with given node info. func (nf *nodebufferlist) node(owner common.Hash, path []byte, hash common.Hash) (node *trienode.Node, err error) { nf.mux.RLock() + defer nf.mux.RUnlock() find := func(nc *multiDifflayer) bool { subset, ok := nc.nodes[owner] if !ok { @@ -141,14 +142,11 @@ func (nf *nodebufferlist) node(owner common.Hash, path []byte, hash common.Hash) } nf.traverse(find) if err != nil { - nf.mux.RUnlock() return nil, err } if node != nil { - nf.mux.RUnlock() return node, nil } - nf.mux.RUnlock() nf.baseMux.RLock() node, err = nf.base.node(owner, path, hash) @@ -602,10 +600,13 @@ func (w *proposedBlockReader) Node(owner common.Hash, path []byte, hash common.H current = current.next } + w.nf.baseMux.RLock() node, err := w.nf.base.node(owner, path, hash) if err != nil { + w.nf.baseMux.RUnlock() return nil, err } + w.nf.baseMux.RUnlock() if node != nil { return node.Blob, nil } From bcb9aa95411324c0a5f5c84ff611c62cc36e9dd9 Mon Sep 17 00:00:00 2001 From: "will@2012" Date: Mon, 22 Apr 2024 12:32:01 +0800 Subject: [PATCH 2/2] chore: update by review tips --- trie/triedb/pathdb/nodebufferlist.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/trie/triedb/pathdb/nodebufferlist.go b/trie/triedb/pathdb/nodebufferlist.go index fbc0d394a3..af9bd828f9 100644 --- a/trie/triedb/pathdb/nodebufferlist.go +++ b/trie/triedb/pathdb/nodebufferlist.go @@ -602,11 +602,10 @@ func (w *proposedBlockReader) Node(owner common.Hash, path []byte, hash common.H w.nf.baseMux.RLock() node, err := w.nf.base.node(owner, path, hash) + w.nf.baseMux.RUnlock() if err != nil { - w.nf.baseMux.RUnlock() return nil, err } - w.nf.baseMux.RUnlock() if node != nil { return node.Blob, nil }