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

fix(store/v2): using defer to ensure close db handle #20871

Merged
merged 3 commits into from
Jul 17, 2024
Merged
Changes from all commits
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
10 changes: 8 additions & 2 deletions store/v2/commitment/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,19 @@ func (m *MetadataStore) GetCommitInfo(version uint64) (*proof.CommitInfo, error)
return cInfo, nil
}

func (m *MetadataStore) flushCommitInfo(version uint64, cInfo *proof.CommitInfo) error {
func (m *MetadataStore) flushCommitInfo(version uint64, cInfo *proof.CommitInfo) (err error) {
// do nothing if commit info is nil, as will be the case for an empty, initializing store
if cInfo == nil {
return nil
}

batch := m.kv.NewBatch()
defer func() {
cErr := batch.Close()
if err == nil {
err = cErr
}
}()
cInfoKey := []byte(fmt.Sprintf(commitInfoKeyFmt, version))
value, err := cInfo.Marshal()
if err != nil {
Expand All @@ -87,7 +93,7 @@ func (m *MetadataStore) flushCommitInfo(version uint64, cInfo *proof.CommitInfo)
if err := batch.WriteSync(); err != nil {
return err
}
return batch.Close()
return nil
Copy link
Collaborator

Choose a reason for hiding this comment

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

This change is no longer correct though. The proper fix when you introduce defer would be to use a named return variable for the error and then in the defer check if the return error was already nil or not then set it so

func (m *MetadataStore) flushCommitInfo(version uint64, cInfo *proof.CommitInfo) (err error) {
      defer func() {
        cErr := batch.Close()
        if err == nil {
           err = cErr
        }
     }()

     ...
     return nil
}

Copy link
Contributor Author

@yukionfire yukionfire Jul 9, 2024

Choose a reason for hiding this comment

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

Yep, we should change to this if we care about the error of batch.Close.

BTW, if we care about the cErr, I think it's better to wrap it with err(if err is not nil)?

}

func (m *MetadataStore) deleteCommitInfo(version uint64) error {
Expand Down
Loading