diff --git a/include/libp2p/protocol/common/asio/asio_scheduler.hpp b/include/libp2p/protocol/common/asio/asio_scheduler.hpp index 81ad616e..18fcea51 100644 --- a/include/libp2p/protocol/common/asio/asio_scheduler.hpp +++ b/include/libp2p/protocol/common/asio/asio_scheduler.hpp @@ -14,7 +14,7 @@ namespace libp2p::protocol { class AsioScheduler : public Scheduler { public: - AsioScheduler(const std::shared_ptr &io, + AsioScheduler(std::shared_ptr io, SchedulerConfig config); ~AsioScheduler() override; @@ -28,7 +28,7 @@ namespace libp2p::protocol { void onImmediate(); - std::weak_ptr io_; + std::shared_ptr io_; Ticks interval_; boost::asio::deadline_timer timer_; boost::posix_time::ptime started_; diff --git a/src/protocol/common/asio/asio_scheduler.cpp b/src/protocol/common/asio/asio_scheduler.cpp index 9b9ce67a..2ce07213 100644 --- a/src/protocol/common/asio/asio_scheduler.cpp +++ b/src/protocol/common/asio/asio_scheduler.cpp @@ -9,7 +9,7 @@ namespace libp2p::protocol { - AsioScheduler::AsioScheduler(const std::shared_ptr &io, + AsioScheduler::AsioScheduler(std::shared_ptr io, SchedulerConfig config) : io_(io), interval_(config.period_msec), @@ -28,8 +28,10 @@ namespace libp2p::protocol { AsioScheduler::~AsioScheduler() { *canceled_ = true; - if (!io_.expired()) { + try { timer_.cancel(); + } catch (...) { + // may throw } }; @@ -41,18 +43,15 @@ namespace libp2p::protocol { void AsioScheduler::scheduleImmediate() { if (!immediate_cb_scheduled_) { immediate_cb_scheduled_ = true; - auto io = io_.lock(); - if (io) io->post(immediate_cb_); + io_->post(immediate_cb_); } } void AsioScheduler::onTimer() { pulse(false); - if (!io_.expired()) { - timer_.expires_at(timer_.expires_at() + timer_.expires_at(timer_.expires_at() + boost::posix_time::milliseconds(interval_)); - timer_.async_wait(timer_cb_); - } + timer_.async_wait(timer_cb_); } void AsioScheduler::onImmediate() {