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

Co-authored-by: Alex | Interchain Labs <alex@skip.money>
Co-authored-by: Alex | Interchain Labs <alex@interchainlabs.io>
This commit is contained in:
lfz941 2025-01-22 07:07:05 +08:00 committed by GitHub
parent c7b878c6eb
commit 9f71028ed2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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
}