From 898cc961adea236b12354ac4e5bf496a57493da2 Mon Sep 17 00:00:00 2001 From: Thulinma Date: Mon, 30 Sep 2013 16:04:06 +0200 Subject: [PATCH] Fixed HTTP proxy race condition. --- src/connectors/conn_http.cpp | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/connectors/conn_http.cpp b/src/connectors/conn_http.cpp index e2b7ca6c..3d866692 100644 --- a/src/connectors/conn_http.cpp +++ b/src/connectors/conn_http.cpp @@ -58,6 +58,8 @@ namespace Connector_HTTP { std::map connectorConnections; ///< Connections to connectors tthread::mutex connMutex; ///< Mutex for adding/removing connector connections. + bool timeoutThreadStarted = false; + tthread::mutex timeoutStartMutex; ///< Mutex for starting timeout thread. tthread::mutex timeoutMutex; ///< Mutex for timeout thread. tthread::thread * timeouter = 0; ///< Thread that times out connections to connectors. JSON::Value capabilities; ///< Holds a list of all HTTP connectors and their properties @@ -67,6 +69,7 @@ namespace Connector_HTTP { void proxyTimeoutThread(void * n){ n = 0; //prevent unused variable warning tthread::lock_guard guard(timeoutMutex); + timeoutThreadStarted = true; while (true){ { tthread::lock_guard guard(connMutex); @@ -414,14 +417,18 @@ namespace Connector_HTTP { std::cout << "Re-using connection " << uid << std::endl; #endif } - //start a new timeout thread, if neccesary - if (timeoutMutex.try_lock()){ - if (timeouter){ - timeouter->join(); - delete timeouter; + {//start a new timeout thread, if neccesary + tthread::lock_guard guard(timeoutStartMutex); + if (timeoutMutex.try_lock()){ + if (timeouter){ + timeouter->join(); + delete timeouter; + } + timeoutThreadStarted = false; + timeouter = new tthread::thread(Connector_HTTP::proxyTimeoutThread, 0); + timeoutMutex.unlock(); + while (!timeoutThreadStarted){Util::sleep(10);} } - timeouter = new tthread::thread(Connector_HTTP::proxyTimeoutThread, 0); - timeoutMutex.unlock(); } //lock the mutex for this connection, and handle the request