Skip to content

Commit fd842b2

Browse files
authored
Merge pull request ClickHouse#88732 from ClickHouse/backport/25.8/88668
Backport ClickHouse#88668 to 25.8: wrap `~PooledConnection` in try-catch
2 parents 52f63b8 + bfa674c commit fd842b2

File tree

6 files changed

+52
-29
lines changed

6 files changed

+52
-29
lines changed

base/poco/Net/include/Poco/Net/HTTPChunkedStream.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ namespace Net
4545
~HTTPChunkedStreamBuf();
4646
void close();
4747

48-
bool isComplete(bool read_from_device_to_check_eof = false);
48+
bool isComplete(bool read_from_device_to_check_eof = false) noexcept;
4949

5050
protected:
5151
int readFromDevice(char * buffer, std::streamsize length);

base/poco/Net/src/HTTPChunkedStream.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ int HTTPChunkedStreamBuf::readFromDevice(char* buffer, std::streamsize length)
140140
}
141141

142142

143-
bool HTTPChunkedStreamBuf::isComplete(bool read_from_device_to_check_eof)
143+
bool HTTPChunkedStreamBuf::isComplete(bool read_from_device_to_check_eof) noexcept
144144
{
145145
if (read_from_device_to_check_eof)
146146
{
@@ -150,7 +150,7 @@ bool HTTPChunkedStreamBuf::isComplete(bool read_from_device_to_check_eof)
150150
/// "Unexpected EOF" exception would be thrown
151151
readFromDevice(nullptr, 0);
152152
}
153-
catch (Poco::Net::MessageException &)
153+
catch (...)
154154
{
155155
return false;
156156
}

src/Common/HTTPConnectionPool.cpp

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -438,38 +438,45 @@ class EndpointConnectionPool : public std::enable_shared_from_this<EndpointConne
438438

439439
~PooledConnection() override
440440
{
441-
if (bool(response_stream))
441+
try
442442
{
443-
if (auto * fixed_steam = dynamic_cast<Poco::Net::HTTPFixedLengthInputStream *>(response_stream))
443+
if (bool(response_stream))
444444
{
445-
response_stream_completed = fixed_steam->isComplete();
445+
if (auto * fixed_steam = dynamic_cast<Poco::Net::HTTPFixedLengthInputStream *>(response_stream))
446+
{
447+
response_stream_completed = fixed_steam->isComplete();
448+
}
449+
else if (auto * chunked_steam = dynamic_cast<Poco::Net::HTTPChunkedInputStream *>(response_stream))
450+
{
451+
response_stream_completed = chunked_steam->isComplete();
452+
}
453+
else if (auto * http_stream = dynamic_cast<Poco::Net::HTTPInputStream *>(response_stream))
454+
{
455+
response_stream_completed = http_stream->isComplete();
456+
}
457+
else
458+
{
459+
response_stream_completed = false;
460+
}
446461
}
447-
else if (auto * chunked_steam = dynamic_cast<Poco::Net::HTTPChunkedInputStream *>(response_stream))
448-
{
449-
response_stream_completed = chunked_steam->isComplete();
450-
}
451-
else if (auto * http_stream = dynamic_cast<Poco::Net::HTTPInputStream *>(response_stream))
452-
{
453-
response_stream_completed = http_stream->isComplete();
454-
}
455-
else
456-
{
457-
response_stream_completed = false;
458-
}
459-
}
460-
response_stream = nullptr;
461-
Session::setSendDataHooks();
462-
Session::setReceiveDataHooks();
463-
Session::setSendThrottler();
464-
Session::setReceiveThrottler();
462+
response_stream = nullptr;
463+
Session::setSendDataHooks();
464+
Session::setReceiveDataHooks();
465+
Session::setSendThrottler();
466+
Session::setReceiveThrottler();
465467

466-
group->atConnectionDestroy();
468+
group->atConnectionDestroy();
467469

468-
if (!isExpired)
469-
if (auto lock = pool.lock())
470-
lock->atConnectionDestroy(*this);
470+
if (!isExpired)
471+
if (auto lock = pool.lock())
472+
lock->atConnectionDestroy(*this);
471473

472-
CurrentMetrics::sub(metrics.active_count);
474+
CurrentMetrics::sub(metrics.active_count);
475+
}
476+
catch (...)
477+
{
478+
tryLogCurrentException(__PRETTY_FUNCTION__);
479+
}
473480
}
474481

475482
private:
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <gtest/gtest.h>
2+
3+
#include <Poco/Net/HTTPChunkedStream.h>
4+
#include <Poco/Net/HTTPClientSession.h>
5+
6+
7+
TEST(HTTPChunkedStreamBuf, IsCompleteHandlesInvalidSocketException)
8+
{
9+
Poco::Net::HTTPClientSession session;
10+
Poco::Net::HTTPChunkedStreamBuf buf(session, std::ios::in);
11+
12+
/// Default-initialized socket throws InvalidSocketException in SocketImpl::receiveBytes,
13+
/// which HTTPChunkedStreamBuf::isComplete should swallow and return false.
14+
bool complete = buf.isComplete(true);
15+
ASSERT_FALSE(complete);
16+
}

tests/queries/0_stateless/02537_distributed_loosing_files_after_exception.reference renamed to tests/queries/0_stateless/02537_distributed_losing_files_after_exception.reference

File renamed without changes.

tests/queries/0_stateless/02537_distributed_loosing_files_after_exception.sql.j2 renamed to tests/queries/0_stateless/02537_distributed_losing_files_after_exception.sql.j2

File renamed without changes.

0 commit comments

Comments
 (0)