From 4f204dcbc15056cdd047b06ac6111becda5c2915 Mon Sep 17 00:00:00 2001 From: Pavel Borzenkov Date: Tue, 2 Apr 2019 15:16:29 +0300 Subject: [PATCH] chunks: close all opened MmapFiles on NewDirReader error path (#575) Signed-off-by: Pavel Borzenkov --- CHANGELOG.md | 1 + chunks/chunks.go | 10 ++++++---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 80f502304..787a180c4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ - [REMOVED] `FromData` is considered unused so was removed. - [FEATURE] Added option WALSegmentSize -1 to disable the WAL. - [BUGFIX] Fsync the meta file to persist it on disk to avoid data loss in case of a host crash. + - [BUGFIX] Fix fd and vm_area leak on error path in chunks.NewDirReader ## 0.6.1 - [BUGFIX] Update `last` after appending a non-overlapping chunk in `chunks.MergeOverlappingChunks`. [#539](https://github.com/prometheus/tsdb/pull/539) diff --git a/chunks/chunks.go b/chunks/chunks.go index 9e7540707..2d93d7c33 100644 --- a/chunks/chunks.go +++ b/chunks/chunks.go @@ -408,13 +408,16 @@ func NewDirReader(dir string, pool chunkenc.Pool) (*Reader, error) { } var ( - bs []ByteSlice - cs []io.Closer + bs []ByteSlice + cs []io.Closer + merr tsdb_errors.MultiError ) for _, fn := range files { f, err := fileutil.OpenMmapFile(fn) if err != nil { - return nil, errors.Wrapf(err, "mmap files") + merr.Add(errors.Wrap(err, "mmap files")) + merr.Add(closeAll(cs)) + return nil, merr } cs = append(cs, f) bs = append(bs, realByteSlice(f.Bytes())) @@ -422,7 +425,6 @@ func NewDirReader(dir string, pool chunkenc.Pool) (*Reader, error) { reader, err := newReader(bs, cs, pool) if err != nil { - var merr tsdb_errors.MultiError merr.Add(err) merr.Add(closeAll(cs)) return nil, merr