This results in QoS 1/2 messages being sent in the incorrect order.
import paho.mqtt.client as mqtt
import time
client = mqtt.Client(callback_api_version=2)
client.on_log = lambda client, userdata, level, buf: print(f"LOG: {time.time():.3f} - {buf}")
client.connect_async("broker.emqx.io", 1883)
client.loop_start()
for seq in range(5):
client.publish("test/replication", f"Message {seq}", qos=2)
time.sleep(0.1)
Publishing message 0...
Publishing message 1...
Publishing message 2...
LOG: 1777419226.510 - Sending CONNECT (u0, p0, wr0, wq0, wf0, c1, k60) client_id=b''
Publishing message 3...
LOG: 1777419226.561 - Sending PUBLISH (d0, q2, r0, m4), 'b'test/replication'', ... (9 bytes)
Publishing message 4...
LOG: 1777419226.662 - Sending PUBLISH (d0, q2, r0, m5), 'b'test/replication'', ... (9 bytes)
LOG: 1777419226.869 - Received CONNACK (0, 0)
LOG: 1777419226.869 - Sending PUBLISH (d0, q2, r0, m1), 'b'test/replication'', ... (9 bytes)
LOG: 1777419226.869 - Sending PUBLISH (d0, q2, r0, m2), 'b'test/replication'', ... (9 bytes)
LOG: 1777419226.869 - Sending PUBLISH (d0, q2, r0, m3), 'b'test/replication'', ... (9 bytes)
LOG: 1777419226.869 - Received PUBREC (Mid: 4)
LOG: 1777419226.869 - Sending PUBREL (Mid: 4)
LOG: 1777419226.869 - Received PUBREC (Mid: 5)
LOG: 1777419226.869 - Sending PUBREL (Mid: 5)
LOG: 1777419227.047 - Received PUBREC (Mid: 1)
LOG: 1777419227.047 - Sending PUBREL (Mid: 1)
LOG: 1777419227.225 - Received PUBREC (Mid: 2)
LOG: 1777419227.225 - Sending PUBREL (Mid: 2)
LOG: 1777419227.225 - Received PUBREC (Mid: 3)
LOG: 1777419227.225 - Sending PUBREL (Mid: 3)
LOG: 1777419227.226 - Received PUBCOMP (Mid: 4)
LOG: 1777419227.226 - Received PUBCOMP (Mid: 5)
LOG: 1777419227.403 - Received PUBCOMP (Mid: 1)
LOG: 1777419227.581 - Received PUBCOMP (Mid: 2)
LOG: 1777419227.581 - Received PUBCOMP (Mid: 3)
LOG: 1777419227.581 - Sending DISCONNECT
Bug Description
Any calls to
publish(...)during the period between CONNECT and CONNACK after aconnect_async(...)will be sent before any queued, unsent messages.This results in QoS 1/2 messages being sent in the incorrect order.
This only appears to be an issue with
connect_async,connectdoes not suffer the same problem.Reproduction
Environment
Linux res-0346 6.8.0-110-generic #110-Ubuntu SMP PREEMPT_DYNAMIC Thu Mar 19 15:09:20 UTC 2026 x86_64 x86_64 x86_64 GNU/LinuxLogs