← index #1073PR #812
Related · high · value 2.225
QUERY · ISSUE

umqtt.simple/umqtt/simple.py MQTTClient.publish() breaks on unicode

openby tpbriscoopened 2025-12-22updated 2026-03-09
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.

2 comments
europrimus · 2026-03-09

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;

[proof_of_bug] Send message "ascii message" (13 chars).
[proof_of_bug] Send message "utf8 message éêè²³" (18 chars).
[proof_of_bug] Send message "ascii message" (13 chars).
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "proof_of_bug.py", line 37, in main
  File "umqtt/simple.py", line 148, in publish
  File "umqtt/simple.py", line 201, in wait_msg
OSError: -1

log of mosquitto (version 2.1.2) mqtt brocker:

2026-03-09T20:49:51: New client connected from 192.168.1.13:59631 as micropython_umqtt.simple_bug (p4, c1, k0, u'unit_test').
2026-03-09T20:49:51: Client micropython_umqtt.simple_bug [192.168.1.13:59631] disconnected: malformed packet.

what I understand:

  • 1st message work
  • 2nd message, with utf8, not fully send or with error
  • 3rd message lead server to close connection because of malformed packet.
    Maybe the publishing of 2nd message not closed/ended ?
Josverl · 2026-03-09

@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.

CANDIDATE · PULL REQUEST

umqtt.simple: Fix MQTTClient publish for connection disruption.

openby khrystynaOopened 2024-02-28updated 2024-02-28

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.

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied