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

copy _subscriptions so that it can't be altered within the loop #21

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

Conversation

tillschoepe
Copy link

Fix for Dictionary Size Change During Iteration in _on_connect

This PR addresses Issue #20, where _on_connect in _client.py (line 168) fails when multiple clients are running and one client publishes a new topic while another subscribes. The root cause is that self._subscriptions is modified during iteration, leading to a RuntimeError: dictionary changed size during iteration.

Fix

I converted the iterator into a list before iterating over self._subscriptions. This ensures that the loop does not reference the dictionary directly, preventing modifications during iteration.

Changes

def _on_connect(self, rc: int) -> None:
    check_connack_code(rc)
    self._births.clear()
    for topic, qos in list(self._subscriptions.items()):  # Convert to list to avoid runtime error
        self._subscribe(topic=topic, qos=qos)

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.

1 participant