This repository was archived by the owner on Dec 9, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
Erase MessageHandler for topics without any registered handlers #229
Merged
hikinggrass
merged 6 commits into
main
from
bugfix/prevent-lingering-threads-from-message-handler
Jan 8, 2025
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
65e41d8
Erase MessageHandler for topics without any registered handlers
hikinggrass 164a929
Use .at() instead of [] for map access
hikinggrass 8988d60
Turn should have matching handler warning into verbose log
hikinggrass 1d15139
Merge branch 'main' into bugfix/prevent-lingering-threads-from-messag…
hikinggrass a0b67f8
Clarify when a subscription to a topic is necessary
hikinggrass 1090b46
clang-format
hikinggrass File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -364,9 +364,11 @@ void MQTTAbstractionImpl::on_mqtt_message(const Message& message) { | |
| } | ||
| lock.unlock(); | ||
|
|
||
| // TODO(kai): this should maybe not even be a warning since it can happen that we unsubscribe from a topic and | ||
| // have removed the message handler but the MQTT unsubscribe didn't complete yet and we still receive messages | ||
| // on this topic that we can just ignore | ||
| if (!found) { | ||
| EVLOG_AND_THROW( | ||
| EverestInternalError(fmt::format("Internal error: topic '{}' should have a matching handler!", topic))); | ||
| EVLOG_warning << fmt::format("Internal error: topic '{}' should have a matching handler!", topic); | ||
| } | ||
| } catch (boost::exception& e) { | ||
| EVLOG_critical << fmt::format("Caught MQTT on_message boost::exception:\n{}", | ||
|
|
@@ -451,15 +453,15 @@ void MQTTAbstractionImpl::register_handler(const std::string& topic, std::shared | |
| if (this->message_handlers.count(topic) == 0) { | ||
| this->message_handlers.emplace(std::piecewise_construct, std::forward_as_tuple(topic), std::forward_as_tuple()); | ||
| } | ||
| this->message_handlers[topic].add_handler(handler); | ||
| this->message_handlers.at(topic).add_handler(handler); | ||
|
|
||
| // only subscribe for this topic if we aren't already and the mqtt client is connected | ||
| // if we are not connected the on_mqtt_connect() callback will subscribe to the topic | ||
| if (this->mqtt_is_connected && this->message_handlers[topic].count_handlers() == 1) { | ||
| if (this->mqtt_is_connected && this->message_handlers.at(topic).count_handlers() == 1) { | ||
|
||
| EVLOG_verbose << fmt::format("Subscribing to {}", topic); | ||
| this->subscribe(topic, qos); | ||
| } | ||
| EVLOG_verbose << fmt::format("#handler[{}] = {}", topic, this->message_handlers[topic].count_handlers()); | ||
| EVLOG_verbose << fmt::format("#handler[{}] = {}", topic, this->message_handlers.at(topic).count_handlers()); | ||
| } | ||
|
|
||
| void MQTTAbstractionImpl::unregister_handler(const std::string& topic, const Token& token) { | ||
|
|
@@ -484,6 +486,7 @@ void MQTTAbstractionImpl::unregister_handler(const std::string& topic, const Tok | |
| EVLOG_verbose << fmt::format("Unsubscribing from {}", topic); | ||
| this->unsubscribe(topic); | ||
| } | ||
| this->message_handlers.erase(topic); | ||
| } | ||
|
|
||
| const std::string handler_count = (number_of_handlers == 0) ? "None" : std::to_string(number_of_handlers); | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not really an error, as is written in the comment, this is an valid but unlikely event.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed to a verbose log and slightly reworded the comment