Skip to content

Commit 16117eb

Browse files
jsvisarjl493456442
andauthored
triedb/pathdb: fix an deadlock in history indexer (ethereum#32260)
Seems the `signal.result` was not sent back in shorten case, this will cause a deadlock. --------- Signed-off-by: jsvisa <[email protected]> Co-authored-by: Gary Rong <[email protected]>
1 parent 3b67602 commit 16117eb

File tree

2 files changed

+66
-6
lines changed

2 files changed

+66
-6
lines changed

triedb/pathdb/history_indexer.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -392,16 +392,17 @@ func (i *indexIniter) run(lastID uint64) {
392392
select {
393393
case signal := <-i.interrupt:
394394
// The indexing limit can only be extended or shortened continuously.
395-
if signal.newLastID != lastID+1 && signal.newLastID != lastID-1 {
396-
signal.result <- fmt.Errorf("invalid history id, last: %d, got: %d", lastID, signal.newLastID)
395+
newLastID := signal.newLastID
396+
if newLastID != lastID+1 && newLastID != lastID-1 {
397+
signal.result <- fmt.Errorf("invalid history id, last: %d, got: %d", lastID, newLastID)
397398
continue
398399
}
399-
i.last.Store(signal.newLastID) // update indexing range
400+
i.last.Store(newLastID) // update indexing range
400401

401402
// The index limit is extended by one, update the limit without
402403
// interrupting the current background process.
403-
if signal.newLastID == lastID+1 {
404-
lastID = signal.newLastID
404+
if newLastID == lastID+1 {
405+
lastID = newLastID
405406
signal.result <- nil
406407
log.Debug("Extended state history range", "last", lastID)
407408
continue
@@ -425,7 +426,9 @@ func (i *indexIniter) run(lastID uint64) {
425426
return
426427
}
427428
// Adjust the indexing target and relaunch the process
428-
lastID = signal.newLastID
429+
lastID = newLastID
430+
signal.result <- nil
431+
429432
done, interrupt = make(chan struct{}), new(atomic.Int32)
430433
go i.index(done, interrupt, lastID)
431434
log.Debug("Shortened state history range", "last", lastID)

triedb/pathdb/history_indexer_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 2025 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package pathdb
18+
19+
import (
20+
"testing"
21+
"time"
22+
23+
"github.com/ethereum/go-ethereum/core/rawdb"
24+
)
25+
26+
// TestHistoryIndexerShortenDeadlock tests that a call to shorten does not
27+
// deadlock when the indexer is active. This specifically targets the case where
28+
// signal.result must be sent to unblock the caller.
29+
func TestHistoryIndexerShortenDeadlock(t *testing.T) {
30+
//log.SetDefault(log.NewLogger(log.NewTerminalHandlerWithLevel(os.Stderr, log.LevelInfo, true)))
31+
db := rawdb.NewMemoryDatabase()
32+
freezer, _ := rawdb.NewStateFreezer(t.TempDir(), false, false)
33+
defer freezer.Close()
34+
35+
histories := makeHistories(100)
36+
for i, h := range histories {
37+
accountData, storageData, accountIndex, storageIndex := h.encode()
38+
rawdb.WriteStateHistory(freezer, uint64(i+1), h.meta.encode(), accountIndex, storageIndex, accountData, storageData)
39+
}
40+
// As a workaround, assign a future block to keep the initer running indefinitely
41+
indexer := newHistoryIndexer(db, freezer, 200)
42+
defer indexer.close()
43+
44+
done := make(chan error, 1)
45+
go func() {
46+
done <- indexer.shorten(200)
47+
}()
48+
49+
select {
50+
case err := <-done:
51+
if err != nil {
52+
t.Fatalf("shorten returned an unexpected error: %v", err)
53+
}
54+
case <-time.After(2 * time.Second):
55+
t.Fatal("timed out waiting for shorten to complete, potential deadlock")
56+
}
57+
}

0 commit comments

Comments
 (0)