diff --git a/src/util/BlockingCache.hpp b/src/util/BlockingCache.hpp index 0deccdd8..0f24b8a5 100644 --- a/src/util/BlockingCache.hpp +++ b/src/util/BlockingCache.hpp @@ -197,22 +197,31 @@ private: std::expected wait(boost::asio::yield_context yield, Updater updater, Verifier verifier) { - boost::asio::steady_timer timer{yield.get_executor(), boost::asio::steady_timer::duration::max()}; + struct SharedContext { + SharedContext(boost::asio::yield_context y) + : timer(y.get_executor(), boost::asio::steady_timer::duration::max()) + { + } + + boost::asio::steady_timer timer; + std::optional> result; + }; + + auto sharedContext = std::make_shared(yield); boost::system::error_code errorCode; - std::optional> result; boost::signals2::scoped_connection const slot = - updateFinished_.connect([yield, &timer, &result](std::expected value) { - boost::asio::spawn(yield, [&timer, &result, value = std::move(value)](auto&&) { - result = std::move(value); - timer.cancel(); + updateFinished_.connect([yield, sharedContext](std::expected value) { + boost::asio::spawn(yield, [sharedContext = std::move(sharedContext), value = std::move(value)](auto&&) { + sharedContext->result = std::move(value); + sharedContext->timer.cancel(); }); }); if (state_ == State::Updating) { - timer.async_wait(yield[errorCode]); - ASSERT(result.has_value(), "There should be some value after waiting"); - return std::move(result).value(); + sharedContext->timer.async_wait(yield[errorCode]); + ASSERT(sharedContext->result.has_value(), "There should be some value after waiting"); + return std::move(sharedContext->result).value(); } return asyncGet(yield, std::move(updater), std::move(verifier)); }