From eb47eb864a22c2ccbf829a742cae53803b7dbe78 Mon Sep 17 00:00:00 2001 From: Juha Ylikoski Date: Fri, 30 Dec 2022 11:20:43 +0200 Subject: [PATCH] Respoect max_inlight message count in reconnect Previously if the message queue filled up for some reason like bad connection and the client disconnected (due to e.g. bad connection), it would send all of it's messages in _out_messages-queue and not respect _max_inflight_messages parameter. With some brokers and when we had 1000s of messages in queue and all being sent with qos 1 without any regard to the _max_inflight_messages, the broker would disconnect us or the connection would again break down. This patch makes sure _max_inflight_messages is respected when reconnect happens for any reason. I believe this also fixes #492 Signed-off-by: Juha Ylikoski --- src/paho/mqtt/client.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/paho/mqtt/client.py b/src/paho/mqtt/client.py index 1c0236e4..8b39e3eb 100644 --- a/src/paho/mqtt/client.py +++ b/src/paho/mqtt/client.py @@ -2945,9 +2945,10 @@ def _check_clean_session(self): def _messages_reconnect_reset_out(self): with self._out_message_mutex: self._inflight_messages = 0 + to_be_inflight = 0 for m in self._out_messages.values(): m.timestamp = 0 - if self._max_inflight_messages == 0 or self._inflight_messages < self._max_inflight_messages: + if self._max_inflight_messages == 0 or to_be_inflight < self._max_inflight_messages: if m.qos == 0: m.state = mqtt_ms_publish elif m.qos == 1: @@ -2955,6 +2956,7 @@ def _messages_reconnect_reset_out(self): if m.state == mqtt_ms_wait_for_puback: m.dup = True m.state = mqtt_ms_publish + to_be_inflight += 1 elif m.qos == 2: # self._inflight_messages = self._inflight_messages + 1 if self._check_clean_session(): @@ -2968,6 +2970,7 @@ def _messages_reconnect_reset_out(self): if m.state == mqtt_ms_wait_for_pubrec: m.dup = True m.state = mqtt_ms_publish + to_be_inflight += 1 else: m.state = mqtt_ms_queued