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

Signed-off-by: guoguangwu <guoguangwug@gmail.com>
This commit is contained in:
guangwu 2024-04-30 16:47:10 +08:00 committed by GitHub
parent 34ee8c6078
commit 9fda9443d4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 3 additions and 4 deletions

View file

@ -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
}

View file

@ -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")