Skip to content

Commit

Permalink
Fix goroutine leaks in core/ tests (#273)
Browse files Browse the repository at this point in the history
  • Loading branch information
darioush authored Jul 7, 2023
1 parent fbd9dad commit 419b262
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 5 deletions.
1 change: 1 addition & 0 deletions core/blockchain_log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func TestEmitLogsCorrectness(t *testing.T) {

chain, err := NewBlockChain(rawdb.NewMemoryDatabase(), DefaultCacheConfig, gspec, engine, vm.Config{}, common.Hash{}, false)
require.NoError(err)
defer chain.Stop()

// Create Log Subscriber
logsCh := make(chan []*types.Log, 10)
Expand Down
1 change: 1 addition & 0 deletions core/blockchain_repair_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,7 @@ func testRepair(t *testing.T, tt *rewindTest, snapshots bool) {
if err != nil {
t.Fatalf("Failed to create chain: %v", err)
}
defer chain.Stop()
lastAcceptedHash := chain.GetBlockByNumber(0).Hash()

// If sidechain blocks are needed, make a light chain and import it
Expand Down
10 changes: 8 additions & 2 deletions core/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,11 +355,14 @@ func TestBlockChainOfflinePruningUngracefulShutdown(t *testing.T) {
return blockchain, nil
}

tempDir := t.TempDir()
if err := blockchain.CleanBlockRootsAboveLastAccepted(); err != nil {
return nil, err
}
// get the target root to prune to before stopping the blockchain
targetRoot := blockchain.LastAcceptedBlock().Root()
blockchain.Stop()

tempDir := t.TempDir()
prunerConfig := pruner.Config{
Datadir: tempDir,
BloomSize: 256,
Expand All @@ -371,7 +374,6 @@ func TestBlockChainOfflinePruningUngracefulShutdown(t *testing.T) {
return nil, fmt.Errorf("offline pruning failed (%s, %d): %w", tempDir, 256, err)
}

targetRoot := blockchain.LastAcceptedBlock().Root()
if err := pruner.Prune(targetRoot); err != nil {
return nil, fmt.Errorf("failed to prune blockchain with target root: %s due to: %w", targetRoot, err)
}
Expand Down Expand Up @@ -465,6 +467,7 @@ func testRepopulateMissingTriesParallel(t *testing.T, parallelism int) {
if err != nil {
t.Fatal(err)
}
defer blockchain.Stop()

for _, block := range chain {
if !blockchain.HasState(block.Root()) {
Expand Down Expand Up @@ -951,6 +954,7 @@ func testCreateThenDelete(t *testing.T, config *params.ChainConfig) {
if err != nil {
t.Fatalf("failed to create tester chain: %v", err)
}
defer chain.Stop()
// Import the blocks
for _, block := range blocks {
if _, err := chain.InsertChain([]*types.Block{block}); err != nil {
Expand Down Expand Up @@ -1037,6 +1041,7 @@ func TestTransientStorageReset(t *testing.T) {
if err != nil {
t.Fatalf("failed to create tester chain: %v", err)
}
defer chain.Stop()
// Import the blocks
if _, err := chain.InsertChain(blocks); err != nil {
t.Fatalf("failed to insert into chain: %v", err)
Expand Down Expand Up @@ -1125,6 +1130,7 @@ func TestEIP3651(t *testing.T) {
if err != nil {
t.Fatalf("failed to create tester chain: %v", err)
}
defer chain.Stop()
if n, err := chain.InsertChain(blocks); err != nil {
t.Fatalf("block %d: failed to insert into chain: %v", n, err)
}
Expand Down
2 changes: 2 additions & 0 deletions core/headerchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ func TestHeaderInsertion(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer chain.Stop()

// chain A: G->A1->A2...A128
chainA, _, _ := GenerateChain(params.TestChainConfig, types.NewBlockWithHeader(genesis.Header()), dummy.NewFaker(), db, 128, 10, func(i int, b *BlockGen) {
b.SetCoinbase(common.Address{0: byte(10), 19: byte(i)})
Expand Down
22 changes: 22 additions & 0 deletions core/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// (c) 2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package core

import (
"testing"

"go.uber.org/goleak"
)

// TestMain uses goleak to verify tests in this package do not leak unexpected
// goroutines.
func TestMain(m *testing.M) {
opts := []goleak.Option{
// No good way to shut down these goroutines:
goleak.IgnoreTopFunction("github.com/ava-labs/coreth/core/state/snapshot.(*diskLayer).generate"),
goleak.IgnoreTopFunction("github.com/ava-labs/coreth/metrics.(*meterArbiter).tick"),
goleak.IgnoreTopFunction("github.com/syndtr/goleveldb/leveldb.(*DB).mpoolDrain"),
}
goleak.VerifyTestMain(m, opts...)
}
1 change: 1 addition & 0 deletions core/test_blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ func checkBlockChainState(
if err != nil {
t.Fatalf("Failed to create new blockchain instance: %s", err)
}
defer newBlockChain.Stop()

for i := uint64(1); i <= lastAcceptedBlock.NumberU64(); i++ {
block := bc.GetBlockByNumber(i)
Expand Down
3 changes: 2 additions & 1 deletion core/txpool/txpool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ func (bc *testBlockChain) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent)
}

func (bc *testBlockChain) SenderCacher() *core.TxSenderCacher {
return core.NewTxSenderCacher(1)
// Zero threads avoids starting goroutines.
return core.NewTxSenderCacher(0)
}

func transaction(nonce uint64, gaslimit uint64, key *ecdsa.PrivateKey) *types.Transaction {
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ require (
github.com/syndtr/goleveldb v1.0.1-0.20220614013038-64ee5596c38a
github.com/tyler-smith/go-bip39 v1.1.0
github.com/urfave/cli/v2 v2.17.2-0.20221006022127-8f469abc00aa
go.uber.org/goleak v1.2.1
golang.org/x/crypto v0.1.0
golang.org/x/exp v0.0.0-20230206171751-46f607a40771
golang.org/x/sync v0.1.0
golang.org/x/sys v0.8.0
golang.org/x/text v0.7.0
Expand Down Expand Up @@ -120,7 +122,6 @@ require (
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
go.uber.org/zap v1.24.0 // indirect
golang.org/x/exp v0.0.0-20230206171751-46f607a40771 // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/term v0.7.0 // indirect
gonum.org/v1/gonum v0.11.0 // indirect
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,8 @@ go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/atomic v1.10.0 h1:9qC72Qh0+3MqyJbAn8YU5xVq1frD8bn3JtD2oXtafVQ=
go.uber.org/atomic v1.10.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
go.uber.org/goleak v1.1.12 h1:gZAh5/EyT/HQwlpkCy6wTpqfH9H8Lz8zbm3dZh+OyzA=
go.uber.org/goleak v1.2.1 h1:NBol2c7O1ZokfZ0LEU9K6Whx/KnwvepVetCUhtKja4A=
go.uber.org/goleak v1.2.1/go.mod h1:qlT2yGI9QafXHhZZLxlSuNsMw3FFLxBr+tBRlmO1xH4=
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
go.uber.org/multierr v1.8.0 h1:dg6GjLku4EH+249NNmoIciG9N/jURbDG+pFlTkhzIC8=
go.uber.org/multierr v1.8.0/go.mod h1:7EAYxJLBy9rStEaz58O2t4Uvip6FSURkq8/ppBp95ak=
Expand Down

0 comments on commit 419b262

Please sign in to comment.