pubsubclient: Guru Meditation Error: Core 1 panic'ed (LoadProhibited). Exception was unhandled.

Hi,

I am experiencing some kernel panics when using the pubsubclient library. The code is quite straightfoward, just connect to an active wifi client. I have attached a simplifed version below. The issue only happens when de ESP it’s not capable of connect to the MQTT server (when the IP is wrong and it’s being refused). Any suggestions what might be happening. I am wondering that maybe a memory issue somehow but I don’t know how to go deeper into it.

pubsubclient: 2.8.0

static WiFiClient _wifiClient;
static PubSubClient _mqttClient(_wifiClient);
_mqttClient.setServer(addr, port);
while (!_mqttClient.connected())
  {
    if ( _mqttClient.connect("SomeID"))                                <----- Here it breaks
    {
      Serial.println("MQTT connected");
      // Once connected, publish an announcement...
      _mqttClient.publish(_topic, String(clientId + " connected").c_str());
    }
    else
    {
      Serial.printf("MQTT failed, state %s, retrying...\n", _mqttClient.state());
      // Wait before retrying
      delay(2500);
    }
  }
}
Guru Meditation Error: Core  1 panic'ed (LoadProhibited). Exception was unhandled.
Core 1 register dump:
PC      : 0x400014e8  PS      : 0x00060830  A0      : 0x80103a64  A1      : 0x3ffb1a40  
A2      : 0xfffffffe  A3      : 0xfffffffc  A4      : 0x000000ff  A5      : 0x0000ff00  
A6      : 0x00ff0000  A7      : 0xff000000  A8      : 0x462d4100  A9      : 0x3ffb1da0  
A10     : 0x00000000  A11     : 0x00000036  A12     : 0x00001007  A13     : 0x3ffb1de4  
A14     : 0x3ffb1de8  A15     : 0x3ffc3a68  SAR     : 0x0000000a  EXCCAUSE: 0x0000001c  
EXCVADDR: 0xfffffffc  LBEG    : 0x400014fd  LEND    : 0x4000150d  LCOUNT  : 0xffffffff  

Backtrace: 0x400014e8:0x3ffb1a40 0x40103a61:0x3ffb1a50 0x4010afda:0x3ffb1d60 0x4010b016:0x3ffb1df0 0x400d5c7a:0x3ffb1e30 0x400d12e9:0x3ffb1ed0 0x400d13bb:0x3ffb1f30 0x400d1605:0x3ffb1f70 0x400d6343:0x3ffb1fb0 0x4008930d:0x3ffb1fd0

That after appliying the exception decoder results in:

PC: 0x400014e8
EXCVADDR: 0xfffffffc

Decoding stack results
0x40103a61: _svfprintf_r at ../../../.././newlib/libc/stdio/vfprintf.c line 1529
0x4010afda: _vsnprintf_r at ../../../.././newlib/libc/stdio/vsnprintf.c line 72
0x4010b016: vsnprintf at ../../../.././newlib/libc/stdio/vsnprintf.c line 41
0x400d5c7a: Print::printf(char const*, ...) at C:\Users\user\AppData\Local\Arduino15\packages\Heltec-esp32\hardware\esp32\0.0.5\cores\esp32\Print.cpp line 55
0x400d12e9: connectMQTT() at C:\Users\user\Desktop\LoraGateway\LoRaGateway/MQTT.ino line 70
0x400d13bb: connectToMQTTServer(IPAddress, unsigned short) at C:\Users\user\Desktop\LoraGateway\LoRaGateway/MQTT.ino line 38
0x400d1605: setup() at C:\Users\user\Desktop\LoraGateway\LoRaGateway/LoRaGateway.ino line 89
0x400d6343: loopTask(void*) at C:\Users\user\AppData\Local\Arduino15\packages\Heltec-esp32\hardware\esp32\0.0.5\cores\esp32\main.cpp line 14
0x4008930d: vPortTaskWrapper at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/freertos/port.c line 143

Cheers

About this issue

Most upvoted comments

I had this same issue. I solved it by making the WiFiClient variable global. -Damian

Same Issue calling connect function