Browse Source

Fix memory leakage in TSyncPageCacheFileLogBackend

Memleak occurred when the storage was running out of space and a TFileError exception was thrown.
In this case, the buffer was not cleared after writing.
srx64 1 year ago
parent
commit
3f83d0726c
1 changed files with 10 additions and 4 deletions
  1. 10 4
      library/cpp/logger/sync_page_cache_file.cpp

+ 10 - 4
library/cpp/logger/sync_page_cache_file.cpp

@@ -2,6 +2,7 @@
 #include "record.h"
 
 #include <util/generic/buffer.h>
+#include <util/generic/yexception.h>
 #include <util/system/file.h>
 #include <util/system/info.h>
 #include <util/system/mutex.h>
@@ -74,10 +75,15 @@ private:
     }
 
     void Write() {
-        File_.Write(Buffer_.Data(), Buffer_.Size());
-        WrittenPtr_ += Buffer_.Size();
-        PageAlignedWrittenPtr_ = AlignDown(WrittenPtr_, GetPageSize());
-        Buffer_.Clear();
+        try {
+            File_.Write(Buffer_.Data(), Buffer_.Size());
+            WrittenPtr_ += Buffer_.Size();
+            PageAlignedWrittenPtr_ = AlignDown(WrittenPtr_, GetPageSize());
+            Buffer_.Clear();
+        } catch (TFileError&) {
+            Buffer_.Clear();
+            throw;
+        }
     }
 
     void FlushAsync(const i64 from, const i64 to) {