diff --git a/src/Common/MemoryTracker.cpp b/src/Common/MemoryTracker.cpp index d037142fbfb..a584885cf0f 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 current in progress in -/// this thread. +/// - to avoid std::terminate(), when stack unwinding is currently in progress +/// in this thread. /// /// NOTE: that since C++11 destructor marked with noexcept by default, and /// this means that any throw from destructor (that is not marked with diff --git a/src/Common/ZooKeeper/IKeeper.h b/src/Common/ZooKeeper/IKeeper.h index 9d4a2ebb16a..c53ea60ec7c 100644 --- a/src/Common/ZooKeeper/IKeeper.h +++ b/src/Common/ZooKeeper/IKeeper.h @@ -331,7 +331,7 @@ public: class IKeeper { public: - virtual ~IKeeper() {} + virtual ~IKeeper() = default; /// If expired, you can only destroy the object. All other methods will throw exception. virtual bool isExpired() const = 0; diff --git a/src/Compression/CompressedWriteBuffer.cpp b/src/Compression/CompressedWriteBuffer.cpp index 02f418dcdf7..8d146e8de23 100644 --- a/src/Compression/CompressedWriteBuffer.cpp +++ b/src/Compression/CompressedWriteBuffer.cpp @@ -8,6 +8,7 @@ #include #include +#include namespace DB @@ -49,14 +50,9 @@ CompressedWriteBuffer::CompressedWriteBuffer( CompressedWriteBuffer::~CompressedWriteBuffer() { - try - { - next(); - } - catch (...) - { - tryLogCurrentException(__PRETTY_FUNCTION__); - } + /// FIXME move final flush into the caller + MemoryTracker::LockExceptionInThread lock; + next(); } } diff --git a/src/DataStreams/IBlockOutputStream.h b/src/DataStreams/IBlockOutputStream.h index 4cc1257e955..79c13b6fa47 100644 --- a/src/DataStreams/IBlockOutputStream.h +++ b/src/DataStreams/IBlockOutputStream.h @@ -57,7 +57,7 @@ public: */ virtual std::string getContentType() const { return "text/plain; charset=UTF-8"; } - virtual ~IBlockOutputStream() {} + virtual ~IBlockOutputStream() = default; /** Don't let to alter table while instance of stream is alive. */ diff --git a/src/IO/AsynchronousWriteBuffer.h b/src/IO/AsynchronousWriteBuffer.h index 74b5804691b..8c44f8c7d4a 100644 --- a/src/IO/AsynchronousWriteBuffer.h +++ b/src/IO/AsynchronousWriteBuffer.h @@ -1,10 +1,8 @@ #pragma once -#include - #include - #include +#include #include @@ -53,18 +51,14 @@ public: ~AsynchronousWriteBuffer() override { - try - { - if (started) - pool.wait(); + /// FIXME move final flush into the caller + MemoryTracker::LockExceptionInThread lock; - swapBuffers(); - out.next(); - } - catch (...) - { - tryLogCurrentException(__PRETTY_FUNCTION__); - } + if (started) + pool.wait(); + + swapBuffers(); + out.next(); } /// That is executed in a separate thread diff --git a/src/IO/BrotliWriteBuffer.cpp b/src/IO/BrotliWriteBuffer.cpp index e4e3713d379..d14c94ca43d 100644 --- a/src/IO/BrotliWriteBuffer.cpp +++ b/src/IO/BrotliWriteBuffer.cpp @@ -6,6 +6,8 @@ # include # include +#include + namespace DB { @@ -47,14 +49,9 @@ BrotliWriteBuffer::BrotliWriteBuffer(std::unique_ptr out_, int comp BrotliWriteBuffer::~BrotliWriteBuffer() { - try - { - finish(); - } - catch (...) - { - tryLogCurrentException(__PRETTY_FUNCTION__); - } + /// FIXME move final flush into the caller + MemoryTracker::LockExceptionInThread lock; + finish(); } void BrotliWriteBuffer::nextImpl() diff --git a/src/IO/HexWriteBuffer.cpp b/src/IO/HexWriteBuffer.cpp index d7b8a993ce5..4e3403ba74b 100644 --- a/src/IO/HexWriteBuffer.cpp +++ b/src/IO/HexWriteBuffer.cpp @@ -1,6 +1,6 @@ #include #include -#include +#include #include @@ -22,14 +22,9 @@ void HexWriteBuffer::nextImpl() HexWriteBuffer::~HexWriteBuffer() { - try - { - nextImpl(); - } - catch (...) - { - tryLogCurrentException(__PRETTY_FUNCTION__); - } + /// FIXME move final flush into the caller + MemoryTracker::LockExceptionInThread lock; + nextImpl(); } } diff --git a/src/IO/IReadableWriteBuffer.h b/src/IO/IReadableWriteBuffer.h index a02dd4e23cb..539825e3a85 100644 --- a/src/IO/IReadableWriteBuffer.h +++ b/src/IO/IReadableWriteBuffer.h @@ -17,7 +17,7 @@ struct IReadableWriteBuffer return getReadBufferImpl(); } - virtual ~IReadableWriteBuffer() {} + virtual ~IReadableWriteBuffer() = default; protected: diff --git a/src/IO/LZMADeflatingWriteBuffer.cpp b/src/IO/LZMADeflatingWriteBuffer.cpp index e3051f1de65..5803bc1e9f1 100644 --- a/src/IO/LZMADeflatingWriteBuffer.cpp +++ b/src/IO/LZMADeflatingWriteBuffer.cpp @@ -1,4 +1,5 @@ #include +#include #if !defined(ARCADIA_BUILD) @@ -48,16 +49,11 @@ LZMADeflatingWriteBuffer::LZMADeflatingWriteBuffer( LZMADeflatingWriteBuffer::~LZMADeflatingWriteBuffer() { - try - { - finish(); + /// FIXME move final flush into the caller + MemoryTracker::LockExceptionInThread lock; - lzma_end(&lstr); - } - catch (...) - { - tryLogCurrentException(__PRETTY_FUNCTION__); - } + finish(); + lzma_end(&lstr); } void LZMADeflatingWriteBuffer::nextImpl() diff --git a/src/IO/WriteBufferFromFile.cpp b/src/IO/WriteBufferFromFile.cpp index aeed4862fba..b3a63842326 100644 --- a/src/IO/WriteBufferFromFile.cpp +++ b/src/IO/WriteBufferFromFile.cpp @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -77,14 +78,10 @@ WriteBufferFromFile::~WriteBufferFromFile() if (fd < 0) return; - try - { - next(); - } - catch (...) - { - tryLogCurrentException(__PRETTY_FUNCTION__); - } + /// FIXME move final flush into the caller + MemoryTracker::LockExceptionInThread lock; + + next(); ::close(fd); } diff --git a/src/IO/WriteBufferFromFileDescriptor.cpp b/src/IO/WriteBufferFromFileDescriptor.cpp index a59ae20c588..bfd874ee396 100644 --- a/src/IO/WriteBufferFromFileDescriptor.cpp +++ b/src/IO/WriteBufferFromFileDescriptor.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include @@ -90,17 +91,15 @@ WriteBufferFromFileDescriptor::WriteBufferFromFileDescriptor( WriteBufferFromFileDescriptor::~WriteBufferFromFileDescriptor() { - try + if (fd < 0) { - if (fd >= 0) - next(); - else - assert(!offset() && "attempt to write after close"); - } - catch (...) - { - tryLogCurrentException(__PRETTY_FUNCTION__); + assert(!offset() && "attempt to write after close"); + return; } + + /// FIXME move final flush into the caller + MemoryTracker::LockExceptionInThread lock; + next(); } diff --git a/src/IO/WriteBufferFromHTTPServerResponse.cpp b/src/IO/WriteBufferFromHTTPServerResponse.cpp index 0f30f1352e3..fb9a6a99d2b 100644 --- a/src/IO/WriteBufferFromHTTPServerResponse.cpp +++ b/src/IO/WriteBufferFromHTTPServerResponse.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #if !defined(ARCADIA_BUILD) # include @@ -206,14 +207,9 @@ void WriteBufferFromHTTPServerResponse::finalize() WriteBufferFromHTTPServerResponse::~WriteBufferFromHTTPServerResponse() { - try - { - finalize(); - } - catch (...) - { - tryLogCurrentException(__PRETTY_FUNCTION__); - } + /// FIXME move final flush into the caller + MemoryTracker::LockExceptionInThread lock; + finalize(); } } diff --git a/src/IO/WriteBufferFromOStream.cpp b/src/IO/WriteBufferFromOStream.cpp index 2c45a21a0a3..cf731934c93 100644 --- a/src/IO/WriteBufferFromOStream.cpp +++ b/src/IO/WriteBufferFromOStream.cpp @@ -1,5 +1,5 @@ #include -#include +#include namespace DB @@ -42,14 +42,9 @@ WriteBufferFromOStream::WriteBufferFromOStream( WriteBufferFromOStream::~WriteBufferFromOStream() { - try - { - next(); - } - catch (...) - { - tryLogCurrentException(__PRETTY_FUNCTION__); - } + /// FIXME move final flush into the caller + MemoryTracker::LockExceptionInThread lock; + next(); } } diff --git a/src/IO/WriteBufferFromPocoSocket.cpp b/src/IO/WriteBufferFromPocoSocket.cpp index c05dc11e330..284fa5dbd97 100644 --- a/src/IO/WriteBufferFromPocoSocket.cpp +++ b/src/IO/WriteBufferFromPocoSocket.cpp @@ -5,6 +5,7 @@ #include #include #include +#include namespace ProfileEvents @@ -70,14 +71,9 @@ WriteBufferFromPocoSocket::WriteBufferFromPocoSocket(Poco::Net::Socket & socket_ WriteBufferFromPocoSocket::~WriteBufferFromPocoSocket() { - try - { - next(); - } - catch (...) - { - tryLogCurrentException(__PRETTY_FUNCTION__); - } + /// FIXME move final flush into the caller + MemoryTracker::LockExceptionInThread lock; + next(); } } diff --git a/src/IO/WriteBufferFromS3.cpp b/src/IO/WriteBufferFromS3.cpp index 09aabb1b21d..a6ec60b295f 100644 --- a/src/IO/WriteBufferFromS3.cpp +++ b/src/IO/WriteBufferFromS3.cpp @@ -4,6 +4,7 @@ # include # include +# include # include # include @@ -78,6 +79,8 @@ void WriteBufferFromS3::nextImpl() void WriteBufferFromS3::finalize() { + /// FIXME move final flush into the caller + MemoryTracker::LockExceptionInThread lock; finalizeImpl(); } @@ -104,14 +107,7 @@ void WriteBufferFromS3::finalizeImpl() WriteBufferFromS3::~WriteBufferFromS3() { - try - { - finalizeImpl(); - } - catch (...) - { - tryLogCurrentException(__PRETTY_FUNCTION__); - } + finalizeImpl(); } void WriteBufferFromS3::createMultipartUpload() diff --git a/src/IO/WriteBufferFromVector.h b/src/IO/WriteBufferFromVector.h index 2a9810f3461..1dcf2c3f327 100644 --- a/src/IO/WriteBufferFromVector.h +++ b/src/IO/WriteBufferFromVector.h @@ -3,6 +3,7 @@ #include #include +#include namespace DB @@ -93,14 +94,9 @@ public: ~WriteBufferFromVector() override { - try - { - finalize(); - } - catch (...) - { - tryLogCurrentException(__PRETTY_FUNCTION__); - } + /// FIXME move final flush into the caller + MemoryTracker::LockExceptionInThread lock; + finalize(); } }; diff --git a/src/IO/WriteBufferValidUTF8.cpp b/src/IO/WriteBufferValidUTF8.cpp index f1f04e9805b..1071ac1078d 100644 --- a/src/IO/WriteBufferValidUTF8.cpp +++ b/src/IO/WriteBufferValidUTF8.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #ifdef __SSE2__ @@ -136,14 +137,9 @@ void WriteBufferValidUTF8::finish() WriteBufferValidUTF8::~WriteBufferValidUTF8() { - try - { - finish(); - } - catch (...) - { - tryLogCurrentException(__PRETTY_FUNCTION__); - } + /// FIXME move final flush into the caller + MemoryTracker::LockExceptionInThread lock; + finish(); } } diff --git a/src/IO/ZlibDeflatingWriteBuffer.cpp b/src/IO/ZlibDeflatingWriteBuffer.cpp index 8efe96877e4..4b838ac6d0a 100644 --- a/src/IO/ZlibDeflatingWriteBuffer.cpp +++ b/src/IO/ZlibDeflatingWriteBuffer.cpp @@ -1,5 +1,7 @@ #include #include +#include +#include namespace DB @@ -46,16 +48,21 @@ ZlibDeflatingWriteBuffer::ZlibDeflatingWriteBuffer( ZlibDeflatingWriteBuffer::~ZlibDeflatingWriteBuffer() { + /// FIXME move final flush into the caller + MemoryTracker::LockExceptionInThread lock; + + finish(); + try { - finish(); - int rc = deflateEnd(&zstr); if (rc != Z_OK) throw Exception(std::string("deflateEnd failed: ") + zError(rc), ErrorCodes::ZLIB_DEFLATE_FAILED); } catch (...) { + /// It is OK not to terminate under an error from deflateEnd() + /// since all data already written to the stream. tryLogCurrentException(__PRETTY_FUNCTION__); } } diff --git a/src/IO/ZstdDeflatingWriteBuffer.cpp b/src/IO/ZstdDeflatingWriteBuffer.cpp index df28820e382..9b79d5ae513 100644 --- a/src/IO/ZstdDeflatingWriteBuffer.cpp +++ b/src/IO/ZstdDeflatingWriteBuffer.cpp @@ -1,4 +1,6 @@ #include +#include +#include namespace DB { @@ -28,14 +30,22 @@ ZstdDeflatingWriteBuffer::ZstdDeflatingWriteBuffer( ZstdDeflatingWriteBuffer::~ZstdDeflatingWriteBuffer() { + /// FIXME move final flush into the caller + MemoryTracker::LockExceptionInThread lock; + + finish(); + try { - finish(); - - ZSTD_freeCCtx(cctx); + int err = ZSTD_freeCCtx(cctx); + /// This is just in case, since it is impossible to get an error by using this wrapper. + if (unlikely(err)) + throw Exception(ErrorCodes::ZSTD_ENCODER_FAILED, "ZSTD_freeCCtx failed: error code: {}; zstd version: {}", err, ZSTD_VERSION_STRING); } catch (...) { + /// It is OK not to terminate under an error from ZSTD_freeCCtx() + /// since all data already written to the stream. tryLogCurrentException(__PRETTY_FUNCTION__); } } diff --git a/src/Server/TCPHandler.cpp b/src/Server/TCPHandler.cpp index d66639ef111..c207d188a85 100644 --- a/src/Server/TCPHandler.cpp +++ b/src/Server/TCPHandler.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -56,6 +57,28 @@ namespace ErrorCodes extern const int SUPPORT_IS_DISABLED; } +TCPHandler::TCPHandler(IServer & server_, const Poco::Net::StreamSocket & socket_, bool parse_proxy_protocol_, std::string server_display_name_) + : Poco::Net::TCPServerConnection(socket_) + , server(server_) + , parse_proxy_protocol(parse_proxy_protocol_) + , log(&Poco::Logger::get("TCPHandler")) + , connection_context(server.context()) + , query_context(server.context()) + , server_display_name(std::move(server_display_name_)) +{ +} +TCPHandler::~TCPHandler() +{ + try + { + state.reset(); + out->next(); + } + catch (...) + { + tryLogCurrentException(__PRETTY_FUNCTION__); + } +} void TCPHandler::runImpl() { diff --git a/src/Server/TCPHandler.h b/src/Server/TCPHandler.h index 41539bef1e1..ee2f7c96b5a 100644 --- a/src/Server/TCPHandler.h +++ b/src/Server/TCPHandler.h @@ -10,6 +10,7 @@ #include #include #include +#include #include #include "IServer.h" @@ -113,16 +114,8 @@ public: * Proxy-forwarded (original client) IP address is used for quota accounting if quota is keyed by forwarded IP. */ TCPHandler(IServer & server_, const Poco::Net::StreamSocket & socket_, bool parse_proxy_protocol_, - std::string server_display_name_) - : Poco::Net::TCPServerConnection(socket_) - , server(server_) - , parse_proxy_protocol(parse_proxy_protocol_) - , log(&Poco::Logger::get("TCPHandler")) - , connection_context(server.context()) - , query_context(server.context()) - , server_display_name(std::move(server_display_name_)) - { - } + std::string server_display_name_); + ~TCPHandler() override; void run() override;