Repository navigation
fix: keep paho network thread alive when MQTT-info refresh fails on disconnect - #82
Merged
Merged
Conversation
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #67. Root cause of stackia/ha-deye-dehumidifier#105 as well.
Root cause
_mqtt_on_disconnectruns on paho's network thread and didasyncio.run_coroutine_threadsafe(self._set_mqtt_info(), self._loop).result().Two things are wrong with that:
_do_on_disconnectre-raises callback exceptions (suppress_exceptionsdefaults toFalse), and the exception propagates out ofloop_forever()/_thread_main(). The trigger in the issue is exactly the common case: the disconnect and the cloud outage share a cause (router reboot), soget_deye_platform_mqtt_info()fails with a DNS timeout ->DeyeCloudApiCannotConnectError-> thread gone. No reconnect, no keepalive, ever again.is_connected()keeps returningTrueafterwards. In paho's_loop_rc_handle(socket read/write error path)on_disconnectis called before_state = MQTT_CS_CONNECTION_LOST, so when the callback raises the state is never updated. That is why the integration'sMqttDisconnectMonitornever fires, entities stay "available", and_publish_command_bytescallspaho.publish()on a dead client instead of queueing to_pending_commands— commands vanish silently. Only reloading the integration (new client) recovers.Blocking the paho thread on the event loop is also a latent deadlock:
disconnect()runs on the loop and callsloop_stop(), whichjoin()s the very thread that is waiting on the loop.Fix
on_disconnectafter the refresh has landed.MQTT_INFO_REFRESH_TIMEOUT(30 s), swallows and logs failures, is deduplicated while in flight, and is cancelled bydisconnect(). A closed event loop is handled without leaking an un-awaited coroutine._mqtt_on_messagecaught onlyJSONDecodeError/KeyError; any other payload error would kill the thread the same way. It now decodes once and logs/drops on any exception.Verification
Reproduced end to end with a local MQTT broker stub, a real paho
loop_start()thread, and a cloud API mock that raisesDeyeCloudApiCannotConnectErrorduring the disconnect (same script,mainvs this branch):pre-commit,mypy,pylint: clean.pytest: 172 passed (7 new tests, run with-W error::RuntimeWarningto catch un-awaited coroutines).Note on ha-deye-dehumidifier#105
The HA-side attribution ("coordinator returns cache so it cannot detect the dead link") describes a symptom, not the cause. Returning cached data is by design for a
cloud_pushintegration; once the network thread survives, paho's keepalive/reconnect handles link loss andis_connected()is truthful again, soMqttDisconnectMonitorcovers prolonged outages. No integration change is needed beyond bumping to the libdeye release containing this fix.