Assert failed: xMBMasterRTUReceiveFSM mbrtu_m.c:235 with Modbus Master from ESP IDF

After long investigations I found out, that it helped to increase the stack from the count_task to prevent this problem in the mb_master task.

In general, the count_task had about 160 bytes (ESP IDF) left according to the high watermark function and for that reason I was not worried to not have enough stack configured for it.

Before:

  xTaskCreate(&nvs_main, "nvs_main", 2000, NULL, 9, &nvs_main_th);
  xTaskCreate(&server_task, "server_task", 2000, NULL, 9, &server_task_th);
  xTaskCreate(&server_handle_task, "server_handle_task", 4000, NULL, 6, &server_handle_task_th);
  xTaskCreate(&count_task, "count_task", 2000, NULL, 2, &count_task_th);
  xTaskCreate(&mb_master_main, "mb_master_main", 3500, NULL, 9, &mb_master_main_th);
  xTaskCreate(&mb_slave_main, "mb_slave_main", 3500, NULL, 9, &mb_slave_main_th);
  xTaskCreate(&mqtt_ssl_main, "mqtt_ssl_main", 1024 * 8, NULL, 9, &mqtt_ssl_main_th);

I (109262) count_task: RAM left 126468, nvs_main_th: 392, server_task_th: 304, server_handle_task_th: 2304, count_task_th: 168, mb_master_main_th: 1512, mb_slave_main_th: 1388, mqtt_ssl_main_th: 6364

After (did increase nvs_main and server_task as well, just to make sure):

  xTaskCreate(&nvs_main, "nvs_main", 2500, NULL, 9, &nvs_main_th);
  xTaskCreate(&server_task, "server_task", 2500, NULL, 9, &server_task_th);
  xTaskCreate(&server_handle_task, "server_handle_task", 4000, NULL, 6, &server_handle_task_th);
  xTaskCreate(&count_task, "count_task", 2500, NULL, 2, &count_task_th);
  xTaskCreate(&mb_master_main, "mb_master_main", 3500, NULL, 9, &mb_master_main_th);
  xTaskCreate(&mb_slave_main, "mb_slave_main", 3500, NULL, 9, &mb_slave_main_th);
  xTaskCreate(&mqtt_ssl_main, "mqtt_ssl_main", 1024 * 8, NULL, 9, &mqtt_ssl_main_th);

I (271792) count_task: RAM left 125352, nvs_main_th: 888, server_task_th: 812, server_handle_task_th: 2392, count_task_th: 672, mb_master_main_th: 1428, mb_slave_main_th: 1480, mqtt_ssl_main_th: 6284

The counter_task is not very complex and its stack usage will proably not change very much:

static void count_task(void *pvParameters)
{
  const static char *TAG = "count_task";
  char out[100];
  int len;
  int clients;
  uint16_t n = 0;
  const int DELAY = 3000 / portTICK_PERIOD_MS; // 3 second

  ESP_LOGI(TAG, "starting task");
  for (;;)
  {
    len = sprintf(out, "Power: %.1f <br>Correction: %.1f <br>Fake Output: %.1f", PowerReal, PowerCorrection, PowerFake);
    clients = ws_server_send_text_all(out, len);

    ESP_LOGI(TAG,
      "RAM left %d, \
      nvs_main_th: %d, \
      server_task_th: %d, \
      server_handle_task_th: %d, \
      count_task_th: %d, \
      mb_master_main_th: %d, \
      mb_slave_main_th: %d, \
      mqtt_ssl_main_th: %d",
      esp_get_free_heap_size(),
      uxTaskGetStackHighWaterMark(nvs_main_th),
      uxTaskGetStackHighWaterMark(server_task_th),
      uxTaskGetStackHighWaterMark(server_handle_task_th),
      uxTaskGetStackHighWaterMark(count_task_th),
      uxTaskGetStackHighWaterMark(mb_master_main_th),
      uxTaskGetStackHighWaterMark(mb_slave_main_th),
      uxTaskGetStackHighWaterMark(mqtt_ssl_main_th)
      );

    if (clients > 0)
    {
      // ESP_LOGI(TAG,"sent: \"%s\" to %i clients",out,clients);
    }
    n++;
    vTaskDelay(DELAY);
  }
}

How can the stack size of the counter_task influence the mb_master task, especially when there were some bytes left according to the high watermark function?