diff --git a/src/cpp/rtps/writer/LivelinessManager.cpp b/src/cpp/rtps/writer/LivelinessManager.cpp index a507776704f..b48c61b53f8 100644 --- a/src/cpp/rtps/writer/LivelinessManager.cpp +++ b/src/cpp/rtps/writer/LivelinessManager.cpp @@ -61,6 +61,19 @@ bool LivelinessManager::add_writer( } } writers_.emplace_back(guid, kind, lease_duration); + + if (!calculate_next()) + { + timer_.cancel_timer(); + return true; + } + + // Some times the interval could be negative if a writer expired during the call to this function + // Once in this situation there is not much we can do but let asio timers expire immediately + auto interval = timer_owner_->time - steady_clock::now(); + timer_.update_interval_millisec((double)duration_cast(interval).count()); + timer_.restart_timer(); + return true; } @@ -95,9 +108,8 @@ bool LivelinessManager::remove_writer( } } - if (timer_owner_ != nullptr && timer_owner_->guid == guid) + if (timer_owner_ != nullptr) { - timer_owner_ = nullptr; if (!calculate_next()) { timer_.cancel_timer();