From b6d7562cc22144d7a4ab236ae525a0130a0c7209 Mon Sep 17 00:00:00 2001 From: Azat Khuzhin Date: Wed, 14 Apr 2021 23:32:13 +0300 Subject: [PATCH 1/4] Update comments for memoryTrackerCanThrow() --- src/Common/MemoryTracker.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Common/MemoryTracker.cpp b/src/Common/MemoryTracker.cpp index a584885cf0f..60fb4d06b14 100644 --- a/src/Common/MemoryTracker.cpp +++ b/src/Common/MemoryTracker.cpp @@ -24,8 +24,8 @@ namespace /// /// - when it is explicitly blocked with LockExceptionInThread /// -/// - to avoid std::terminate(), when stack unwinding is currently in progress -/// in this thread. +/// - when there are uncaught exceptions objects in the current thread +/// (to avoid std::terminate()) /// /// NOTE: that since C++11 destructor marked with noexcept by default, and /// this means that any throw from destructor (that is not marked with From 22d394c4a367c4bb8774d099727eb2f422b0a144 Mon Sep 17 00:00:00 2001 From: Azat Khuzhin Date: Wed, 14 Apr 2021 23:33:36 +0300 Subject: [PATCH 2/4] Block all memory tracking limits in tryLogCurrentException() --- src/Common/Exception.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Common/Exception.cpp b/src/Common/Exception.cpp index e8a98021588..dca19eea7f2 100644 --- a/src/Common/Exception.cpp +++ b/src/Common/Exception.cpp @@ -150,7 +150,7 @@ void tryLogCurrentException(Poco::Logger * logger, const std::string & start_of_ /// /// And in this case the exception will not be logged, so let's block the /// MemoryTracker until the exception will be logged. - MemoryTracker::LockExceptionInThread lock_memory_tracker; + MemoryTracker::LockExceptionInThread lock_memory_tracker(VariableContext::Global); try { From 8ccc61b3bf321eb93914c201e8609a45ed18aab9 Mon Sep 17 00:00:00 2001 From: Azat Khuzhin Date: Wed, 14 Apr 2021 23:19:53 +0300 Subject: [PATCH 3/4] Block all memory tracking limits in SCOPE_EXIT_*SAFE --- base/ext/scope_guard_safe.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/base/ext/scope_guard_safe.h b/base/ext/scope_guard_safe.h index 7cfb3959a81..55140213572 100644 --- a/base/ext/scope_guard_safe.h +++ b/base/ext/scope_guard_safe.h @@ -12,7 +12,8 @@ /// /// NOTE: it should be used with caution. #define SCOPE_EXIT_MEMORY(...) SCOPE_EXIT( \ - MemoryTracker::LockExceptionInThread lock_memory_tracker; \ + MemoryTracker::LockExceptionInThread \ + lock_memory_tracker(VariableContext::Global); \ __VA_ARGS__; \ ) @@ -56,7 +57,8 @@ #define SCOPE_EXIT_MEMORY_SAFE(...) SCOPE_EXIT( \ try \ { \ - MemoryTracker::LockExceptionInThread lock_memory_tracker; \ + MemoryTracker::LockExceptionInThread \ + lock_memory_tracker(VariableContext::Global); \ __VA_ARGS__; \ } \ catch (...) \ From 9b9e0a9bbcd1ed8ed4ef4a7db368eb4f1a03d9ea Mon Sep 17 00:00:00 2001 From: Azat Khuzhin Date: Wed, 14 Apr 2021 23:37:08 +0300 Subject: [PATCH 4/4] Block all memory tracking limits in destructors --- src/IO/BrotliWriteBuffer.cpp | 2 +- src/IO/LZMADeflatingWriteBuffer.cpp | 2 +- src/IO/WriteBufferFromFile.cpp | 2 +- src/IO/WriteBufferFromFileDescriptor.cpp | 2 +- src/IO/WriteBufferFromOStream.cpp | 2 +- src/IO/WriteBufferFromPocoSocket.cpp | 2 +- src/IO/WriteBufferFromS3.cpp | 2 +- src/IO/WriteBufferFromVector.h | 2 +- src/IO/WriteBufferValidUTF8.cpp | 2 +- src/IO/ZlibDeflatingWriteBuffer.cpp | 2 +- src/IO/ZstdDeflatingWriteBuffer.cpp | 2 +- src/Interpreters/ThreadStatusExt.cpp | 2 +- src/Server/HTTP/WriteBufferFromHTTPServerResponse.cpp | 2 +- 13 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/IO/BrotliWriteBuffer.cpp b/src/IO/BrotliWriteBuffer.cpp index 512ed5fc93f..3d0afa86360 100644 --- a/src/IO/BrotliWriteBuffer.cpp +++ b/src/IO/BrotliWriteBuffer.cpp @@ -50,7 +50,7 @@ BrotliWriteBuffer::BrotliWriteBuffer(std::unique_ptr out_, int comp BrotliWriteBuffer::~BrotliWriteBuffer() { /// FIXME move final flush into the caller - MemoryTracker::LockExceptionInThread lock; + MemoryTracker::LockExceptionInThread lock(VariableContext::Global); finish(); } diff --git a/src/IO/LZMADeflatingWriteBuffer.cpp b/src/IO/LZMADeflatingWriteBuffer.cpp index 7ea4f7945dc..29cde872241 100644 --- a/src/IO/LZMADeflatingWriteBuffer.cpp +++ b/src/IO/LZMADeflatingWriteBuffer.cpp @@ -50,7 +50,7 @@ LZMADeflatingWriteBuffer::LZMADeflatingWriteBuffer( LZMADeflatingWriteBuffer::~LZMADeflatingWriteBuffer() { /// FIXME move final flush into the caller - MemoryTracker::LockExceptionInThread lock; + MemoryTracker::LockExceptionInThread lock(VariableContext::Global); finish(); lzma_end(&lstr); diff --git a/src/IO/WriteBufferFromFile.cpp b/src/IO/WriteBufferFromFile.cpp index b3a63842326..67cd7ba27d6 100644 --- a/src/IO/WriteBufferFromFile.cpp +++ b/src/IO/WriteBufferFromFile.cpp @@ -79,7 +79,7 @@ WriteBufferFromFile::~WriteBufferFromFile() return; /// FIXME move final flush into the caller - MemoryTracker::LockExceptionInThread lock; + MemoryTracker::LockExceptionInThread lock(VariableContext::Global); next(); diff --git a/src/IO/WriteBufferFromFileDescriptor.cpp b/src/IO/WriteBufferFromFileDescriptor.cpp index bfd874ee396..cd265653bb9 100644 --- a/src/IO/WriteBufferFromFileDescriptor.cpp +++ b/src/IO/WriteBufferFromFileDescriptor.cpp @@ -98,7 +98,7 @@ WriteBufferFromFileDescriptor::~WriteBufferFromFileDescriptor() } /// FIXME move final flush into the caller - MemoryTracker::LockExceptionInThread lock; + MemoryTracker::LockExceptionInThread lock(VariableContext::Global); next(); } diff --git a/src/IO/WriteBufferFromOStream.cpp b/src/IO/WriteBufferFromOStream.cpp index cf731934c93..36fbf8301c1 100644 --- a/src/IO/WriteBufferFromOStream.cpp +++ b/src/IO/WriteBufferFromOStream.cpp @@ -43,7 +43,7 @@ WriteBufferFromOStream::WriteBufferFromOStream( WriteBufferFromOStream::~WriteBufferFromOStream() { /// FIXME move final flush into the caller - MemoryTracker::LockExceptionInThread lock; + MemoryTracker::LockExceptionInThread lock(VariableContext::Global); next(); } diff --git a/src/IO/WriteBufferFromPocoSocket.cpp b/src/IO/WriteBufferFromPocoSocket.cpp index 45f6e96218a..7338bcabdb5 100644 --- a/src/IO/WriteBufferFromPocoSocket.cpp +++ b/src/IO/WriteBufferFromPocoSocket.cpp @@ -73,7 +73,7 @@ WriteBufferFromPocoSocket::WriteBufferFromPocoSocket(Poco::Net::Socket & socket_ WriteBufferFromPocoSocket::~WriteBufferFromPocoSocket() { /// FIXME move final flush into the caller - MemoryTracker::LockExceptionInThread lock; + MemoryTracker::LockExceptionInThread lock(VariableContext::Global); next(); } diff --git a/src/IO/WriteBufferFromS3.cpp b/src/IO/WriteBufferFromS3.cpp index 93aaf9456b5..a63e00efce4 100644 --- a/src/IO/WriteBufferFromS3.cpp +++ b/src/IO/WriteBufferFromS3.cpp @@ -87,7 +87,7 @@ void WriteBufferFromS3::allocateBuffer() void WriteBufferFromS3::finalize() { /// FIXME move final flush into the caller - MemoryTracker::LockExceptionInThread lock; + MemoryTracker::LockExceptionInThread lock(VariableContext::Global); finalizeImpl(); } diff --git a/src/IO/WriteBufferFromVector.h b/src/IO/WriteBufferFromVector.h index 6341a9b698b..0e0cd3c522b 100644 --- a/src/IO/WriteBufferFromVector.h +++ b/src/IO/WriteBufferFromVector.h @@ -95,7 +95,7 @@ public: ~WriteBufferFromVector() override { /// FIXME move final flush into the caller - MemoryTracker::LockExceptionInThread lock; + MemoryTracker::LockExceptionInThread lock(VariableContext::Global); finalize(); } }; diff --git a/src/IO/WriteBufferValidUTF8.cpp b/src/IO/WriteBufferValidUTF8.cpp index 1071ac1078d..ecdf38eae34 100644 --- a/src/IO/WriteBufferValidUTF8.cpp +++ b/src/IO/WriteBufferValidUTF8.cpp @@ -138,7 +138,7 @@ void WriteBufferValidUTF8::finish() WriteBufferValidUTF8::~WriteBufferValidUTF8() { /// FIXME move final flush into the caller - MemoryTracker::LockExceptionInThread lock; + MemoryTracker::LockExceptionInThread lock(VariableContext::Global); finish(); } diff --git a/src/IO/ZlibDeflatingWriteBuffer.cpp b/src/IO/ZlibDeflatingWriteBuffer.cpp index 7e91820f298..3cf00f627b3 100644 --- a/src/IO/ZlibDeflatingWriteBuffer.cpp +++ b/src/IO/ZlibDeflatingWriteBuffer.cpp @@ -49,7 +49,7 @@ ZlibDeflatingWriteBuffer::ZlibDeflatingWriteBuffer( ZlibDeflatingWriteBuffer::~ZlibDeflatingWriteBuffer() { /// FIXME move final flush into the caller - MemoryTracker::LockExceptionInThread lock; + MemoryTracker::LockExceptionInThread lock(VariableContext::Global); finish(); diff --git a/src/IO/ZstdDeflatingWriteBuffer.cpp b/src/IO/ZstdDeflatingWriteBuffer.cpp index 5b97588b33e..08b289a2e04 100644 --- a/src/IO/ZstdDeflatingWriteBuffer.cpp +++ b/src/IO/ZstdDeflatingWriteBuffer.cpp @@ -31,7 +31,7 @@ ZstdDeflatingWriteBuffer::ZstdDeflatingWriteBuffer( ZstdDeflatingWriteBuffer::~ZstdDeflatingWriteBuffer() { /// FIXME move final flush into the caller - MemoryTracker::LockExceptionInThread lock; + MemoryTracker::LockExceptionInThread lock(VariableContext::Global); finish(); diff --git a/src/Interpreters/ThreadStatusExt.cpp b/src/Interpreters/ThreadStatusExt.cpp index ec6bdb8d526..c04534e11a1 100644 --- a/src/Interpreters/ThreadStatusExt.cpp +++ b/src/Interpreters/ThreadStatusExt.cpp @@ -323,7 +323,7 @@ void ThreadStatus::finalizeQueryProfiler() void ThreadStatus::detachQuery(bool exit_if_already_detached, bool thread_exits) { - MemoryTracker::LockExceptionInThread lock; + MemoryTracker::LockExceptionInThread lock(VariableContext::Global); if (exit_if_already_detached && thread_state == ThreadState::DetachedFromQuery) { diff --git a/src/Server/HTTP/WriteBufferFromHTTPServerResponse.cpp b/src/Server/HTTP/WriteBufferFromHTTPServerResponse.cpp index 355af038da9..a4fe3649e6f 100644 --- a/src/Server/HTTP/WriteBufferFromHTTPServerResponse.cpp +++ b/src/Server/HTTP/WriteBufferFromHTTPServerResponse.cpp @@ -196,7 +196,7 @@ void WriteBufferFromHTTPServerResponse::finalize() WriteBufferFromHTTPServerResponse::~WriteBufferFromHTTPServerResponse() { /// FIXME move final flush into the caller - MemoryTracker::LockExceptionInThread lock; + MemoryTracker::LockExceptionInThread lock(VariableContext::Global); finalize(); }