From a27a2c35f0a50d5b03b3731f01f595410fe28ec7 Mon Sep 17 00:00:00 2001 From: Carl Hetherington Date: Tue, 12 Feb 2019 22:18:43 +0000 Subject: [PATCH] Fix a couple of deadlocks caused by emitting ServersListChanged with a lock on _servers_mutex; handlers to that signal may call ::servers() which tries to take a lock on the same mutex. --- src/lib/encode_server_finder.cc | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/lib/encode_server_finder.cc b/src/lib/encode_server_finder.cc index 1837101b5..6cdd8ce3c 100644 --- a/src/lib/encode_server_finder.cc +++ b/src/lib/encode_server_finder.cc @@ -149,10 +149,10 @@ try } /* Discard servers that we haven't seen for a while */ + bool removed = false; { boost::mutex::scoped_lock lm (_servers_mutex); - bool removed = false; list::iterator i = _servers.begin(); while (i != _servers.end()) { if (i->last_seen_seconds() > 2 * interval) { @@ -165,10 +165,10 @@ try ++i; } } + } - if (removed) { - emit (boost::bind (boost::ref (ServersListChanged))); - } + if (removed) { + emit (boost::bind (boost::ref (ServersListChanged))); } boost::mutex::scoped_lock lm (_search_condition_mutex); @@ -236,8 +236,10 @@ EncodeServerFinder::handle_accept (boost::system::error_code ec, shared_ptrset_seen (); } else { EncodeServerDescription sd (ip, xml->number_child("Threads"), xml->optional_number_child("Version").get_value_or(0)); - boost::mutex::scoped_lock lm (_servers_mutex); - _servers.push_back (sd); + { + boost::mutex::scoped_lock lm (_servers_mutex); + _servers.push_back (sd); + } emit (boost::bind (boost::ref (ServersListChanged))); } -- 2.30.2