From 7d2a6b99e4d6c22aded00a1f77d9b6e77f5a2119 Mon Sep 17 00:00:00 2001 From: yukionfire Date: Wed, 17 Jul 2024 23:11:37 +0800 Subject: [PATCH] fix(store/v2): using `defer` to ensure close db handle (#20871) --- store/v2/commitment/metadata.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/store/v2/commitment/metadata.go b/store/v2/commitment/metadata.go index 258f32672d..6b98ef2257 100644 --- a/store/v2/commitment/metadata.go +++ b/store/v2/commitment/metadata.go @@ -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 { @@ -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 } func (m *MetadataStore) deleteCommitInfo(version uint64) error {