Skip to content

fix(store/v2): chunkBody.Close and chunkFile.Close both are called twice #23271

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

Merged
merged 6 commits into from
Jan 21, 2025
Merged
Changes from 1 commit
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
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
Loading