From ef93881d711839a0cc118055363250a5e4b7f46b Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Thu, 13 Dec 2012 12:09:48 -0800 Subject: [PATCH] Fix the deadlock. We were destroying the WSConnection while holding the MapLock causing a deadlock between the MapLock and MonitorLock. --- src/cpp/ripple/WSHandler.h | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/cpp/ripple/WSHandler.h b/src/cpp/ripple/WSHandler.h index 824c690611..4d24f6f898 100644 --- a/src/cpp/ripple/WSHandler.h +++ b/src/cpp/ripple/WSHandler.h @@ -91,10 +91,17 @@ public: } void on_close(connection_ptr cpClient) - { - boost::mutex::scoped_lock sl(mMapLock); - - mMap.erase(cpClient); + { // we cannot destroy the connection while holding the map lock or we deadlock with pubLedger + typedef boost::shared_ptr< WSConnection > wsc_ptr; + wsc_ptr ptr; + { + boost::mutex::scoped_lock sl(mMapLock); + typename boost::unordered_map::iterator it = mMap.find(cpClient); + if (it == mMap.end()) + return; + ptr = it->second; // prevent the WSConnection from being destroyed until we release the lock + mMap.erase(cpClient); + } } void on_message(connection_ptr cpClient, message_ptr mpMessage)