Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove defer in loop #20223

Merged
merged 29 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
248ad76
refactor defer in loop, and add error handling for Close function
tuantran1702 Apr 30, 2024
306cf5b
Update store/storage/storage_test_suite.go
tuantran1702 Apr 30, 2024
7b0116e
Update x/group/keeper/invariants.go
tuantran1702 Apr 30, 2024
e45b71f
Update store/commitment/store.go
tuantran1702 Apr 30, 2024
023247e
Update store/commitment/store.go
tuantran1702 Apr 30, 2024
93c36dc
Update store/migration/manager.go
tuantran1702 Apr 30, 2024
b0c99b4
refactor to use anonymous closure
tuantran1702 May 1, 2024
badd9c5
Merge branch 'refactor-defer' of github.com:notional-labs/cosmos-sdk …
tuantran1702 May 1, 2024
9995356
linting
tuantran1702 May 1, 2024
b6c8dbe
rollback importer defer, bring pw.Close() to closure
tuantran1702 May 1, 2024
097384b
remove incorrect Close()
tuantran1702 May 1, 2024
e7b1a17
refactor
tuantran1702 May 2, 2024
b6a63be
fix redundant err check
tuantran1702 May 2, 2024
95fc4a7
refactor defer in loop, and add error handling for Close function
tuantran1702 Apr 30, 2024
869ec3e
refactor to use anonymous closure
tuantran1702 May 1, 2024
d819ddd
Update store/storage/storage_test_suite.go
tuantran1702 Apr 30, 2024
4543e67
Update store/commitment/store.go
tuantran1702 Apr 30, 2024
fce217a
Update store/commitment/store.go
tuantran1702 Apr 30, 2024
fbaef03
linting
tuantran1702 May 1, 2024
953cfac
rollback importer defer, bring pw.Close() to closure
tuantran1702 May 1, 2024
879999d
remove incorrect Close()
tuantran1702 May 1, 2024
e9df29b
refactor
tuantran1702 May 2, 2024
8701127
fix redundant err check
tuantran1702 May 2, 2024
0415482
add missing import
tuantran1702 May 9, 2024
a737df0
add err handling
tuantran1702 May 9, 2024
2f0ac3f
Merge branch 'main' into refactor-defer
tuantran1702 May 11, 2024
5bc9597
Merge branch 'refactor-defer' of github.com:notional-labs/cosmos-sdk …
tuantran1702 May 11, 2024
076a43f
Merge branch 'main' into refactor-defer
tuantran1702 May 17, 2024
101706b
lint errorf
tuantran1702 May 17, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions store/commitment/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,10 @@ loop:
if err := importer.Commit(); err != nil {
return snapshotstypes.SnapshotItem{}, fmt.Errorf("failed to commit importer: %w", err)
}
importer.Close()
err := importer.Close()
if err != nil {
return snapshotstypes.SnapshotItem{}, err
}
tuantran1702 marked this conversation as resolved.
Show resolved Hide resolved
}

storeKey = item.Store.Name
Expand All @@ -411,7 +414,6 @@ loop:
if err != nil {
return snapshotstypes.SnapshotItem{}, fmt.Errorf("failed to import tree for version %d: %w", version, err)
}
defer importer.Close()
odeke-em marked this conversation as resolved.
Show resolved Hide resolved

case *snapshotstypes.SnapshotItem_IAVL:
if importer == nil {
Expand Down Expand Up @@ -460,6 +462,11 @@ loop:
}
}

err := importer.Close()
if err != nil {
return snapshotstypes.SnapshotItem{}, err
}
tuantran1702 marked this conversation as resolved.
Show resolved Hide resolved

return snapshotItem, c.LoadVersion(version)
}

Expand Down
6 changes: 5 additions & 1 deletion store/migration/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,18 @@ func (m *Manager) writeChangeset() error {
}

batch := m.db.NewBatch()
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)
}

err = batch.Close()
if err != nil {
return err
}
tuantran1702 marked this conversation as resolved.
Show resolved Hide resolved
}

return nil
Expand Down
1 change: 0 additions & 1 deletion store/snapshots/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ func (s *Store) Load(height uint64, format uint32) (*types.Snapshot, <-chan io.R
_ = pw.CloseWithError(err)
return
}
defer chunk.Close()
tuantran1702 marked this conversation as resolved.
Show resolved Hide resolved
_, err = io.Copy(pw, chunk)
if err != nil {
odeke-em marked this conversation as resolved.
Show resolved Hide resolved
_ = pw.CloseWithError(err)
odeke-em marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
14 changes: 10 additions & 4 deletions store/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,18 @@ 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()
if err != nil {
return
}
tuantran1702 marked this conversation as resolved.
Show resolved Hide resolved
}

// 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 +294,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
12 changes: 10 additions & 2 deletions x/group/keeper/invariants.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ func GroupTotalWeightInvariantHelper(ctx sdk.Context, storeService storetypes.KV
msg += fmt.Sprintf("PrefixScan failure on group table\n%v\n", err)
return msg, broken
}
defer groupIt.Close()
defer func(groupIt orm.Iterator) {
err := groupIt.Close()
if err != nil {
return
}
}(groupIt)
tuantran1702 marked this conversation as resolved.
Show resolved Hide resolved

groups := make(map[uint64]group.GroupInfo)
for {
Expand Down Expand Up @@ -76,7 +81,6 @@ func GroupTotalWeightInvariantHelper(ctx sdk.Context, storeService storetypes.KV
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()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your change to move it down below creates an even less likely chance that memIt.Close() due to the large number of branches that could return before it is invoked. You could apply that anonymous closure trick that I showed up above.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much appreciated @odeke-em for your insightful review. I have refactored the code, could you kindly take a look. Thank you!


for {
var groupMember group.GroupMember
Expand Down Expand Up @@ -113,6 +117,10 @@ func GroupTotalWeightInvariantHelper(ctx sdk.Context, storeService storetypes.KV
msg += fmt.Sprintf("group's TotalWeight must be equal to the sum of its members' weights\ngroup weight: %s\nSum of group members weights: %s\n", groupWeight.String(), membersWeight.String())
break
}
err = memIt.Close()
if err != nil {
return "", broken
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure proper error handling when closing the memIt iterator.

-		err = memIt.Close()
-		if err != nil {
-			return "", broken
-		}
+		if err := memIt.Close(); err != nil {
+			return fmt.Sprintf("failed to close member iterator: %v", err), true
+		}

This change ensures that any error during the closing of the memIt iterator is properly handled and reported, which aligns with the PR's objective of improving error handling in resource management.


Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
err = memIt.Close()
if err != nil {
return "", broken
}
if err := memIt.Close(); err != nil {
return fmt.Sprintf("failed to close member iterator: %v", err), true
}

}

return msg, broken
Expand Down
Loading