Skip to content

Commit 7b12783

Browse files
committed
fix connection leak (misplaced defer) and perform proper rollback on errs
1 parent e842aad commit 7b12783

File tree

4 files changed

+12
-11
lines changed

4 files changed

+12
-11
lines changed

params/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const (
2424
VersionMajor = 1 // Major version component of the current release
2525
VersionMinor = 10 // Minor version component of the current release
2626
VersionPatch = 2 // Patch version component of the current release
27-
VersionMeta = "statediff-0.0.17" // Version metadata to append to the version string
27+
VersionMeta = "statediff-0.0.18" // Version metadata to append to the version string
2828
)
2929

3030
// Version holds the textual version string.

statediff/indexer/indexer.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ type BlockTx struct {
7373
dbtx *sqlx.Tx
7474
BlockNumber uint64
7575
headerID int64
76-
err error
77-
Close func() error
76+
Close func(err error) error
7877
}
7978

8079
// Reporting function to run as goroutine
@@ -128,11 +127,12 @@ func (sdi *StateDiffIndexer) PushBlock(block *types.Block, receipts types.Receip
128127
blocktx := BlockTx{
129128
dbtx: tx,
130129
// handle transaction commit or rollback for any return case
131-
Close: func() error {
132-
var err error
130+
Close: func(err error) error {
133131
if p := recover(); p != nil {
134132
shared.Rollback(tx)
135133
panic(p)
134+
} else if err != nil {
135+
shared.Rollback(tx)
136136
} else {
137137
tDiff := time.Since(t)
138138
indexerMetrics.tStateStoreCodeProcessing.Update(tDiff)

statediff/indexer/indexer_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func setup(t *testing.T) {
117117
if err != nil {
118118
t.Fatal(err)
119119
}
120-
defer tx.Close()
120+
defer tx.Close(err)
121121
for _, node := range mocks.StateDiffs {
122122
err = ind.PushStateNode(tx, node)
123123
if err != nil {

statediff/service.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,13 @@ import (
2323
"sync"
2424
"sync/atomic"
2525

26-
"github.com/ethereum/go-ethereum/eth/ethconfig"
27-
2826
"github.com/ethereum/go-ethereum/common"
2927
"github.com/ethereum/go-ethereum/core"
3028
"github.com/ethereum/go-ethereum/core/state"
3129
"github.com/ethereum/go-ethereum/core/types"
3230
"github.com/ethereum/go-ethereum/crypto"
3331
"github.com/ethereum/go-ethereum/eth"
32+
"github.com/ethereum/go-ethereum/eth/ethconfig"
3433
"github.com/ethereum/go-ethereum/event"
3534
"github.com/ethereum/go-ethereum/log"
3635
"github.com/ethereum/go-ethereum/metrics"
@@ -626,18 +625,20 @@ func (sds *Service) writeStateDiff(block *types.Block, parentRoot common.Hash, p
626625
// log.Info("Writing state diff", "block height", block.Number().Uint64())
627626
var totalDifficulty *big.Int
628627
var receipts types.Receipts
628+
var err error
629+
var tx *ind.BlockTx
629630
if params.IncludeTD {
630631
totalDifficulty = sds.BlockChain.GetTdByHash(block.Hash())
631632
}
632633
if params.IncludeReceipts {
633634
receipts = sds.BlockChain.GetReceiptsByHash(block.Hash())
634635
}
635-
tx, err := sds.indexer.PushBlock(block, receipts, totalDifficulty)
636+
tx, err = sds.indexer.PushBlock(block, receipts, totalDifficulty)
637+
// defer handling of commit/rollback for any return case
638+
defer tx.Close(err)
636639
if err != nil {
637640
return err
638641
}
639-
// defer handling of commit/rollback for any return case
640-
defer tx.Close()
641642
output := func(node StateNode) error {
642643
return sds.indexer.PushStateNode(tx, node)
643644
}

0 commit comments

Comments
 (0)