From cf081e7e258bbc45e83696f9eb457a0fe6e2a7ad Mon Sep 17 00:00:00 2001 From: Sergey Kuznetsov Date: Wed, 23 Oct 2024 14:24:05 +0100 Subject: [PATCH] fix: Fix timer spurious calls (#1700) Fixes #1634. I also checked other timers and they don't have the issue. --- src/util/Retry.cpp | 6 ++++++ src/util/Retry.hpp | 22 ++++++++++++++++------ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/util/Retry.cpp b/src/util/Retry.cpp index 0f720274..ec52034a 100644 --- a/src/util/Retry.cpp +++ b/src/util/Retry.cpp @@ -57,10 +57,16 @@ Retry::Retry(RetryStrategyPtr strategy, boost::asio::strand #include +#include #include #include #include @@ -80,6 +81,7 @@ class Retry { RetryStrategyPtr strategy_; boost::asio::steady_timer timer_; size_t attemptNumber_ = 0; + std::shared_ptr canceled_{std::make_shared(false)}; public: /** @@ -90,6 +92,11 @@ public: */ Retry(RetryStrategyPtr strategy, boost::asio::strand strand); + /** + * @brief Destroy the Retry object + */ + ~Retry(); + /** * @brief Schedule a retry * @@ -100,15 +107,18 @@ public: void retry(Fn&& func) { + *canceled_ = false; timer_.expires_after(strategy_->getDelay()); strategy_->increaseDelay(); - timer_.async_wait([this, func = std::forward(func)](boost::system::error_code const& ec) { - if (ec == boost::asio::error::operation_aborted) { - return; + timer_.async_wait( + [this, canceled = canceled_, func = std::forward(func)](boost::system::error_code const& ec) { + if (ec == boost::asio::error::operation_aborted or *canceled) { + return; + } + ++attemptNumber_; + func(); } - ++attemptNumber_; - func(); - }); + ); } /**