sbtinstruments / aiomqtt

The idiomatic asyncio MQTT client, wrapped around paho-mqtt
https://sbtinstruments.github.io/aiomqtt
BSD 3-Clause "New" or "Revised" License
392 stars 71 forks source link

Cannot instantiate a client due to internal mqtt problem #289

Closed mbait closed 3 months ago

mbait commented 3 months ago

The following code

import asyncio
import time

import asyncio_mqtt

async def main():
    async with asyncio_mqtt.Client('localhost') as client:
        while True:
            ts = int(time.time() * 1e+3)
            client.publish('meters/volts', f'Urms u0=0 {ts}')
            await asyncio.sleep(1)

asyncio.run(main())

fails with

Exception ignored in: <function Client.__del__ at 0x7f6c4866a0e0>
Traceback (most recent call last):
  File "/home/mbait/src/smart-machines/.venv/lib/python3.10/site-packages/paho/mqtt/client.py", line 874, in __del__
    self._reset_sockets()
  File "/home/mbait/src/smart-machines/.venv/lib/python3.10/site-packages/paho/mqtt/client.py", line 1133, in _reset_sockets
    self._sock_close()
  File "/home/mbait/src/smart-machines/.venv/lib/python3.10/site-packages/paho/mqtt/client.py", line 1119, in _sock_close
    if not self._sock:
AttributeError: 'Client' object has no attribute '_sock'
Traceback (most recent call last):
  File "/home/mbait/src/smart-machines/mqtt-test.py", line 15, in <module>
    asyncio.run(main())
  File "/usr/lib/python3.10/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/usr/lib/python3.10/asyncio/base_events.py", line 649, in run_until_complete
    return future.result()
  File "/home/mbait/src/smart-machines/mqtt-test.py", line 8, in main
    async with asyncio_mqtt.Client('iot.mmr.systems') as client:
  File "/home/mbait/src/smart-machines/.venv/lib/python3.10/site-packages/asyncio_mqtt/client.py", line 292, in __init__
    self._client: mqtt.Client = mqtt.Client(
TypeError: Client.__init__() missing 1 required positional argument: 'callback_api_version'

Process finished with exit code 1

Looking at the offensive line of the code it seems the error is legit as, indeed, asyncio-mqtt doesn't provide the required callback_api_version. I guess that effectively renders all examples from the library docs as broken.

empicano commented 3 months ago

Hi Alexander!

(We renamed the project from asyncio_mqtt to aiomqtt with version 1.0.0. New versions are distributed only under the aiomqtt name.)

In older versions we specified the paho-mqtt dependency as >=1.0.0. Paho released a 2.0.0 version not long ago, which breaks older versions of asyncio_mqtt / aiomqtt, that's our fault. The issue was fixed in commit bb2ad6c, which is included from asyncio_mqtt / aiomqtt version 1.0.0 onwards.

I think our two options are (1.) pinning paho-mqtt to ^1.0.0 in your requirements manually or (2.) upgrading to a newer version of asyncio_mqtt / aiomqtt.

Let me know if that helps! 🙂

mbait commented 3 months ago

Thank you for the explanation. For now I've manually downgraded paho from 2.0.0 to 1.6.1, but will switch to aiomqtt eventually.