Skip to content

Intermittent MQTTS Connection Failures (IDFGH-13979) #288

@vicatcu

Description

@vicatcu

Checklist

  • Checked the issue tracker for similar issues to ensure this is not a duplicate
  • Read the documentation to confirm the issue is not addressed there and your configuration is set correctly
  • Tested with the latest version to ensure the issue hasn't been fixed

How often does this bug occurs?

rarely

Expected behavior

MQTTS connection should consistently work

Actual behavior (suspected bug)

When connecting to my MQTT Server (self-hosted, Mosquitto), I intermittently experience the failures to connect with MQTTS.

Error logs or terminal output

When it fails to connect, I get the following output from my code:


E (15049) mqtt_client: esp_mqtt_handle_transport_read_error: transport_read(): EOF
E (15050) mqtt_client: esp_mqtt_handle_transport_read_error: transport_read() error: errno=119
[MQTT EVENT] reported from esp-tls (0x8008)
[MQTT EVENT] Last errno string (Success)
E (15065) mqtt_client: esp_mqtt_connect: mqtt_message_receive() returned -2
E (15068) mqtt_client: MQTT connect failed

Steps to reproduce the behavior

My code effectively looks like this:

  static esp_mqtt_client_config_t mqtt_cfg;
  mqtt_cfg.broker.address.hostname = settings->server;
  mqtt_cfg.broker.address.transport = MQTT_TRANSPORT_OVER_SSL;
  mqtt_cfg.broker.address.port = 8883;
  mqtt_cfg.credentials.username = settings->username;
  mqtt_cfg.credentials.client_id = settings->identity;
  mqtt_cfg.credentials.authentication.password = settings->password;
  mqtt_cfg.broker.verification.certificate = root_ca;
  esp_mqtt_client_handle_t _client = esp_mqtt_client_init(&mqtt_cfg);
  esp_mqtt_client_register_event(_client, (esp_mqtt_event_id_t) ESP_EVENT_ANY_ID, mqtt_event_handler, NULL);
  const esp_err_t ret = esp_mqtt_client_start(_client);
  boolean connected = false;
  if (ESP_OK == ret) {
      connected = waitForMqttConnected();
      // polls for global _mqttIsConnected global variable set in mqtt event handler to become true
      // case MQTT_EVENT_CONNECTED sets it to true
      // case MQTT_EVENT_DISCONNECTED sets it to false
      // case MQTT_EVENT_ERROR sets it to false 
      // or times out after 15 seconds
  }

  ... event handler code that is printing out the [MQTT EVENT] log messages looks like this:
    case MQTT_EVENT_ERROR:
      lastMqttEventId = MQTT_EVENT_ERROR;
      _mqttIsError = true;
      _mqttIsConnected = false;
      esp_tls_stack_err = 0;
      if (event->error_handle->error_type == MQTT_ERROR_TYPE_TCP_TRANSPORT) {
        if (event->error_handle->esp_tls_last_esp_err  != 0) {
          Serial.printf("[MQTT EVENT] reported from esp-tls (0x%x)\r\n", event->error_handle->esp_tls_last_esp_err);
        }
        if (event->error_handle->esp_tls_stack_err != 0) {
          esp_tls_stack_err = event->error_handle->esp_tls_stack_err;
          Serial.printf("[MQTT EVENT] reported from tls stack (0x%x)\r\n", event->error_handle->esp_tls_stack_err);
        }
        if (event->error_handle->esp_transport_sock_errno != 0) {
          Serial.printf("[MQTT EVENT] captured as transport's socket errno (0x%x)\r\n", event->error_handle->esp_transport_sock_errno);
        }
        Serial.printf("[MQTT EVENT] Last errno string (%s)\r\n", strerror(event->error_handle->esp_transport_sock_errno));
      }
      break;
   ...

Project release version

idf-release_v5.1-632e0c2a

System architecture

Intel/AMD 64-bit (modern PC, older Mac)

Operating system

Linux

Operating system version

Ubuntu 22.04

Shell

Bash

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions