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

Creating a paho.mqtt.client.Client with invalid client_id and clean_session=False raises an AttributeError on deletion #520

Closed
chris-hibberd opened this issue Oct 15, 2020 · 1 comment · Fixed by home-assistant/core#59339
Milestone

Comments

@chris-hibberd
Copy link

For package version paho-mqtt 1.5.1 and Python 3.8.2, running the following code:

from paho.mqtt.client import Client
try:
    Client(client_id="", clean_session=False)
except ValueError as e:
    print(f"This is handled: {e}")

Produces the following output:

This is handled: A client id must be provided if clean session is False.
Exception ignored in: <function Client.__del__ at 0x7f4531fab0d0>
Traceback (most recent call last):
  File "/c/Users/hibbe/Code/bg-hub-applications/.venv/lib/python3.8/site-packages/paho/mqtt/client.py", line 660, in __del__
    self._reset_sockets()
  File "/c/Users/hibbe/Code/bg-hub-applications/.venv/lib/python3.8/site-packages/paho/mqtt/client.py", line 704, in _reset_sockets
    self._sock_close()
  File "/c/Users/hibbe/Code/bg-hub-applications/.venv/lib/python3.8/site-packages/paho/mqtt/client.py", line 691, in _sock_close
    if not self._sock:
AttributeError: 'Client' object has no attribute '_sock'

The error is raised in _sock_close when __del__ is called on the client as _sock_close checks if not self._sock: but the _sock attribute is never created on the client object as the exception is raised first (from __init__ of Client in .venv/lib/python3.8/site-packages/paho/mqtt/client.py):

            if not clean_session and (client_id == "" or client_id is None):
                raise ValueError(
                    'A client id must be provided if clean session is False.')
            self._clean_session = clean_session

        if transport.lower() not in ('websockets', 'tcp'):
            raise ValueError(
                'transport must be "websockets" or "tcp", not %s' % transport)
        self._transport = transport.lower()
        self._protocol = protocol
        self._userdata = userdata
        self._sock = None

Perhaps either attribute initialisation should be moved earlier in the constructor, or _sock_close should check that the attribute exists.

@ralight
Copy link
Contributor

ralight commented Jul 22, 2021

Thanks Chris, and sorry for the delay in getting to this. I've pushed a fix and this will be part of 1.6.0.

@ralight ralight closed this as completed Jul 22, 2021
@ralight ralight added this to the 1.6.0 milestone Jul 22, 2021
ralight added a commit that referenced this issue 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

Successfully merging a pull request may close this issue.

2 participants