Merge pull request #61309 from ClickHouse/chesema-fix-poco-tcp-server

fix data race in poco tcp server
This commit is contained in:
Sema Checherinda 2024-03-14 12:11:52 +01:00 committed by GitHub
commit 689c715408
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 6 additions and 9 deletions

View File

@ -93,7 +93,7 @@ void TCPServerDispatcher::release()
void TCPServerDispatcher::run() void TCPServerDispatcher::run()
{ {
AutoPtr<TCPServerDispatcher> guard(this, true); // ensure object stays alive AutoPtr<TCPServerDispatcher> guard(this); // ensure object stays alive
int idleTime = (int) _pParams->getThreadIdleTime().totalMilliseconds(); int idleTime = (int) _pParams->getThreadIdleTime().totalMilliseconds();
@ -149,11 +149,13 @@ void TCPServerDispatcher::enqueue(const StreamSocket& socket)
{ {
try try
{ {
this->duplicate();
_threadPool.startWithPriority(_pParams->getThreadPriority(), *this, threadName); _threadPool.startWithPriority(_pParams->getThreadPriority(), *this, threadName);
++_currentThreads; ++_currentThreads;
} }
catch (Poco::Exception& exc) catch (Poco::Exception& exc)
{ {
this->release();
++_refusedConnections; ++_refusedConnections;
std::cerr << "Got exception while starting thread for connection. Error code: " std::cerr << "Got exception while starting thread for connection. Error code: "
<< exc.code() << ", message: '" << exc.displayText() << "'" << std::endl; << exc.code() << ", message: '" << exc.displayText() << "'" << std::endl;

View File

@ -123,17 +123,15 @@ protected:
std::string getServerUrl() const std::string getServerUrl() const
{ {
return "http://" + server_data.socket->address().toString(); return "http://" + server_data.server->socket().address().toString();
} }
void startServer() void startServer()
{ {
server_data.reset(); server_data.reset();
server_data.params = new Poco::Net::HTTPServerParams();
server_data.socket = std::make_unique<Poco::Net::ServerSocket>(server_data.port);
server_data.handler_factory = new HTTPRequestHandlerFactory(slowdown_receive); server_data.handler_factory = new HTTPRequestHandlerFactory(slowdown_receive);
server_data.server = std::make_unique<Poco::Net::HTTPServer>( server_data.server = std::make_unique<Poco::Net::HTTPServer>(
server_data.handler_factory, *server_data.socket, server_data.params); server_data.handler_factory, server_data.port);
server_data.server->start(); server_data.server->start();
} }
@ -155,8 +153,7 @@ protected:
{ {
// just some port to avoid collisions with others tests // just some port to avoid collisions with others tests
UInt16 port = 9871; UInt16 port = 9871;
Poco::Net::HTTPServerParams::Ptr params;
std::unique_ptr<Poco::Net::ServerSocket> socket;
HTTPRequestHandlerFactory::Ptr handler_factory; HTTPRequestHandlerFactory::Ptr handler_factory;
std::unique_ptr<Poco::Net::HTTPServer> server; std::unique_ptr<Poco::Net::HTTPServer> server;
@ -171,8 +168,6 @@ protected:
server = nullptr; server = nullptr;
handler_factory = nullptr; handler_factory = nullptr;
socket = nullptr;
params = nullptr;
} }
~ServerData() { ~ServerData() {