Skip to content

fix: make quit work after connection loss#54

Open
foosel wants to merge 1 commit intosynman:develfrom
OctoPrint:fix/paho-mqtt-disconnect
Open

fix: make quit work after connection loss#54
foosel wants to merge 1 commit intosynman:develfrom
OctoPrint:fix/paho-mqtt-disconnect

Conversation

@foosel
Copy link
Copy Markdown
Contributor

@foosel foosel commented May 6, 2026

Ran into an issue with the Bambu Connector Plugin for OctoPrint: when losing connection to the printer (e.g. because it was powered off), bpm stayed active and then caused issues if at some later point in time the printer returned. Easy fix, just react to the disconnect from MQTT and tell bpm to disconnect. However, that then got stuck in the thread join and everything hung for good.

So I went digging and learned a lot about paho mqtt and its loop_forever method, which culminated in this fix.

A connection loss (printer goes offline) would formerly be seen as "client already disconnected" by bpm and thus the mqtt client wouldn't be told to disconnect. However, in the specific state of an unexpected connection loss the mqtt client's loop_forever will continue to attempt to reconnect forever. So if we call quit after a disconnect, to close things for good, instead of actually disconnecting and ending the session, bpm will get stuck on the join as the mqtt client's loop_forever will never quit and thus the thread never end.

Making sure to always call disconnect on the mqtt client (if we have it) solves this.

I also switched to checking self._client here, as self.client always creates a new client (is that really intentionally? It feels wrong to me).

A connection loss (printer goes offline) would formerly be seen as "client already disconnected"
and thus the mqtt client wouldn't be disconnected. However, in that specific state the mqtt
client's loop_forever will continue to attempt to reconnect forever. So if we call quit after
a disconnect, to close things for good, instead of actually disconnecting and ending the session,
bpm will get stuck on the join as the mqtt client's loop_forever will never quit and thus
the thread never end.

Making sure to always call disconnect on the mqtt client (if we have it) solves this.
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