Skip to content
This repository was archived by the owner on Aug 13, 2019. It is now read-only.

Commit 520b1d8

Browse files
pborzenkovkrasi-georgiev
authored andcommitted
index: add a test to trigger fd leak on corrupted index (#576)
The test is designed to fail for windows when the function leaves open files.
1 parent 8eeb70f commit 520b1d8

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
- [REMOVED] `FromData` is considered unused so was removed.
55
- [FEATURE] Added option WALSegmentSize -1 to disable the WAL.
66
- [BUGFIX] Fsync the meta file to persist it on disk to avoid data loss in case of a host crash.
7-
- [BUGFIX] Fix fd and vm_area leak on error path in chunks.NewDirReader
7+
- [BUGFIX] Fix fd and vm_area leak on error path in chunks.NewDirReader.
8+
- [BUGFIX] Fix fd and vm_area leak on error path in index.NewFileReader.
89

910
## 0.6.1
1011
- [BUGFIX] Update `last` after appending a non-overlapping chunk in `chunks.MergeOverlappingChunks`. [#539](https://github.com/prometheus/tsdb/pull/539)

index/index.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"github.com/pkg/errors"
3030
"github.com/prometheus/tsdb/chunks"
3131
"github.com/prometheus/tsdb/encoding"
32+
tsdb_errors "github.com/prometheus/tsdb/errors"
3233
"github.com/prometheus/tsdb/fileutil"
3334
"github.com/prometheus/tsdb/labels"
3435
)
@@ -625,7 +626,15 @@ func NewFileReader(path string) (*Reader, error) {
625626
if err != nil {
626627
return nil, err
627628
}
628-
return newReader(realByteSlice(f.Bytes()), f)
629+
r, err := newReader(realByteSlice(f.Bytes()), f)
630+
if err != nil {
631+
var merr tsdb_errors.MultiError
632+
merr.Add(err)
633+
merr.Add(f.Close())
634+
return nil, merr
635+
}
636+
637+
return r, nil
629638
}
630639

631640
func newReader(b ByteSlice, c io.Closer) (*Reader, error) {

index/index_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,3 +412,18 @@ func TestReaderWithInvalidBuffer(t *testing.T) {
412412
_, err := NewReader(b)
413413
testutil.NotOk(t, err)
414414
}
415+
416+
// TestNewFileReaderErrorNoOpenFiles ensures that in case of an error no file remains open.
417+
func TestNewFileReaderErrorNoOpenFiles(t *testing.T) {
418+
dir := testutil.NewTemporaryDirectory("block", t)
419+
420+
idxName := filepath.Join(dir.Path(), "index")
421+
err := ioutil.WriteFile(idxName, []byte("corrupted contents"), 0644)
422+
testutil.Ok(t, err)
423+
424+
_, err = NewFileReader(idxName)
425+
testutil.NotOk(t, err)
426+
427+
// dir.Close will fail on Win if idxName fd is not closed on error path.
428+
dir.Close()
429+
}

0 commit comments

Comments
 (0)