-
Notifications
You must be signed in to change notification settings - Fork 298
Description
When using MQTT-C with MQTT_USE_MBEDTLS, the application crashes if the broker shuts down unexpectedly and mqtt_sync() tries to publish or process queued data.
The crash occurs because mbedtls_ssl_write (inside mqtt_pal_sendall) is still invoked after the broker has already closed the TLS session. In this case, the underlying TCP socket may remain open, but the TLS context is no longer valid. Since the error is not properly propagated back to the MQTT client, the library attempts to continue using the invalid TLS session, which eventually leads to a crash instead of triggering a reconnect.
When the broker closes the TLS session, mqtt_pal_recvall already sets
error = MQTT_ERROR_SOCKET_ERROR;
However, this error is not propagated back to the caller. Instead, mqtt_recv continues, which leaves the client in an invalid state.
A safer approach is to return MQTT_ERROR_SOCKET_ERROR immediately from mqtt_recv whenever this condition is detected. This ensures that mqtt_sync can see the error and trigger the reconnect logic instead of continuing with a broken TLS context.