trie: fix mutex flaw

This commit is contained in:
Martin Holst Swende 2024-10-04 09:49:00 +02:00
parent 97de2299fd
commit 4abe141814
No known key found for this signature in database
GPG key ID: 683B438C05A5DDF0

View file

@ -97,6 +97,7 @@ func (c *committer) commit(path []byte, n node, topmost bool) node {
func (c *committer) commitChildren(path []byte, n *fullNode, parallel bool) [17]node { func (c *committer) commitChildren(path []byte, n *fullNode, parallel bool) [17]node {
var ( var (
wg sync.WaitGroup wg sync.WaitGroup
nodesMu sync.Mutex
children [17]node children [17]node
) )
for i := 0; i < 16; i++ { for i := 0; i < 16; i++ {
@ -118,15 +119,14 @@ func (c *committer) commitChildren(path []byte, n *fullNode, parallel bool) [17]
children[i] = c.commit(append(path, byte(i)), child, false) children[i] = c.commit(append(path, byte(i)), child, false)
} else { } else {
wg.Add(1) wg.Add(1)
var nodesMu sync.Mutex
go func(index int) { go func(index int) {
p := append(path, byte(i)) p := append(path, byte(i))
set := trienode.NewNodeSet(c.nodes.Owner) childSet := trienode.NewNodeSet(c.nodes.Owner)
childComitter := newCommitter(set, c.tracer, c.collectLeaf, false) childComitter := newCommitter(childSet, c.tracer, c.collectLeaf, false)
h := childComitter.commit(p, child, false) h := childComitter.commit(p, child, false)
children[index] = h children[index] = h
nodesMu.Lock() nodesMu.Lock()
c.nodes.MergeSet(set) c.nodes.MergeSet(childSet)
nodesMu.Unlock() nodesMu.Unlock()
wg.Done() wg.Done()
}(i) }(i)