fix: drop non-UTF-8 cloud mips message instead of crashing mqtt loop#1750
Open
Heartcoolman wants to merge 1 commit into
Open
fix: drop non-UTF-8 cloud mips message instead of crashing mqtt loop#1750Heartcoolman wants to merge 1 commit into
Heartcoolman wants to merge 1 commit into
Conversation
MipsCloudClient._on_mips_message decodes the push payload with a bare
payload.decode('utf-8'). Some cloud pushes carry payloads that are not
valid UTF-8, which raises UnicodeDecodeError inside paho's on_message,
aborts the read loop and silently drops the message. Lost device
online/offline pushes can leave a device stuck unavailable until the
config entry is reloaded manually.
Catch UnicodeDecodeError, log the topic and the payload hex for
diagnosis, and skip the message so that one malformed push cannot
break message processing.
Related to XiaoMi#1749
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Problem
Fixes #1749.
MipsCloudClient._on_mips_messagedecodes every cloud push with a barepayload.decode('utf-8')(miot_mips.py:1104). Some cloud pushes carry payloads that are not valid UTF-8 — on my instance this fired 19 times in 8 days (offending byte always around position 162–190, e.g.0xaa,0x80,0xee), each time raisingUnicodeDecodeErrorinside paho'son_message:The exception aborts paho's read loop (
__mqtt_loop_handlerre-raises) and the message is lost. When a lost push is a device online/offline state notification, the device's entities stayunavailablein HA indefinitely — in my case alinp.light.lx2bcwwas stuck for ~17 hours while working fine in the Mi Home app, and reloading the config entry restored it instantly. Full logs in #1749.Change
Catch
UnicodeDecodeErrorinMipsCloudClient._on_mips_message, log the topic and the payload hex (so the actual encoding of these messages can be diagnosed from user reports), and skip the message. One malformed push can no longer abort MQTT message processing.The failing byte offsets (160+ bytes decode fine, then an invalid byte) suggest mostly-JSON text with an embedded non-UTF-8 sequence rather than a packed/TLV message, so no alternative decode path is attempted — the message is dropped defensively.
Testing
_on_mips_message(duck-typed client):log_error, handler not called;pylint --rcfile=.pylintrcon the file: 10.00/10 (same as before the change).test/test_mips.pyrequires live cloud credentials/gateway, not run.