Skip to content

Commit 89540b1

Browse files
committed
add locking aroung db.dirties access
1 parent 7c1903d commit 89540b1

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

trie/triedb/hashdb/database.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ func (db *Database) Cap(limit common.StorageSize) error {
329329
// outside code doesn't see an inconsistent state (referenced data removed from
330330
// memory cache during commit but not yet in persistent storage). This is ensured
331331
// by only uncaching existing data when the database write finalizes.
332+
db.lock.RLock()
332333
nodes, storage, start := len(db.dirties), db.dirtiesSize, time.Now()
333334
batch := db.diskdb.NewBatch()
334335

@@ -337,12 +338,15 @@ func (db *Database) Cap(limit common.StorageSize) error {
337338
// counted.
338339
size := db.dirtiesSize + common.StorageSize(len(db.dirties)*cachedNodeSize)
339340
size += db.childrenSize
341+
db.lock.RUnlock()
340342

341343
// Keep committing nodes from the flush-list until we're below allowance
342344
oldest := db.oldest
343345
for size > limit && oldest != (common.Hash{}) {
344346
// Fetch the oldest referenced node and push into the batch
347+
db.lock.RLock()
345348
node := db.dirties[oldest]
349+
db.lock.RUnlock()
346350
rawdb.WriteLegacyTrieNode(batch, oldest, node.node)
347351

348352
// If we exceeded the ideal batch size, commit and reset
@@ -418,7 +422,9 @@ func (db *Database) Commit(node common.Hash, report bool) error {
418422
batch := db.diskdb.NewBatch()
419423

420424
// Move the trie itself into the batch, flushing if enough data is accumulated
425+
db.lock.RLock()
421426
nodes, storage := len(db.dirties), db.dirtiesSize
427+
db.lock.RUnlock()
422428

423429
uncacher := &cleaner{db}
424430
if err := db.commit(node, batch, uncacher); err != nil {

0 commit comments

Comments
 (0)