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: Iterating over received messages multiple times does not yield proper results #312

Merged
merged 2 commits into from
Jul 1, 2024

Conversation

ryan-summers
Copy link
Contributor

Because of the iterator construction in __init__, any attempt to iterate over client.messages after the initial iteration will yield no data.

Here's an MVP of the actual defect:

import sys
import os

# Change to the "Selector" event loop if platform is Windows
if sys.platform.lower() == "win32" or os.name.lower() == "nt":
    from asyncio import set_event_loop_policy, WindowsSelectorEventLoopPolicy
    set_event_loop_policy(WindowsSelectorEventLoopPolicy())
import asyncio
import aiomqtt


async def main():
    async with aiomqtt.Client("test.mosquitto.org") as client:
        async def listen():
            async for message in client.messages:
                print(message.payload)

        listen_task = asyncio.create_task(listen())

        await client.subscribe("temperature/#")

        try:
            await asyncio.wait_for(listen_task, timeout=0.5)
        except asyncio.TimeoutError:
            listen_task.cancel()

        # Should continue forever
        await listen()
        raise Exception("Listen returned too early!")


asyncio.run(main())

Running the MVP fails with current main, but works properly after this PR.

@empicano empicano linked an issue Jul 1, 2024 that may be closed by this pull request
@empicano empicano merged commit 7b2cce6 into empicano:main Jul 1, 2024
@empicano
Copy link
Owner

empicano commented Jul 1, 2024

LGTM 👍 Thank you for your first contribution to aiomqtt! 🎉

@empicano empicano mentioned this pull request Jul 1, 2024
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.

Broken messages generator with client context re-use
2 participants