Skip to content

Commit

Permalink
fix: subscriber callback not removed (#136)
Browse files Browse the repository at this point in the history
* Updated remove_subscriber implementation to use reference when erasing callback from list to ensure that it is properly erased from the shared state
* Added some warnings if removal occurs when topic or component are not found
  • Loading branch information
finger563 authored Dec 28, 2023
1 parent 197249f commit af0852f
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion components/event_manager/src/event_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ bool EventManager::remove_subscriber(const std::string &topic, const std::string
{
std::lock_guard<std::recursive_mutex> lk(events_mutex_);
if (!events_.subscribers.contains(topic)) {
logger_.warn("Cannot remove subscriber, there are no subscribers for topic '{}'", topic);
// there is no publisher for this topic
return false;
}
Expand All @@ -128,6 +129,8 @@ bool EventManager::remove_subscriber(const std::string &topic, const std::string
auto elem = std::find(std::begin(topic_subscribers), std::end(topic_subscribers), component);
bool exists = elem != std::end(topic_subscribers);
if (!exists) {
logger_.warn("Cannot remove subscriber, '{}' is not registered as a subscriber for topic '{}'",
component, topic);
// component is not registered as a subscriber, so return false
return false;
}
Expand All @@ -138,7 +141,7 @@ bool EventManager::remove_subscriber(const std::string &topic, const std::string
// remove from `subscriber_callbacks_`
{
std::lock_guard<std::recursive_mutex> lk(callbacks_mutex_);
auto callbacks = subscriber_callbacks_[topic];
auto& callbacks = subscriber_callbacks_[topic];

auto is_component = [&component](std::pair<std::string, event_callback_fn> &e) {
return std::get<0>(e) == component;
Expand Down

0 comments on commit af0852f

Please sign in to comment.