umqtt.simple/umqtt/simple.py MQTTClient.publish() breaks on unicode
I've been looking over this for a couple of weeks, and I'm struggling to find the underlying issue.
On a live MQTT session (I'm running from a RP2040 to a Mosquitto server), a mq.publish with a unicode character will break the session, eventually resulting in a Errno 104 / ECONNRESET (presumably when the input buffer is full).
The more "obvious" way to observe it is to do a mosquitto_sub on the topic, and the publish method stops transmitting data.
To observe:
mosquitto_sub -h yourserver -t homeassistant/sensor/bedroom/# -v
From a micropython:
mq.publish('homeassistant/sensor/bedroom/mytest', 'F') # works fine
mq.publish('homeassistant/sensor/bedroom/mytest' '°F') # ^@ followed by F, appears to work
mq.publish('homeassistant/sensor/bedroom/mytest', 'F') # no data is received
subsequent/enough further publish() calls will result in the ECONNRESET
Testing against mosquitto_pub with the same topic and unicode message indicate it all works.
Somehow I think the length calculation is thrown off by the string-vs-bytes-vs-unicode thing, but haven't been able to nail it down yet.
umqtt.simple: Fix MQTTClient publish for connection disruption.
Method now handles OSError: -1 after many packets, attempting reconnection.
Issue #754
Modified the MQTTClient publish method to gracefully handle connection disruptions. Previously, the method encountered an OSError: -1 after sending a large number of packets, leading to a disconnection from the server. The updated method now includes error handling to detect this situation and attempts to reconnect before retrying the operation. This enhancement ensures that the client can recover from connection disruptions seamlessly, maintaining stable communication with the server.
I confirm this bug.
I make a little script for test it
I run it on an ESP32 with MicroPython v1.27.0 on 2025-12-09;
log of mosquitto (version 2.1.2) mqtt brocker:
what I understand:
Maybe the publishing of 2nd message not closed/ended ?
@europrimus ,
Thank you for confirming, and providing a script to reproduce the issue.
I will have a PR open that addresses a number of unicode issues in MicroPython
It would be worthwhile checking if that resolves this issue as well.