From 9b5aa967b0d17a5c004e4781dd53cc168d6d266d Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Fri, 23 Jan 2009 16:40:00 +0000 Subject: [PATCH] fixed race condition when saving DHT state --- ChangeLog | 1 + include/libtorrent/aux_/session_impl.hpp | 6 +++++- src/session_impl.cpp | 18 +++++++++++++++++- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index 611117578..52b08acac 100644 --- a/ChangeLog +++ b/ChangeLog @@ -47,6 +47,7 @@ release 0.14.2 * fixed bug in http_connection when binding to a particular IP * fixed typo in python binding (torrent_handle::piece_prioritize should be torrent_handle::piece_priorities) + * fixed race condition when saving DHT state release 0.14.1 diff --git a/include/libtorrent/aux_/session_impl.hpp b/include/libtorrent/aux_/session_impl.hpp index b72d7fcd0..d21e2aef0 100644 --- a/include/libtorrent/aux_/session_impl.hpp +++ b/include/libtorrent/aux_/session_impl.hpp @@ -53,6 +53,7 @@ POSSIBILITY OF SUCH DAMAGE. #include #include #include +#include #ifdef _MSC_VER #pragma warning(pop) @@ -176,6 +177,7 @@ namespace libtorrent dht_settings const& get_dht_settings() const { return m_dht_settings; } void start_dht(entry const& startup_state); void stop_dht(); + entry dht_state() const; void maybe_update_udp_mapping(int nat, int local_port, int external_port); #endif @@ -327,6 +329,8 @@ namespace libtorrent // private: + void session_impl::dht_state_callback(boost::condition_variable_any& c + , entry& e, bool& done) const; void on_lsd_peer(tcp::endpoint peer, sha1_hash const& ih); #ifndef TORRENT_DISABLE_POOL_ALLOCATOR @@ -347,7 +351,7 @@ namespace libtorrent // this is where all active sockets are stored. // the selector can sleep while there's no activity on // them - io_service m_io_service; + mutable io_service m_io_service; // handles disk io requests asynchronously // peers have pointers into the disk buffer diff --git a/src/session_impl.cpp b/src/session_impl.cpp index b93d572ba..5346eea20 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -2331,11 +2331,27 @@ namespace aux { m_dht_settings.service_port = m_listen_interface.port(); } + void session_impl::dht_state_callback(boost::condition_variable_any& c + , entry& e, bool& done) const + { + if (m_dht) e = m_dht->state(); + mutex_t::scoped_lock l(m_mutex); + done = true; + l.unlock(); + c.notify_all(); + } + entry session_impl::dht_state() const { + boost::condition_variable_any cond; mutex_t::scoped_lock l(m_mutex); if (!m_dht) return entry(); - return m_dht->state(); + entry e; + bool done = false; + m_io_service.post(boost::bind(&session_impl::dht_state_callback + , this, boost::ref(cond), boost::ref(e), boost::ref(done))); + while (!done) cond.wait(l); + return e; } void session_impl::add_dht_node(std::pair const& node)