diff --git a/core/future-util.hh b/core/future-util.hh index 3b692f1760..3e32754d9a 100644 --- a/core/future-util.hh +++ b/core/future-util.hh @@ -38,17 +38,22 @@ template static inline void do_until_continued(StopCondition&& stop_cond, AsyncAction&& action, promise<> p) { while (!stop_cond()) { - auto&& f = action(); - if (!f.available()) { - f.then([action = std::forward(action), + try { + auto&& f = action(); + if (!f.available()) { + f.then([action = std::forward(action), stop_cond = std::forward(stop_cond), p = std::move(p)]() mutable { - do_until_continued(stop_cond, std::forward(action), std::move(p)); - }); - return; - } + do_until_continued(stop_cond, std::forward(action), std::move(p)); + }); + return; + } - if (f.failed()) { - f.forward_to(std::move(p)); + if (f.failed()) { + f.forward_to(std::move(p)); + return; + } + } catch (...) { + p.set_exception(std::current_exception()); return; } }