Skip to content

Commit

Permalink
fix(store/v2): chunkBody.Close and chunkFile.Close both are called twice
Browse files Browse the repository at this point in the history
  • Loading branch information
lfz941 committed Jan 10, 2025
1 parent 517839b commit dbfa2d8
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions store/v2/snapshots/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,29 +298,30 @@ func (s *Store) Save(
// saveChunk saves the given chunkBody with the given index to its appropriate path on disk.
// The hash of the chunk is appended to the snapshot's metadata,
// and the overall snapshot hash is updated with the chunk content too.
func (s *Store) saveChunk(chunkBody io.ReadCloser, index uint32, snapshot *types.Snapshot, chunkHasher, snapshotHasher hash.Hash) error {
defer chunkBody.Close()
func (s *Store) saveChunk(chunkBody io.ReadCloser, index uint32, snapshot *types.Snapshot, chunkHasher, snapshotHasher hash.Hash) (err error) {
defer func() {
if cErr := chunkBody.Close(); cErr != nil {
err = errors.Wrapf(cErr, "failed to close snapshot chunk body %d", index)
}
}()

path := s.PathChunk(snapshot.Height, snapshot.Format, index)
chunkFile, err := os.Create(path)
if err != nil {
return errors.Wrapf(err, "failed to create snapshot chunk file %q", path)
}
defer chunkFile.Close()

defer func() {
if cErr := chunkFile.Close(); cErr != nil {
err = errors.Wrapf(cErr, "failed to close snapshot chunk file %d", index)
}
}()

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

if err := chunkFile.Close(); err != nil {
return errors.Wrapf(err, "failed to close snapshot chunk file %d", index)
}

if err := chunkBody.Close(); err != nil {
return errors.Wrapf(err, "failed to close snapshot chunk body %d", index)
}

snapshot.Metadata.ChunkHashes = append(snapshot.Metadata.ChunkHashes, chunkHasher.Sum(nil))
return nil
}
Expand Down

0 comments on commit dbfa2d8

Please sign in to comment.