From 9fda9443d492aaaa5de31e6e1e9bba085be2fc30 Mon Sep 17 00:00:00 2001 From: guangwu Date: Tue, 30 Apr 2024 16:47:10 +0800 Subject: [PATCH] fix(promql/query_logger): close file in error handling (#13948) Signed-off-by: guoguangwu --- promql/query_logger.go | 2 ++ promql/query_logger_test.go | 5 +---- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/promql/query_logger.go b/promql/query_logger.go index fa4e1fb07..7ddd8c2d5 100644 --- a/promql/query_logger.go +++ b/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 } diff --git a/promql/query_logger_test.go b/promql/query_logger_test.go index 4135753fd..376d61b64 100644 --- a/promql/query_logger_test.go +++ b/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")