Only sweep the subscriptions when one was removed during dispatch

This commit is contained in:
J. Nick Koston
2026-10-13 20:43:47 -10:00
parent cbf46c655b
commit 12f7bf4d5d
2 changed files with 8 additions and 2 deletions
+7 -2
View File
@@ -491,8 +491,11 @@ void MQTTClientComponent::unsubscribe(const std::string &topic) {
subscription.removed = true;
}
// A callback may be running; the outermost dispatch erases them once it returns.
if (!this->dispatching_)
if (this->dispatching_) {
this->removed_while_dispatching_ = true;
} else {
this->erase_removed_subscriptions_();
}
}
void MQTTClientComponent::erase_removed_subscriptions_() {
@@ -668,8 +671,10 @@ void MQTTClientComponent::on_message(const std::string &topic, const std::string
subscription.callback.call(topic, payload);
}
this->dispatching_ = was_dispatching;
if (!was_dispatching)
if (!was_dispatching && this->removed_while_dispatching_) {
this->removed_while_dispatching_ = false;
this->erase_removed_subscriptions_();
}
#ifdef USE_ESP8266
});
#endif
+1
View File
@@ -369,6 +369,7 @@ class MQTTClientComponent final : public Component {
bool publish_nan_as_none_{false};
bool wait_for_connection_{false};
bool dispatching_{false};
bool removed_while_dispatching_{false};
};
extern MQTTClientComponent *global_mqtt_client; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)