From 1d8edc07227311c83c75cf168173d5f7f941350c Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Mon, 24 Mar 2008 04:38:43 +0000 Subject: [PATCH] fixed race condition in dht --- include/libtorrent/kademlia/dht_tracker.hpp | 7 +++++++ src/kademlia/dht_tracker.cpp | 15 ++++++++++++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/include/libtorrent/kademlia/dht_tracker.hpp b/include/libtorrent/kademlia/dht_tracker.hpp index db01b7b64..0d0064af5 100644 --- a/include/libtorrent/kademlia/dht_tracker.hpp +++ b/include/libtorrent/kademlia/dht_tracker.hpp @@ -45,6 +45,7 @@ POSSIBILITY OF SUCH DAMAGE. #include #include #include +#include #include "libtorrent/kademlia/node.hpp" #include "libtorrent/kademlia/node_id.hpp" @@ -117,6 +118,12 @@ namespace libtorrent { namespace dht dht_settings const& m_settings; int m_refresh_bucket; + // The mutex is used to abort the dht node + // it's only used to set m_abort to true + typedef boost::mutex mutex_t; + mutable mutex_t m_mutex; + bool m_abort; + // used to resolve hostnames for nodes udp::resolver m_host_resolver; diff --git a/src/kademlia/dht_tracker.cpp b/src/kademlia/dht_tracker.cpp index 4336823b6..bc638d045 100644 --- a/src/kademlia/dht_tracker.cpp +++ b/src/kademlia/dht_tracker.cpp @@ -155,6 +155,7 @@ namespace libtorrent { namespace dht , m_refresh_timer(sock.get_io_service()) , m_settings(settings) , m_refresh_bucket(160) + , m_abort(false) , m_host_resolver(sock.get_io_service()) , m_refs(0) { @@ -212,6 +213,8 @@ namespace libtorrent { namespace dht void dht_tracker::stop() { + mutex_t::scoped_lock l(m_mutex); + m_abort = true; m_timer.cancel(); m_connection_timer.cancel(); m_refresh_timer.cancel(); @@ -228,7 +231,9 @@ namespace libtorrent { namespace dht void dht_tracker::connection_timeout(asio::error_code const& e) try { - if (e) return; + mutex_t::scoped_lock l(m_mutex); + if (e || m_abort) return; + time_duration d = m_dht.connection_timeout(); m_connection_timer.expires_from_now(d); m_connection_timer.async_wait(bind(&dht_tracker::connection_timeout, self(), _1)); @@ -245,7 +250,9 @@ namespace libtorrent { namespace dht void dht_tracker::refresh_timeout(asio::error_code const& e) try { - if (e) return; + mutex_t::scoped_lock l(m_mutex); + if (e || m_abort) return; + time_duration d = m_dht.refresh_timeout(); m_refresh_timer.expires_from_now(d); m_refresh_timer.async_wait( @@ -259,7 +266,9 @@ namespace libtorrent { namespace dht void dht_tracker::tick(asio::error_code const& e) try { - if (e) return; + mutex_t::scoped_lock l(m_mutex); + if (e || m_abort) return; + m_timer.expires_from_now(minutes(tick_period)); m_timer.async_wait(bind(&dht_tracker::tick, self(), _1));