Skip to content

QoS2 sends messages out-of-order if published between CONNECT and CONNACK when using connect_async() #910

Description

@dcrowe

Bug Description

Any calls to publish(...) during the period between CONNECT and CONNACK after a connect_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, connect does not suffer the same problem.

Reproduction

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)

Environment

  • Python version: Python 3.12.13
  • Library version: 2.1.0
  • Operating system (including version): 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/Linux
  • MQTT server: broker.emqx.io; has also been observed against a paid EMQX instance

Logs


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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Status: AvailableNo one has claimed responsibility for resolving this issue.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions