diff --git a/tsdb/chunks/chunks.go b/tsdb/chunks/chunks.go index ffd3e5302..5a824d965 100644 --- a/tsdb/chunks/chunks.go +++ b/tsdb/chunks/chunks.go @@ -217,7 +217,7 @@ func (w *Writer) cut() error { return nil } -func cutSegmentFile(dirFile *os.File, magicNumber uint32, chunksFormat byte, allocSize int64) (headerSize int, newFile *os.File, seq int, err error) { +func cutSegmentFile(dirFile *os.File, magicNumber uint32, chunksFormat byte, allocSize int64) (headerSize int, newFile *os.File, seq int, returnErr error) { p, seq, err := nextSequenceFile(dirFile.Name()) if err != nil { return 0, nil, 0, errors.Wrap(err, "next sequence file") @@ -227,6 +227,18 @@ func cutSegmentFile(dirFile *os.File, magicNumber uint32, chunksFormat byte, all if err != nil { return 0, nil, 0, errors.Wrap(err, "open temp file") } + defer func() { + if returnErr != nil { + var merr tsdb_errors.MultiError + merr.Add(returnErr) + if f != nil { + merr.Add(f.Close()) + } + // Calling RemoveAll on a non-existent file does not return error. + merr.Add(os.RemoveAll(ptmp)) + returnErr = merr.Err() + } + }() if allocSize > 0 { if err = fileutil.Preallocate(f, allocSize, true); err != nil { return 0, nil, 0, errors.Wrap(err, "preallocate") @@ -248,6 +260,7 @@ func cutSegmentFile(dirFile *os.File, magicNumber uint32, chunksFormat byte, all if err := f.Close(); err != nil { return 0, nil, 0, errors.Wrap(err, "close temp file") } + f = nil if err := fileutil.Rename(ptmp, p); err != nil { return 0, nil, 0, errors.Wrap(err, "replace file") @@ -257,7 +270,7 @@ func cutSegmentFile(dirFile *os.File, magicNumber uint32, chunksFormat byte, all if err != nil { return 0, nil, 0, errors.Wrap(err, "open final file") } - // Skip the header for the further writes. + // Skip header for further writes. if _, err := f.Seek(int64(n), 0); err != nil { return 0, nil, 0, errors.Wrap(err, "seek in final file") }