From 119b3af889d2f1ff716854e7b5c76ca7b1d78a3d Mon Sep 17 00:00:00 2001 From: EdouardMalot Date: Fri, 26 Jun 2026 20:55:57 +0200 Subject: [PATCH] Fix MQTT client: reset use_tls flag in connection_end (TLS to plain reconnect) _nxd_mqtt_client_secure_connect sets nxd_mqtt_client_use_tls = 1 before delegating to _nxd_mqtt_client_connect, but the flag is never cleared afterwards. A subsequent non-secure connect on the same client inherits the stale flag, triggers nx_secure_tls_session_start on a plain TCP socket, and returns NXD_MQTT_CONNECT_FAILURE (0x10005). Clear the flag in _nxd_mqtt_client_connection_end, after the TCP socket is disconnected and unbound: while the socket can still deliver data, a late TLS record (e.g. the peer's close_notify) must keep failing through the TLS receive path rather than being parsed as plaintext MQTT bytes. Clearing the flag before the socket teardown would let such a record be misread as a partial MQTT message and parked forever in nxd_mqtt_client_processing_packet, leaking a packet (caught by netx_mqtt_packet_leak_test). Co-Authored-By: Claude Fable 5 --- addons/mqtt/nxd_mqtt_client.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/addons/mqtt/nxd_mqtt_client.c b/addons/mqtt/nxd_mqtt_client.c index 3f8e85677..badefcfd6 100644 --- a/addons/mqtt/nxd_mqtt_client.c +++ b/addons/mqtt/nxd_mqtt_client.c @@ -2571,6 +2571,10 @@ VOID _nxd_mqtt_client_connection_end(NXD_MQTT_CLIENT *client_ptr, ULONG wait_opt nx_tcp_socket_disconnect(&(client_ptr -> nxd_mqtt_client_socket), wait_option); nx_tcp_client_socket_unbind(&(client_ptr -> nxd_mqtt_client_socket)); +#ifdef NX_SECURE_ENABLE + client_ptr -> nxd_mqtt_client_use_tls = 0; +#endif + /* Disable timer if timer has been started. */ if (client_ptr -> nxd_mqtt_keepalive) {