Skip to content
This repository has been archived by the owner on Feb 10, 2023. It is now read-only.

Commit

Permalink
Merge pull request #28 from amosbird/clickhouse
Browse files Browse the repository at this point in the history
Fix thread counter leak

(cherry picked from commit f3d791f)
  • Loading branch information
alexey-milovidov authored and alesapin committed Dec 7, 2020
1 parent bc8ad1b commit 2a3406f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 41 deletions.
24 changes: 0 additions & 24 deletions Net/include/Poco/Net/TCPServerDispatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,30 +100,6 @@ class Net_API TCPServerDispatcher: public Poco::Runnable
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;
Expand Down
36 changes: 19 additions & 17 deletions Net/src/TCPServerDispatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,29 +98,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;
}
}
}

Expand Down

0 comments on commit 2a3406f

Please sign in to comment.