Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Received payload is corrupted depending on message properties #585

Closed
sammck opened this issue Jul 14, 2021 · 2 comments
Closed

Received payload is corrupted depending on message properties #585

sammck opened this issue Jul 14, 2021 · 2 comments
Milestone

Comments

@sammck
Copy link

sammck commented Jul 14, 2021

I have noticed that received payloads are consistently corrupted with an extra byte at the front, when certain message properties are set on the publishing side. It seems to be related to the total length of the attached properties. I have verified that the bug is on the receiving side rather than the sending side, because other MQTT clients subscribed to the topic do not see any problem with the received payloads.

I'm using the latest release in PyPI.

The following code demonstrates the problem:


import logging

from typing import List

logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)

import paho.mqtt.client as mqtt
from paho.mqtt.packettypes import PacketTypes
import uuid
import json
import sys

port = 1883

host = 'mqtt.eclipse.org'
username = None
password = None

id = str(uuid.uuid4())
req_topic = "%s/req" % id
resp_topic = "%s/resp" % id

payload: bytes = '{"value": "this is just a test request #0"}'.encode('utf-8')
n_received: int = 0

def on_connect(client: mqtt.Client, userdata, flags, rc: mqtt.ReasonCodes, properties: mqtt.Properties):
    logger.debug("on_connect: Connected with result code %s" % rc.getName())
    client.subscribe(req_topic)

def on_subscribe(client: mqtt.Client, userdata, mid, rc: List[mqtt.ReasonCodes], properties: mqtt.Properties):
    logger.debug("on_subscribe: subcription acked with mid=%s, rc=%s" % (mid, [x.getName() for x in rc]))
    client.publish(req_topic, payload=payload, qos=1)
    properties = mqtt.Properties(PacketTypes.PUBLISH)
    properties.ContentType = 'application/json'
    properties.ResponseTopic = resp_topic
    properties.CorrelationData = b'     0'
    properties.UserProperty = [('ts', '2021-07-14T07:09:40.365778Z'), ('start_ts', '2021-07-14T07:09:40.208527Z'), ('seq', '2')]
    client.publish(req_topic, payload=payload, qos=1, properties=properties)

def on_message(client: mqtt.Client, userdata, msg: mqtt.MQTTMessage):
    global n_received
    logger.info("on_message %s: props=%s: [%s]" % (msg.topic, str(msg.properties), str(msg.payload)))
    n_received += 1

    if repr(msg.payload) != repr(payload):
        logger.error("Data Corruption! Received payload %s does not match published payload %s" % (msg.payload, payload))

    if n_received == 2:
        client.disconnect()

client = mqtt.Client(protocol=mqtt.MQTTv5)
client.on_connect = on_connect
client.on_message = on_message
client.on_subscribe = on_subscribe
client.enable_logger()
if not username is None:
  client.username_pw_set(username, password=password)

logger.info('Connecting to MQTT broker at %s:%d' % (host, port))

client.connect(host, port=port)

client.loop_forever()
@sammck
Copy link
Author

sammck commented Jul 14, 2021

Aha, this appears to be a duplicate of #541

@ralight
Copy link
Contributor

ralight commented Jul 21, 2021

Yes it is, but thank you for the report anyway!

@ralight ralight closed this as completed Jul 21, 2021
@ralight ralight added this to the 1.6.0 milestone Jul 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants