alexCajas / EmbeddedMqttBroker

This is a Mqtt broker for embedded devices, developed in C++, FreeRTOS to use advanced multitasking capabilities, and arduino core. Tested in an Esp32 and esp8266.
https://github.com/alexCajas/EmbeddedMqttBroker
MIT License
77 stars 15 forks source link

Guru Meditation in ReaderMqttPacket::bytesToString when there is some traffic load on the Broaker #6

Open diodi opened 1 year ago

diodi commented 1 year ago

I modified httpServerAndMqttBroker.ino to operate as as WIFI_AP and to allow up to 8 clients.

#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiAP.h>
#include "EmbeddedMqttBroker.h"
...

void setup(){

  Serial.begin(115200);
  Serial.println();
  Serial.println("Configuring access point...");

  WiFi.mode(WIFI_AP);  
  WiFi.softAP(ssid, password);
  delay(500);
  IPAddress myIP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.println(myIP);

  // Start http server
  server.begin();
...

  // Start the mqtt broker
  broker.setMaxNumClients(8); // set according to your system.
  broker.startBroker();
  Serial.println("broker started"

Connecting two clients and generating some trafic I run into an unhandled exception.

publishing: moba/ToESP
enviando publish
enviando publish
publishing: moba/ToESP
enviando publish
enviando publish
no action
no action
no action
no action
Guru Meditation Error: Core  1 panic'ed (StoreProhibited). Exception was unhandled.

The memcopy operation

int ReaderMqttPacket::bytesToString(int index, size_t textFieldLengt,String*textField){
    // reserving memory to copy the mqtt text field.
    char* aux = (char*)malloc(textFieldLengt+1);

    // memcpy is necesary because reader object will be deleted.
    memcpy(aux,&remainingPacket[index],textFieldLengt);

leads to the exception.

Please let me know if you need more. Thank you!

alexCajas commented 1 year ago

ok! it is a important issue, I will fix it.

alexCajas commented 1 year ago

Hi again, @diodi , can you give information about the clients please? Desktops clients, esp clients maybe?, Is the traffic only two publish?

diodi commented 1 year ago

Two esp clients are active. Both publish serialized JsonDocuments (ArduinoJson.h) to several sub-topics within the same main topic using PubSubClientStatic. Both clients subscribe to the main topic to receive all published information. Thank you for taking a look.

rolliracker commented 1 year ago

I get the same error as described by @diodi. But I also get it without running an Access Point. I use the simpleMqttBroker example with WiFi.mode(WIFI_STA); and with only one client that publishes two topics every 10 seconds (Tasmota on an ESP8266).

When I print out the free memory with ESP.getFreeHeap() I see decreasing free memory after each publication event.

The time until the exception occurs varies, but often is only a couple of minutes. I tried version 1.0.5 and 1.0.3 with basically the same result

lutorm commented 1 month ago

I wonder if I'm seeing the same problem. I'm running SimpleMqqtBroker in STA mode, with 2 clients publishing and 2 subscribing. After about 10 minutes, the broker repeatably drops off the network. There is nothing printed on the serial when this happens, do you have to do something to enable the core panic message above?

Edit: my issue seems different so I made another issue: https://github.com/alexCajas/EmbeddedMqttBroker/issues/22