From 6f28667aa3aea43b6bc697a9896d8e44fd12a7da Mon Sep 17 00:00:00 2001 From: moneromooo-monero Date: Mon, 15 Oct 2018 22:10:49 +0000 Subject: [PATCH] daemon: fix reading past stack on exit --- src/daemon/daemon.cpp | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/daemon/daemon.cpp b/src/daemon/daemon.cpp index f536495182b..49d6d49cf71 100644 --- a/src/daemon/daemon.cpp +++ b/src/daemon/daemon.cpp @@ -137,13 +137,18 @@ bool t_daemon::run(bool interactive) throw std::runtime_error{"Can't run stopped daemon"}; } - std::atomic stop(false); - boost::thread([&stop, this] { + std::atomic stop(false), shutdown(false); + boost::thread stop_thread = boost::thread([&stop, &shutdown, this] { while (!stop) epee::misc_utils::sleep_no_w(100); - this->stop_p2p(); - }).detach(); - tools::signal_handler::install([&stop](int){ stop = true; }); + if (shutdown) + this->stop_p2p(); + }); + epee::misc_utils::auto_scope_leave_caller scope_exit_handler = epee::misc_utils::create_scope_leave_handler([&](){ + stop = true; + stop_thread.join(); + }); + tools::signal_handler::install([&stop, &shutdown](int){ stop = shutdown = true; }); try {