refactor(store/v2): handle the error returned by batch.Close correctly (#21095)

This commit is contained in:
lfz941 2024-07-31 11:37:18 +08:00 committed by GitHub
parent e5e2dafca3
commit 6eea0ae6d2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -183,8 +183,13 @@ func (m *Manager) writeChangeset() error {
batch := m.db.NewBatch()
// 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()
err = func() (err error) {
defer func() {
cErr := batch.Close()
if err == nil {
err = cErr
}
}()
if err := batch.Set(csKey, csBytes); err != nil {
return fmt.Errorf("failed to write changeset to db.Batch: %w", err)