Browse Source

fix(promql/query_logger): close file in error handling (#13948)

Signed-off-by: guoguangwu <guoguangwug@gmail.com>
pull/13966/head^2
guangwu 7 months ago committed by GitHub
parent
commit
9fda9443d4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 2
      promql/query_logger.go
  2. 5
      promql/query_logger_test.go

2
promql/query_logger.go

@ -96,12 +96,14 @@ func getMMapedFile(filename string, filesize int, logger log.Logger) ([]byte, io
err = file.Truncate(int64(filesize))
if err != nil {
file.Close()
level.Error(logger).Log("msg", "Error setting filesize.", "filesize", filesize, "err", err)
return nil, nil, err
}
fileAsBytes, err := mmap.Map(file, mmap.RDWR, 0)
if err != nil {
file.Close()
level.Error(logger).Log("msg", "Failed to mmap", "file", filename, "Attempted size", filesize, "err", err)
return nil, nil, err
}

5
promql/query_logger_test.go

@ -110,10 +110,7 @@ func TestMMapFile(t *testing.T) {
filename := file.Name()
defer os.Remove(filename)
fileAsBytes, closer, err := getMMapedFile(filename, 2, nil)
if err != nil {
t.Cleanup(func() { closer.Close() })
}
fileAsBytes, _, err := getMMapedFile(filename, 2, nil)
require.NoError(t, err)
copy(fileAsBytes, "ab")

Loading…
Cancel
Save