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

Commit

Permalink
index: add a test to trigger fd leak on corrupted index (#576)
Browse files Browse the repository at this point in the history
The test is designed to fail for windows when the function leaves open files.
  • Loading branch information
pborzenkov authored and krasi-georgiev committed Apr 3, 2019
1 parent 8eeb70f commit 520b1d8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
- [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
- [BUGFIX] Fix fd and vm_area leak on error path in chunks.NewDirReader.
- [BUGFIX] Fix fd and vm_area leak on error path in index.NewFileReader.

## 0.6.1
- [BUGFIX] Update `last` after appending a non-overlapping chunk in `chunks.MergeOverlappingChunks`. [#539](https://github.com/prometheus/tsdb/pull/539)
Expand Down
11 changes: 10 additions & 1 deletion index/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/pkg/errors"
"github.com/prometheus/tsdb/chunks"
"github.com/prometheus/tsdb/encoding"
tsdb_errors "github.com/prometheus/tsdb/errors"
"github.com/prometheus/tsdb/fileutil"
"github.com/prometheus/tsdb/labels"
)
Expand Down Expand Up @@ -625,7 +626,15 @@ func NewFileReader(path string) (*Reader, error) {
if err != nil {
return nil, err
}
return newReader(realByteSlice(f.Bytes()), f)
r, err := newReader(realByteSlice(f.Bytes()), f)
if err != nil {
var merr tsdb_errors.MultiError
merr.Add(err)
merr.Add(f.Close())
return nil, merr
}

return r, nil
}

func newReader(b ByteSlice, c io.Closer) (*Reader, error) {
Expand Down
15 changes: 15 additions & 0 deletions index/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,3 +412,18 @@ func TestReaderWithInvalidBuffer(t *testing.T) {
_, err := NewReader(b)
testutil.NotOk(t, err)
}

// TestNewFileReaderErrorNoOpenFiles ensures that in case of an error no file remains open.
func TestNewFileReaderErrorNoOpenFiles(t *testing.T) {
dir := testutil.NewTemporaryDirectory("block", t)

idxName := filepath.Join(dir.Path(), "index")
err := ioutil.WriteFile(idxName, []byte("corrupted contents"), 0644)
testutil.Ok(t, err)

_, err = NewFileReader(idxName)
testutil.NotOk(t, err)

// dir.Close will fail on Win if idxName fd is not closed on error path.
dir.Close()
}

0 comments on commit 520b1d8

Please sign in to comment.