mirror of
https://github.com/pocoproject/poco.git
synced 2025-03-29 11:09:42 +01:00
Fix thread counter leak (#3992)
Cherry-picked from https://github.com/ClickHouse/poco/pull/28, see the mentioned PR for more details. Co-authored-by: alexey-milovidov <milovidov@yandex-team.ru>
This commit is contained in:
parent
2a6434a86d
commit
c6fd0db4b6
@ -101,30 +101,6 @@ private:
|
||||
TCPServerDispatcher(const TCPServerDispatcher&);
|
||||
TCPServerDispatcher& operator = (const TCPServerDispatcher&);
|
||||
|
||||
class ThreadCountWatcher
|
||||
{
|
||||
public:
|
||||
ThreadCountWatcher(TCPServerDispatcher* pDisp) : _pDisp(pDisp)
|
||||
{
|
||||
}
|
||||
|
||||
~ThreadCountWatcher()
|
||||
{
|
||||
FastMutex::ScopedLock lock(_pDisp->_mutex);
|
||||
if (_pDisp->_currentThreads > 1 && _pDisp->_queue.empty())
|
||||
{
|
||||
--_pDisp->_currentThreads;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
ThreadCountWatcher();
|
||||
ThreadCountWatcher(const ThreadCountWatcher&);
|
||||
ThreadCountWatcher& operator=(const ThreadCountWatcher&);
|
||||
|
||||
TCPServerDispatcher* _pDisp;
|
||||
};
|
||||
|
||||
std::atomic<int> _rc;
|
||||
TCPServerParams::Ptr _pParams;
|
||||
std::atomic<int> _currentThreads;
|
||||
|
@ -103,29 +103,31 @@ void TCPServerDispatcher::run()
|
||||
|
||||
for (;;)
|
||||
{
|
||||
try
|
||||
{
|
||||
ThreadCountWatcher tcw(this);
|
||||
try
|
||||
AutoPtr<Notification> pNf = _queue.waitDequeueNotification(idleTime);
|
||||
if (pNf)
|
||||
{
|
||||
AutoPtr<Notification> pNf = _queue.waitDequeueNotification(idleTime);
|
||||
if (pNf)
|
||||
TCPConnectionNotification* pCNf = dynamic_cast<TCPConnectionNotification*>(pNf.get());
|
||||
if (pCNf)
|
||||
{
|
||||
TCPConnectionNotification* pCNf = dynamic_cast<TCPConnectionNotification*>(pNf.get());
|
||||
if (pCNf)
|
||||
{
|
||||
std::unique_ptr<TCPServerConnection> pConnection(_pConnectionFactory->createConnection(pCNf->socket()));
|
||||
poco_check_ptr(pConnection.get());
|
||||
beginConnection();
|
||||
pConnection->start();
|
||||
endConnection();
|
||||
}
|
||||
std::unique_ptr<TCPServerConnection> pConnection(_pConnectionFactory->createConnection(pCNf->socket()));
|
||||
poco_check_ptr(pConnection.get());
|
||||
beginConnection();
|
||||
pConnection->start();
|
||||
endConnection();
|
||||
}
|
||||
}
|
||||
catch (Poco::Exception &exc) { ErrorHandler::handle(exc); }
|
||||
catch (std::exception &exc) { ErrorHandler::handle(exc); }
|
||||
catch (...) { ErrorHandler::handle(); }
|
||||
}
|
||||
if (_stopped || (_currentThreads > 1 && _queue.empty())) break;
|
||||
catch (Poco::Exception &exc) { ErrorHandler::handle(exc); }
|
||||
catch (std::exception &exc) { ErrorHandler::handle(exc); }
|
||||
catch (...) { ErrorHandler::handle(); }
|
||||
FastMutex::ScopedLock lock(_mutex);
|
||||
if (_stopped || (_currentThreads > 1 && _queue.empty()))
|
||||
{
|
||||
--_currentThreads;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user