From b701fb252aa166314b13663ab2be29e181668b46 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Sat, 20 Aug 2016 11:04:44 -0400 Subject: [PATCH] clear alert nofify function when calling abort on a session, to avoid touching a destructed session object (#1015) --- include/libtorrent/session.hpp | 2 +- simulation/test_checking.cpp | 1 - simulation/test_fast_extensions.cpp | 1 - simulation/test_ip_filter.cpp | 1 - simulation/test_optimistic_unchoke.cpp | 1 - simulation/test_socks5.cpp | 1 - simulation/test_tracker.cpp | 2 -- simulation/test_transfer.cpp | 1 - simulation/test_web_seed.cpp | 1 - src/session.cpp | 8 ++++++++ src/session_impl.cpp | 4 ++++ 11 files changed, 13 insertions(+), 10 deletions(-) diff --git a/include/libtorrent/session.hpp b/include/libtorrent/session.hpp index 7f0f85264..f16f41e15 100644 --- a/include/libtorrent/session.hpp +++ b/include/libtorrent/session.hpp @@ -281,7 +281,7 @@ namespace libtorrent // session_proxy(); // ~session_proxy() // }; - session_proxy abort() { return session_proxy(m_io_service, m_thread, m_impl); } + session_proxy abort(); private: diff --git a/simulation/test_checking.cpp b/simulation/test_checking.cpp index 9fd3e044d..9754b174e 100644 --- a/simulation/test_checking.cpp +++ b/simulation/test_checking.cpp @@ -69,7 +69,6 @@ void run_test(Setup const& setup, Test const& test) test(*ses); // shut down - ses->set_alert_notify([]{}); zombie = ses->abort(); ses.reset(); }); diff --git a/simulation/test_fast_extensions.cpp b/simulation/test_fast_extensions.cpp index ae3350e22..3c50126d0 100644 --- a/simulation/test_fast_extensions.cpp +++ b/simulation/test_fast_extensions.cpp @@ -79,7 +79,6 @@ void run_fake_peer_test( sim::timer t(sim, lt::seconds(1) , [&](boost::system::error_code const& ec) { - ses->set_alert_notify([]{}); // shut down zombie = ses->abort(); diff --git a/simulation/test_ip_filter.cpp b/simulation/test_ip_filter.cpp index 576dcb7d7..becadefe1 100644 --- a/simulation/test_ip_filter.cpp +++ b/simulation/test_ip_filter.cpp @@ -88,7 +88,6 @@ void run_test(Setup const& setup { test(*ses, test_peers); - ses->set_alert_notify([]{}); // shut down zombie = ses->abort(); diff --git a/simulation/test_optimistic_unchoke.cpp b/simulation/test_optimistic_unchoke.cpp index f6ff2dc00..78e560959 100644 --- a/simulation/test_optimistic_unchoke.cpp +++ b/simulation/test_optimistic_unchoke.cpp @@ -148,7 +148,6 @@ TORRENT_TEST(optimistic_unchoke) { p->abort(); } - ses->set_alert_notify([]{}); proxy = ses->abort(); ses.reset(); }); diff --git a/simulation/test_socks5.cpp b/simulation/test_socks5.cpp index b0d78834c..f4940ec4a 100644 --- a/simulation/test_socks5.cpp +++ b/simulation/test_socks5.cpp @@ -101,7 +101,6 @@ void run_test(Setup const& setup { fprintf(stderr, "shutting down\n"); // shut down - ses->set_alert_notify([] {}); zombie = ses->abort(); ses.reset(); }); diff --git a/simulation/test_tracker.cpp b/simulation/test_tracker.cpp index 64ae26d4f..e5abcee0f 100644 --- a/simulation/test_tracker.cpp +++ b/simulation/test_tracker.cpp @@ -352,7 +352,6 @@ TORRENT_TEST(ipv6_support) , [&ses,&zombie](boost::system::error_code const& ec) { zombie = ses->abort(); - ses->set_alert_notify([]{}); ses.reset(); }); @@ -427,7 +426,6 @@ void tracker_test(Setup setup, Announce a, Test1 test1, Test2 test2 , [&ses,&zombie](boost::system::error_code const& ec) { zombie = ses->abort(); - ses->set_alert_notify([]{}); ses.reset(); }); diff --git a/simulation/test_transfer.cpp b/simulation/test_transfer.cpp index ddc3aceaf..5b732e170 100644 --- a/simulation/test_transfer.cpp +++ b/simulation/test_transfer.cpp @@ -139,7 +139,6 @@ void run_test( int idx = 0; for (auto& s : ses) { - s->set_alert_notify([]{}); zombie[idx++] = s->abort(); s.reset(); } diff --git a/simulation/test_web_seed.cpp b/simulation/test_web_seed.cpp index 1468f7db6..da99e5dc8 100644 --- a/simulation/test_web_seed.cpp +++ b/simulation/test_web_seed.cpp @@ -113,7 +113,6 @@ void run_test(Setup const& setup { fprintf(stderr, "shutting down\n"); // shut down - ses->set_alert_notify([] {}); zombie = ses->abort(); ses.reset(); }); diff --git a/src/session.cpp b/src/session.cpp index a0bac8e5f..125f4f310 100644 --- a/src/session.cpp +++ b/src/session.cpp @@ -409,6 +409,14 @@ namespace libtorrent m_thread->join(); } + session_proxy session::abort() + { + // stop calling the alert notify function now, to avoid it thinking the + // session is still alive + m_impl->alerts().set_notify_function(boost::function()); + return session_proxy(m_io_service, m_thread, m_impl); + } + #ifndef TORRENT_NO_DEPRECATE session_settings::session_settings(std::string const& user_agent_) { diff --git a/src/session_impl.cpp b/src/session_impl.cpp index 33f0ddc6d..c27a551c2 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -1076,6 +1076,10 @@ namespace aux { session_log(" *** ABORT CALLED ***"); #endif + // at this point we cannot call the notify function anymore, since the + // session will become invalid. + m_alerts.set_notify_function(boost::function()); + // this will cancel requests that are not critical for shutting down // cleanly. i.e. essentially tracker hostname lookups that we're not // about to send event=stopped to