Merge pull request #49112 from ClickHouse/Avogar-patch-3

Fix possible terminate called for uncaught exception in some places
This commit is contained in:
Kruglov Pavel 2023-05-31 16:55:43 +02:00 committed by GitHub
commit 0beca0336d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 38 additions and 5 deletions

View File

@ -232,12 +232,28 @@ void Connection::disconnect()
maybe_compressed_out = nullptr; maybe_compressed_out = nullptr;
in = nullptr; in = nullptr;
last_input_packet_type.reset(); last_input_packet_type.reset();
out = nullptr; // can write to socket std::exception_ptr finalize_exception;
try
{
// finalize() can write to socket and throw an exception.
if (out)
out->finalize();
}
catch (...)
{
/// Don't throw an exception here, it will leave Connection in invalid state.
finalize_exception = std::current_exception();
}
out = nullptr;
if (socket) if (socket)
socket->close(); socket->close();
socket = nullptr; socket = nullptr;
connected = false; connected = false;
nonce.reset(); nonce.reset();
if (finalize_exception)
std::rethrow_exception(finalize_exception);
} }

View File

@ -106,7 +106,14 @@ WriteBufferFromPocoSocket::WriteBufferFromPocoSocket(Poco::Net::Socket & socket_
WriteBufferFromPocoSocket::~WriteBufferFromPocoSocket() WriteBufferFromPocoSocket::~WriteBufferFromPocoSocket()
{ {
finalize(); try
{
finalize();
}
catch (...)
{
tryLogCurrentException(__PRETTY_FUNCTION__);
}
} }
} }

View File

@ -93,10 +93,13 @@ void InterserverIOHTTPHandler::handleRequest(HTTPServerRequest & request, HTTPSe
auto write_response = [&](const std::string & message) auto write_response = [&](const std::string & message)
{ {
if (response.sent())
return;
auto & out = *used_output.out; auto & out = *used_output.out;
if (response.sent())
{
out.finalize();
return;
}
try try
{ {
writeString(message, out); writeString(message, out);
@ -127,7 +130,10 @@ void InterserverIOHTTPHandler::handleRequest(HTTPServerRequest & request, HTTPSe
catch (Exception & e) catch (Exception & e)
{ {
if (e.code() == ErrorCodes::TOO_MANY_SIMULTANEOUS_QUERIES) if (e.code() == ErrorCodes::TOO_MANY_SIMULTANEOUS_QUERIES)
{
used_output.out->finalize();
return; return;
}
response.setStatusAndReason(Poco::Net::HTTPResponse::HTTP_INTERNAL_SERVER_ERROR); response.setStatusAndReason(Poco::Net::HTTPResponse::HTTP_INTERNAL_SERVER_ERROR);

View File

@ -490,6 +490,7 @@ private:
{ {
/// Stop ParallelFormattingOutputFormat correctly. /// Stop ParallelFormattingOutputFormat correctly.
writer.reset(); writer.reset();
write_buf->finalize();
throw; throw;
} }
} }

View File

@ -955,6 +955,7 @@ private:
{ {
/// Stop ParallelFormattingOutputFormat correctly. /// Stop ParallelFormattingOutputFormat correctly.
writer.reset(); writer.reset();
write_buf->finalize();
throw; throw;
} }
} }

View File

@ -831,6 +831,7 @@ private:
{ {
/// Stop ParallelFormattingOutputFormat correctly. /// Stop ParallelFormattingOutputFormat correctly.
writer.reset(); writer.reset();
write_buf->finalize();
throw; throw;
} }
} }

View File

@ -480,6 +480,7 @@ void StorageURLSink::finalize()
{ {
/// Stop ParallelFormattingOutputFormat correctly. /// Stop ParallelFormattingOutputFormat correctly.
writer.reset(); writer.reset();
write_buf->finalize();
throw; throw;
} }
} }