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

How to correctly disconnect & clean up #216

Open
patrickjane opened this issue Jun 4, 2020 · 0 comments
Open

How to correctly disconnect & clean up #216

patrickjane opened this issue Jun 4, 2020 · 0 comments

Comments

@patrickjane
Copy link

I hope I am not duplicating #106 here, but while this isse has been resolved long time ago, I still get the mentioned error when I try to gracefully shutdown my program.

In a nutshell:

def main():
    loop = asyncio.get_event_loop()
    mq = mqtt.Mqtt()  # my wrapper class

    try:
        loop.run_until_complete(mq.connect())
    except KeyboardInterrupt:
        pass
    except Exception as e:
        self.log.error("Got exception: {}".format(e))
    finally:
        loop.run_until_complete(mq.disconnect())
        loop.close()

class Mqtt

59    async def connect(self):
60        self.client = MQTTClient()
61
62        await self.client.connect(uri)
63        await self.client.subscribe([(self.topic, QOS_0)])
64
65        while True:
66            try:
67                message = await self.client.deliver_message()
68                await ... # handle message coro
69            except ClientException as e:
70                self.log.error("MQTT Client exception: {}".format(e))
71                break
72            except Exception as e:
73                self.log.error("MQTT exception: {}".format(e))
74                break

80    async def disconnect(self):
81        await self.client.unsubscribe([self.topic])
82        await self.client.disconnect()

Now when I do CTRL+C, it should interrupt the loop execution and call the stop-logic in the finally:-block, which will ultimately call unsubscribe/disconnect.

However I get the following exception:

future: <Task finished coro=<Mqtt.connect() done, defined at /Users/s710/Development/myapp/mqtt.py:59> exception=IndexError('pop from an empty deque')>
Traceback (most recent call last):
  File "/Users/s710/Development/myapp/mqtt.py", line 67, in connect
    message = await self.client.deliver_message()
  File "/Users/s710/Development/myapp/venv/lib/python3.7/site-packages/hbmqtt/client.py", line 356, in deliver_message
    self.client_tasks.pop()
IndexError: pop from an empty deque

Do I have some general misconception here (I am still new to asyncio programming), or can this error be ignored?

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

No branches or pull requests

1 participant