|
| 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