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

Fix: retained message delayed on TLS connection #343

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

wpickard
Copy link

Hi, my name is William Pickard and I work for Toro Robotics Colorado.

We noticed that on subscription to topics with a retained message over TLS, the retained message for that topic is received after a considerable delay (seems to be until after the first keepalive).

A review of the paho-mqtt project turned up and older PR addressing this issue:

See: eclipse-paho/paho.mqtt.python#131

This PR ports the above fix from paho-mqtt to the aiomqtt client.

Minimal steps to reproduce:

import aiomqtt
import asyncio
from async_timeout import timeout
import ssl

HOSTNAME = 'test.mosquitto.org'
TOPIC = '/tests/aiomqtt/test_delayed_retain_ssl'

# The TLS cert on test.mosquitto.org seems broken
CONTEXT = ssl.create_default_context()
CONTEXT.check_hostname = False
CONTEXT.verify_mode = ssl.VerifyMode.CERT_NONE

async def test_retained_sub():
    async with aiomqtt.Client(HOSTNAME, 8883, tls_context=CONTEXT) as client:
        await client.publish(TOPIC, payload='hello', retain=True)
        print('Published retained message')
        await client.subscribe(TOPIC)
        
        # This isn't a perfet timing test. It should be almost immediate.
        # The timeout can be removed to observed the total delay.
        async with timeout(1):
            async for message in client.messages:
                print(f'Received {message.payload.decode()}')
                break
            
asyncio.run(test_retained_sub())

@tsturzl
Copy link

tsturzl commented Dec 5, 2024

Experiencing this issue also, this fix seems to resolve the issue.

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

Successfully merging this pull request may close these issues.

2 participants