From 228d0b653a70231ed13bbe81426705ee287a02b7 Mon Sep 17 00:00:00 2001 From: jsvisa Date: Tue, 22 Jul 2025 23:05:53 +0800 Subject: [PATCH 1/4] triedb/pathdb: use a variable Signed-off-by: jsvisa --- triedb/pathdb/history_indexer.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/triedb/pathdb/history_indexer.go b/triedb/pathdb/history_indexer.go index 42103fab32c9..7fbb6c430584 100644 --- a/triedb/pathdb/history_indexer.go +++ b/triedb/pathdb/history_indexer.go @@ -392,16 +392,17 @@ func (i *indexIniter) run(lastID uint64) { select { case signal := <-i.interrupt: // The indexing limit can only be extended or shortened continuously. - if signal.newLastID != lastID+1 && signal.newLastID != lastID-1 { - signal.result <- fmt.Errorf("invalid history id, last: %d, got: %d", lastID, signal.newLastID) + newLastID := signal.newLastID + if newLastID != lastID+1 && newLastID != lastID-1 { + signal.result <- fmt.Errorf("invalid history id, last: %d, got: %d", lastID, newLastID) continue } - i.last.Store(signal.newLastID) // update indexing range + i.last.Store(newLastID) // update indexing range // The index limit is extended by one, update the limit without // interrupting the current background process. - if signal.newLastID == lastID+1 { - lastID = signal.newLastID + if newLastID == lastID+1 { + lastID = newLastID signal.result <- nil log.Debug("Extended state history range", "last", lastID) continue @@ -425,7 +426,7 @@ func (i *indexIniter) run(lastID uint64) { return } // Adjust the indexing target and relaunch the process - lastID = signal.newLastID + lastID = newLastID done, interrupt = make(chan struct{}), new(atomic.Int32) go i.index(done, interrupt, lastID) log.Debug("Shortened state history range", "last", lastID) From 5fe9e85eb9c42a123db886b9a54a66f3428568e9 Mon Sep 17 00:00:00 2001 From: jsvisa Date: Tue, 22 Jul 2025 23:18:44 +0800 Subject: [PATCH 2/4] fix: pass nil to signal.result Signed-off-by: jsvisa --- triedb/pathdb/history_indexer.go | 1 + 1 file changed, 1 insertion(+) diff --git a/triedb/pathdb/history_indexer.go b/triedb/pathdb/history_indexer.go index 7fbb6c430584..ff07f1e438fb 100644 --- a/triedb/pathdb/history_indexer.go +++ b/triedb/pathdb/history_indexer.go @@ -427,6 +427,7 @@ func (i *indexIniter) run(lastID uint64) { } // Adjust the indexing target and relaunch the process lastID = newLastID + signal.result <- nil done, interrupt = make(chan struct{}), new(atomic.Int32) go i.index(done, interrupt, lastID) log.Debug("Shortened state history range", "last", lastID) From a22c7b57455045b4a64e68ba3e0dac1d363ef63c Mon Sep 17 00:00:00 2001 From: jsvisa Date: Tue, 22 Jul 2025 23:18:53 +0800 Subject: [PATCH 3/4] add testcase Signed-off-by: jsvisa --- triedb/pathdb/history_indexer_test.go | 57 +++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 triedb/pathdb/history_indexer_test.go diff --git a/triedb/pathdb/history_indexer_test.go b/triedb/pathdb/history_indexer_test.go new file mode 100644 index 000000000000..2e237a6b0768 --- /dev/null +++ b/triedb/pathdb/history_indexer_test.go @@ -0,0 +1,57 @@ +// Copyright 2025 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see . + +package pathdb + +import ( + "testing" + "time" + + "github.com/ethereum/go-ethereum/core/rawdb" + "github.com/ethereum/go-ethereum/ethdb" +) + +// TestHistoryIndexerShortenDeadlock tests that a call to shorten does not +// deadlock when the indexer is active. This specifically targets the case where +// signal.result must be sent to unblock the caller. +func TestHistoryIndexerShortenDeadlock(t *testing.T) { + db := rawdb.NewMemoryDatabase() + freezer, _ := rawdb.NewStateFreezer(t.TempDir(), false, false) + histories := makeHistories(1000) + + // Assume we only have 100 histories indexed + for i, h := range histories[:100] { + accountData, storageData, accountIndex, storageIndex := h.encode() + rawdb.WriteStateHistory(freezer.(ethdb.AncientWriter), uint64(i+1), h.meta.encode(), accountIndex, storageIndex, accountData, storageData) + } + indexer := newHistoryIndexer(db, freezer, uint64(len(histories))) + defer indexer.close() + defer freezer.Close() + + done := make(chan error, 1) + go func() { + done <- indexer.shorten(uint64(len(histories))) + }() + + select { + case err := <-done: + if err != nil { + t.Fatalf("shorten returned an unexpected error: %v", err) + } + case <-time.After(2 * time.Second): + t.Fatal("timed out waiting for shorten to complete, potential deadlock") + } +} From f0b38d6fae3d0b4fb0c75d27891636b669ae43e5 Mon Sep 17 00:00:00 2001 From: Gary Rong Date: Wed, 23 Jul 2025 11:21:54 +0800 Subject: [PATCH 4/4] triedb/pathdb: polish --- triedb/pathdb/history_indexer.go | 1 + triedb/pathdb/history_indexer_test.go | 16 ++++++++-------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/triedb/pathdb/history_indexer.go b/triedb/pathdb/history_indexer.go index ff07f1e438fb..054d43e946d3 100644 --- a/triedb/pathdb/history_indexer.go +++ b/triedb/pathdb/history_indexer.go @@ -428,6 +428,7 @@ func (i *indexIniter) run(lastID uint64) { // Adjust the indexing target and relaunch the process lastID = newLastID signal.result <- nil + done, interrupt = make(chan struct{}), new(atomic.Int32) go i.index(done, interrupt, lastID) log.Debug("Shortened state history range", "last", lastID) diff --git a/triedb/pathdb/history_indexer_test.go b/triedb/pathdb/history_indexer_test.go index 2e237a6b0768..abfcafc94545 100644 --- a/triedb/pathdb/history_indexer_test.go +++ b/triedb/pathdb/history_indexer_test.go @@ -21,29 +21,29 @@ import ( "time" "github.com/ethereum/go-ethereum/core/rawdb" - "github.com/ethereum/go-ethereum/ethdb" ) // TestHistoryIndexerShortenDeadlock tests that a call to shorten does not // deadlock when the indexer is active. This specifically targets the case where // signal.result must be sent to unblock the caller. func TestHistoryIndexerShortenDeadlock(t *testing.T) { + //log.SetDefault(log.NewLogger(log.NewTerminalHandlerWithLevel(os.Stderr, log.LevelInfo, true))) db := rawdb.NewMemoryDatabase() freezer, _ := rawdb.NewStateFreezer(t.TempDir(), false, false) - histories := makeHistories(1000) + defer freezer.Close() - // Assume we only have 100 histories indexed - for i, h := range histories[:100] { + histories := makeHistories(100) + for i, h := range histories { accountData, storageData, accountIndex, storageIndex := h.encode() - rawdb.WriteStateHistory(freezer.(ethdb.AncientWriter), uint64(i+1), h.meta.encode(), accountIndex, storageIndex, accountData, storageData) + rawdb.WriteStateHistory(freezer, uint64(i+1), h.meta.encode(), accountIndex, storageIndex, accountData, storageData) } - indexer := newHistoryIndexer(db, freezer, uint64(len(histories))) + // As a workaround, assign a future block to keep the initer running indefinitely + indexer := newHistoryIndexer(db, freezer, 200) defer indexer.close() - defer freezer.Close() done := make(chan error, 1) go func() { - done <- indexer.shorten(uint64(len(histories))) + done <- indexer.shorten(200) }() select {