Skip to content

Commit

Permalink
refactor: remove defer in loop (#20223)
Browse files Browse the repository at this point in the history
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
  • Loading branch information
tuantran1702 and coderabbitai[bot] authored May 29, 2024
1 parent e4ec753 commit 2b34f69
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 43 deletions.
17 changes: 12 additions & 5 deletions store/snapshots/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package snapshots
import (
"crypto/sha256"
"encoding/binary"
"fmt"
"hash"
"io"
"math"
Expand Down Expand Up @@ -140,6 +141,7 @@ func (s *Store) Load(height uint64, format uint32) (*types.Snapshot, <-chan io.R
}

ch := make(chan io.ReadCloser)

go func() {
defer close(ch)
for i := uint32(0); i < snapshot.Chunks; i++ {
Expand All @@ -150,14 +152,19 @@ func (s *Store) Load(height uint64, format uint32) (*types.Snapshot, <-chan io.R
_ = pw.CloseWithError(err)
return
}
defer chunk.Close()
_, err = io.Copy(pw, chunk)
err = func() error {
defer chunk.Close()

if _, err := io.Copy(pw, chunk); err != nil {
_ = pw.CloseWithError(err)
return fmt.Errorf("failed to copy chunk %d: %w", i, err)
}

return pw.Close()
}()
if err != nil {
_ = pw.CloseWithError(err)
return
}
chunk.Close()
pw.Close()
}
}()

Expand Down
4 changes: 3 additions & 1 deletion store/v2/commitment/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,9 @@ loop:
if err := importer.Commit(); err != nil {
return snapshotstypes.SnapshotItem{}, fmt.Errorf("failed to commit importer: %w", err)
}
importer.Close()
if err := importer.Close(); err != nil {
return snapshotstypes.SnapshotItem{}, fmt.Errorf("failed to close importer: %w", err)
}
}

storeKey = []byte(item.Store.Name)
Expand Down
20 changes: 15 additions & 5 deletions store/v2/migration/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,21 @@ func (m *Manager) writeChangeset() error {
}

batch := m.db.NewBatch()
if err := batch.Set(csKey, csBytes); err != nil {
return fmt.Errorf("failed to write changeset to db.Batch: %w", err)
}
if err := batch.Write(); err != nil {
return fmt.Errorf("failed to write changeset to db: %w", err)
// Invoking this code in a closure so that defer is called immediately on return
// yet not in the for-loop which can leave resource lingering.
err = func() error {
defer batch.Close()

if err := batch.Set(csKey, csBytes); err != nil {
return fmt.Errorf("failed to write changeset to db.Batch: %w", err)
}
if err := batch.Write(); err != nil {
return fmt.Errorf("failed to write changeset to db: %w", err)
}
return nil
}()
if err != nil {
return err
}
batch.Close()
}
Expand Down
12 changes: 8 additions & 4 deletions store/v2/storage/storage_test_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,6 @@ func (s *StorageTestSuite) TestDatabase_Iterator() {
itr, err := db.Iterator(storeKey1Bytes, v, []byte("key000"), nil)
s.Require().NoError(err)

defer itr.Close()

var i, count int
for ; itr.Valid(); itr.Next() {
s.Require().Equal([]byte(fmt.Sprintf("key%03d", i)), itr.Key(), string(itr.Key()))
Expand All @@ -271,15 +269,16 @@ func (s *StorageTestSuite) TestDatabase_Iterator() {

// seek past domain, which should make the iterator invalid and produce an error
s.Require().False(itr.Valid())

err = itr.Close()
s.Require().NoError(err, "Failed to close iterator")
}

// iterator with a start and end domain over multiple versions
for v := uint64(1); v < 5; v++ {
itr2, err := db.Iterator(storeKey1Bytes, v, []byte("key010"), []byte("key019"))
s.Require().NoError(err)

defer itr2.Close()

i, count := 10, 0
for ; itr2.Valid(); itr2.Next() {
s.Require().Equal([]byte(fmt.Sprintf("key%03d", i)), itr2.Key())
Expand All @@ -293,6 +292,11 @@ func (s *StorageTestSuite) TestDatabase_Iterator() {

// seek past domain, which should make the iterator invalid and produce an error
s.Require().False(itr2.Valid())

err = itr2.Close()
if err != nil {
return
}
}

// start must be <= end
Expand Down
3 changes: 2 additions & 1 deletion tests/integration/auth/client/cli/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,15 @@ func (s *CLITestSuite) TestCLISignBatchTotalFees() {
sdk.NewCoins(sendTokens), clitestutil.TestTxConfig{GenOnly: true})
s.Require().NoError(err)
txFile := testutil.WriteToNewTempFile(s.T(), tx.String()+"\n")
defer txFile.Close()
txFiles[i] = txFile.Name()

unsignedTx, err := txCfg.TxJSONDecoder()(tx.Bytes())
s.Require().NoError(err)
txBuilder, err := txCfg.WrapTxBuilder(unsignedTx)
s.Require().NoError(err)
expectedBatchedTotalFee += txBuilder.GetTx().GetFee().AmountOf(tc.denom).Int64()
err = txFile.Close()
s.NoError(err)
}

// Test batch sign
Expand Down
57 changes: 30 additions & 27 deletions x/group/keeper/invariants.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ func GroupTotalWeightInvariantHelper(ctx sdk.Context, storeService storetypes.KV
msg += fmt.Sprintf("LoadNext failure on group table iterator\n%v\n", err)
return msg, broken
}

groups[groupInfo.Id] = groupInfo
}

groupByIDs := maps.Keys(groups)
sort.Slice(groupByIDs, func(i, j int) bool {
return groupByIDs[i] < groupByIDs[j]
})

for _, groupID := range groupByIDs {
groupInfo := groups[groupID]
membersWeight, err := groupmath.NewNonNegativeDecFromString("0")
Expand All @@ -71,35 +71,38 @@ func GroupTotalWeightInvariantHelper(ctx sdk.Context, storeService storetypes.KV
return msg, broken
}

memIt, err := groupMemberByGroupIndex.Get(kvStore, groupInfo.Id)
if err != nil {
msg += fmt.Sprintf("error while returning group member iterator for group with ID %d\n%v\n", groupInfo.Id, err)
return msg, broken
}
defer memIt.Close()

for {
var groupMember group.GroupMember
_, err = memIt.LoadNext(&groupMember)
if errors.ErrORMIteratorDone.Is(err) {
break
}
if err != nil {
msg += fmt.Sprintf("LoadNext failure on member table iterator\n%v\n", err)
return msg, broken
}

curMemWeight, err := groupmath.NewPositiveDecFromString(groupMember.GetMember().GetWeight())
err = func() error {
memIt, err := groupMemberByGroupIndex.Get(kvStore, groupInfo.Id)
if err != nil {
msg += fmt.Sprintf("error while parsing non-nengative decimal for group member %s\n%v\n", groupMember.Member.Address, err)
return msg, broken
return fmt.Errorf("error while returning group member iterator for group with ID %d\n%w", groupInfo.Id, err)
}

membersWeight, err = groupmath.Add(membersWeight, curMemWeight)
if err != nil {
msg += fmt.Sprintf("decimal addition error while adding group member voting weight to total voting weight\n%v\n", err)
return msg, broken
defer memIt.Close()

for {
var groupMember group.GroupMember
_, err = memIt.LoadNext(&groupMember)
if errors.ErrORMIteratorDone.Is(err) {
break
}
if err != nil {
return fmt.Errorf("LoadNext failure on member table iterator\n%w", err)
}

curMemWeight, err := groupmath.NewPositiveDecFromString(groupMember.GetMember().GetWeight())
if err != nil {
return fmt.Errorf("error while parsing non-nengative decimal for group member %s\n%w", groupMember.Member.Address, err)
}

membersWeight, err = groupmath.Add(membersWeight, curMemWeight)
if err != nil {
return fmt.Errorf("decimal addition error while adding group member voting weight to total voting weight\n%w", err)
}
}
return nil
}()
if err != nil {
msg += err.Error() + "\n"
return msg, broken
}

groupWeight, err := groupmath.NewNonNegativeDecFromString(groupInfo.GetTotalWeight())
Expand Down

0 comments on commit 2b34f69

Please sign in to comment.