Skip to content
New issue

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

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

Already on GitHub? # to your account

fix: correctly close iterator in 07-tendermint #3022

Merged
merged 3 commits into from
Jan 18, 2023
Merged
Changes from 1 commit
Commits
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
3 changes: 2 additions & 1 deletion modules/light-clients/07-tendermint/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ func IterateConsensusMetadata(store sdk.KVStore, cb func(key, val []byte) bool)
iterator := sdk.KVStorePrefixIterator(store, []byte(host.KeyConsensusStatePrefix))

// iterate over processed time and processed height
defer iterator.Close()
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the defer pattern is still better to use, I worry that calling iterator.Close() could later be accidentally re-converted to a defer statement as it is typically more idiomatic. We can make defer version work in a few ways, either create a new variable to defer

e.g.

iter := iterator
defer iter.Close()

or use a new variable name below rather than re-assign. (I don't see any reason why we would need to re-assign below.)

WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice suggestion, I agree sticking to the defer pattern is probably best. I've pushed an update using a new variable instead of reassignment.

for ; iterator.Valid(); iterator.Next() {
keySplit := strings.Split(string(iterator.Key()), "/")
// processed time key in prefix store has format: "consensusState/<height>/processedTime"
Expand All @@ -99,6 +98,8 @@ func IterateConsensusMetadata(store sdk.KVStore, cb func(key, val []byte) bool)
}
}

iterator.Close()

// iterate over iteration keys
iterator = sdk.KVStorePrefixIterator(store, []byte(KeyIterateConsensusStatePrefix))

Expand Down