Skip to content

Commit dbfa2d8

Browse files
committed
fix(store/v2): chunkBody.Close and chunkFile.Close both are called twice
1 parent 517839b commit dbfa2d8

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

store/v2/snapshots/store.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -298,29 +298,30 @@ func (s *Store) Save(
298298
// saveChunk saves the given chunkBody with the given index to its appropriate path on disk.
299299
// The hash of the chunk is appended to the snapshot's metadata,
300300
// and the overall snapshot hash is updated with the chunk content too.
301-
func (s *Store) saveChunk(chunkBody io.ReadCloser, index uint32, snapshot *types.Snapshot, chunkHasher, snapshotHasher hash.Hash) error {
302-
defer chunkBody.Close()
301+
func (s *Store) saveChunk(chunkBody io.ReadCloser, index uint32, snapshot *types.Snapshot, chunkHasher, snapshotHasher hash.Hash) (err error) {
302+
defer func() {
303+
if cErr := chunkBody.Close(); cErr != nil {
304+
err = errors.Wrapf(cErr, "failed to close snapshot chunk body %d", index)
305+
}
306+
}()
303307

304308
path := s.PathChunk(snapshot.Height, snapshot.Format, index)
305309
chunkFile, err := os.Create(path)
306310
if err != nil {
307311
return errors.Wrapf(err, "failed to create snapshot chunk file %q", path)
308312
}
309-
defer chunkFile.Close()
313+
314+
defer func() {
315+
if cErr := chunkFile.Close(); cErr != nil {
316+
err = errors.Wrapf(cErr, "failed to close snapshot chunk file %d", index)
317+
}
318+
}()
310319

311320
chunkHasher.Reset()
312321
if _, err := io.Copy(io.MultiWriter(chunkFile, chunkHasher, snapshotHasher), chunkBody); err != nil {
313322
return errors.Wrapf(err, "failed to generate snapshot chunk %d", index)
314323
}
315324

316-
if err := chunkFile.Close(); err != nil {
317-
return errors.Wrapf(err, "failed to close snapshot chunk file %d", index)
318-
}
319-
320-
if err := chunkBody.Close(); err != nil {
321-
return errors.Wrapf(err, "failed to close snapshot chunk body %d", index)
322-
}
323-
324325
snapshot.Metadata.ChunkHashes = append(snapshot.Metadata.ChunkHashes, chunkHasher.Sum(nil))
325326
return nil
326327
}

0 commit comments

Comments
 (0)