Links2004 / arduinoWebSockets

arduinoWebSockets
GNU Lesser General Public License v2.1
1.89k stars 555 forks source link

ESP32 Restarting with websocket error if new client wants to connect. #587

Closed zekageri closed 2 years ago

zekageri commented 3 years ago

I'm using Ethernet with esp32

Here is the dump and the backtrace:

Guru Meditation Error: Core  0 panic'ed (LoadProhibited). Exception was unhandled.
Core 0 register dump:
PC      : 0x4019535b  PS      : 0x00060030  A0      : 0x800f7fb5  A1      : 0x3ffdf8a0
A2      : 0x3ffc1954  A3      : 0x00000000  A4      : 0x00000005  A5      : 0x3ffc1d80
A6      : 0x00000001  A7      : 0x000000cc  A8      : 0x800f7e8b  A9      : 0x3ffdf880
A10     : 0x00000001  A11     : 0x3ffc1cb4  A12     : 0x00000000  A13     : 0x3ffdf8a8  
A14     : 0x00000004  A15     : 0x3ffc53a8  SAR     : 0x0000001b  EXCCAUSE: 0x0000001c
EXCVADDR: 0x00000008  LBEG    : 0x4000c2e0  LEND    : 0x4000c2f6  LCOUNT  : 0xffffffff

Backtrace: 0x4019535b:0x3ffdf8a0 0x400f7fb2:0x3ffdf8c0 0x400f7fe9:0x3ffdf8e0 
0x400f8915:0x3ffdf900 0x400d3ebe:0x3ffdf920 0x4008acd9:0x3ffdf960
  #0  0x4019535b:0x3ffdf8a0 in WebSocketsServerCore::dropNativeClient(WSclient_t*) at lib\arduinoWebSockets-master\src/WebSocketsServer.cpp:518
  #1  0x400f7fb2:0x3ffdf8c0 in WebSocketsServerCore::handleNewClient(WiFiClient*) at lib\arduinoWebSockets-master\src/WebSocketsServer.cpp:137
  #2  0x400f7fe9:0x3ffdf8e0 in WebSocketsServer::handleNewClients() at lib\arduinoWebSockets-master\src/WebSocketsServer.cpp:137
  #3  0x400f8915:0x3ffdf900 in WebSocketsServer::loop() at lib\arduinoWebSockets-master\src/WebSocketsServer.cpp:137
  #4  0x400d3ebe:0x3ffdf920 in syncSocket_Task(void*) at lib/modbus-esp8266-master/src/Modbus.h:240
  #5  0x4008acd9:0x3ffdf960 in vPortTaskWrapper at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/freertos/port.c:355 (discriminator 1)

Rebooting...
ets Jun  8 2016 00:22:57

I'm using it within a task like this:

void setup(){
    Sync_Socket_Init(2048,0,2);
}

uint8_t lastClient;
void syncSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {

    switch(type) {
        case WStype_DISCONNECTED:{
                IPAddress client_IP = syncSocket.remoteIP(num);
                Serial.println("Client Disconnected with IP: " + client_IP.toString());
                //log_To_File("Client Disconnected with IP: " + client_IP.toString(), socketLOG); // Always shows 0.0.0.0  <O_o>
        }
            break;
        case WStype_CONNECTED:{
            lastClient = num;
            IPAddress client_IP = syncSocket.remoteIP(num);
            Serial.println("Client Connected with IP: " + client_IP.toString());
            //log_To_File("Client Connected with IP: " + client_IP.toString(), socketLOG);
        }
            break;
                case WStype_TEXT:
                case WStype_BIN:
        case WStype_ERROR:          
        case WStype_FRAGMENT_TEXT_START:
        case WStype_FRAGMENT_BIN_START:
        case WStype_FRAGMENT:
        case WStype_FRAGMENT_FIN:
        case WStype_PING:
        case WStype_PONG:
            break;
    }

}

static const inline void Send_Sync_All(String Message,String SpecChar){
    syncSocket.broadcastTXT(Message+SpecChar);
}
static const inline void Send_Sync_Last(String Message,String SpecChar){
    syncSocket.sendTXT(lastClient, Message+SpecChar);
}

static const inline void Sync_Socket_Init(int Stack_Depth,int Core,int Priority){
    xTaskCreatePinnedToCore(syncSocket_Task,"syncSocket_Task",Stack_Depth,NULL,Priority,&syncSocket_Task_Handle,Core);
}

void syncSocket_Task( void * parameter ){
    syncSocket.begin();
    syncSocket.onEvent(syncSocketEvent);
    for ever{
        syncSocket.loop();
        vTaskDelay(5);
    }
}

When a client is disconnecting for some reason it is detecting the disconect and tries to connect again after 3sec. When two or more clients are trying to connect ( sometimes even with one ) the esp spits this error and reboots.

I usually have 140kb free heap when that happens.

Sometimes i get an error like this:

Guru Meditation Error: Core  0 panic'ed (Unhandled debug exception)
Debug exception reason: Stack canary watchpoint triggered (syncSocket_Task)
Core 0 register dump:
PC      : 0x400851b4  PS      : 0x00060c36  A0      : 0x3ffe2860  A1      : 0x3ffe27a0
A2      : 0x3ffe2ffc  A3      : 0x3ffe2ba0  A4      : 0x3ffe2a8c  A5      : 0x00000010
A6      : 0x00000004  A7      : 0x3ffe2ac4  A8      : 0x5b007070  A9      : 0x00000000
A10     : 0x00000003  A11     : 0x00060023  A12     : 0x00060020  A13     : 0x00000001
A14     : 0x00060023  A15     : 0x00000000  SAR     : 0x0000000a  EXCCAUSE: 0x00000001
EXCVADDR: 0x00000000  LBEG    : 0x400014fd  LEND    : 0x4000150d  LCOUNT  : 0xfffffffc

Backtrace: 0x400851b4:0x3ffe27a0 0x3ffe285d:0x3ffe2890 0x40137496:0x3ffe2ba0 0x401374d2:0x3ffe2c30 0x400fd0b6:0x3ffe2c70 0x40160da9:0x3ffe2cd0 0x400f93dd:0x3ffe2d00 0x400f95d9:0x3ffe2d40 0x400f925a:0x3ffe2d60 0x400f92f5:0x3ffe2d90 0x400f9375:0x3ffe2db0 0x400f9592:0x3ffe2df0 0x400f95bb:0x3ffe2e30 0x400f8991:0x3ffe2e50 0x400f8a22:0x3ffe2e80 0x400f8a3e:0x3ffe2ea0 0x400d3f32:0x3ffe2ec0 0x4008fa29:0x3ffe2f00
  #0  0x400851b4:0x3ffe27a0 in _xt_lowint1 at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/freertos/xtensa_vectors.S:1118     
  #1  0x3ffe285d:0x3ffe2890 in ?? ??:0
  #2  0x40137496:0x3ffe2ba0 in _vsnprintf_r at /Users/ivan/e/newlib_xtensa-2.2.0-bin/newlib_xtensa-2.2.0/xtensa-esp32-elf/newlib/libc/stdio/../../../.././newlib/libc/stdio/vsnprintf.c:72
  #3  0x401374d2:0x3ffe2c30 in vsnprintf at /Users/ivan/e/newlib_xtensa-2.2.0-bin/newlib_xtensa-2.2.0/xtensa-esp32-elf/newlib/libc/stdio/../../../.././newlib/libc/stdio/vsnprintf.c:41
  #4  0x400fd0b6:0x3ffe2c70 in log_printf at C:\Users\Dr.Random\.platformio\packages\framework-arduinoespressif32\cores\esp32/esp32-hal-uart.c:480
  #5  0x40160da9:0x3ffe2cd0 in WiFiClient::connected() at C:\Users\Dr.Random\.platformio\packages\framework-arduinoespressif32\libraries\WiFi\src/WiFiClient.cpp:564      
  #6  0x400f93dd:0x3ffe2d00 in WebSockets::handleWebsocketCb(WSclient_t*) at lib\arduinoWebSockets-master\src/WebSockets.cpp:380 (discriminator 2)
  #7  0x400f95d9:0x3ffe2d40 in std::_Function_handler<void (WSclient_t*, bool), std::_Bind<WebSockets::handleWebsocketWaitFor(WSclient_t*, unsigned int)::{lambda(WebSockets*, unsigned int, WSclient_t*, bool)#1} (WebSockets*, unsigned int, std::_Placeholder<1>, WebSockets::handleWebsocketWaitFor(WSclient_t*, unsigned int)::{lambda(WebSockets*, unsigned int, WSclient_t*, bool)#1}<2>)> >::_M_invoke(std::_Any_data const&, WSclient_t*&&, bool&&) at lib\arduinoWebSockets-master\src/WebSockets.cpp:367
      (inlined by) __call<void, WSclient_t*&&, bool&&, 0u, 1u, 2u, 3u> at c:\users\dr.random\.platformio\packages\toolchain-xtensa32\xtensa-esp32-elf\include\c++\5.2.0/functional:1074
      (inlined by) operator()<WSclient_t*, bool, void> at c:\users\dr.random\.platformio\packages\toolchain-xtensa32\xtensa-esp32-elf\include\c++\5.2.0/functional:1133   
      (inlined by) _M_invoke at c:\users\dr.random\.platformio\packages\toolchain-xtensa32\xtensa-esp32-elf\include\c++\5.2.0/functional:1871
  #8  0x400f925a:0x3ffe2d60 in std::function<void (WSclient_t*, bool)>::operator()(WSclient_t*, bool) const at c:\users\dr.random\.platformio\packages\toolchain-xtensa32\xtensa-esp32-elf\include\c++\5.2.0/functional:2271
  #9  0x400f92f5:0x3ffe2d90 in WebSockets::readCb(WSclient_t*, unsigned char*, unsigned int, std::function<void (WSclient_t*, bool)>) at lib\arduinoWebSockets-master\src/WebSockets.cpp:652
  #10 0x400f9375:0x3ffe2db0 in WebSockets::handleWebsocketWaitFor(WSclient_t*, unsigned int) at lib\arduinoWebSockets-master\src/WebSockets.cpp:375
  #11 0x400f9592:0x3ffe2df0 in WebSockets::handleWebsocketCb(WSclient_t*) at lib\arduinoWebSockets-master\src/WebSockets.cpp:391
  #12 0x400f95bb:0x3ffe2e30 in WebSockets::handleWebsocket(WSclient_t*) at lib\arduinoWebSockets-master\src/WebSockets.cpp:339
  #13 0x400f8991:0x3ffe2e50 in WebSocketsServerCore::handleClientData() at lib\arduinoWebSockets-master\src/WebSocketsServer.cpp:137
  #14 0x400f8a22:0x3ffe2e80 in WebSocketsServerCore::loop() at lib\arduinoWebSockets-master\src/WebSocketsServer.cpp:137
  #15 0x400f8a3e:0x3ffe2ea0 in WebSocketsServer::loop() at lib\arduinoWebSockets-master\src/WebSocketsServer.cpp:137
  #16 0x400d3f32:0x3ffe2ec0 in syncSocket_Task(void*) at lib/modbus-esp8266-master/src/Modbus.h:240
  #17 0x4008fa29:0x3ffe2f00 in vPortTaskWrapper at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/freertos/port.c:355 (discriminator 1)

Rebooting...
ets Jun  8 2016 00:22:57
Links2004 commented 3 years ago

looks like a problem inside the ESP32 core WiFiClient::connected() connect function.

WiFiClient::connected() at C:\Users\Dr.Random\.platformio\packages\framework-arduinoespressif32\libraries\WiFi\src/WiFiClient.cpp:564  

what is you exact version of the framework-arduinoespressif32 ?

the current upstream repo has a empty line at line 564 https://github.com/espressif/arduino-esp32/blob/master/libraries/WiFi/src/WiFiClient.cpp#L564

one think to check is that the tasks have only a limited stack size (not related to free heap) may the code hits this limit, simply try to change the 2k to 4k for a test?

Note: the setup / loop of the Arduino runs with a 8k stack.

zekageri commented 3 years ago

Tried to increase the task's stack space. Unfortunatelly that did not work. I'm trying with async version since i don't need client socket.

Links2004 commented 3 years ago

does the problem only happen with the websocket code running in its own task? or will it happen too with it the main loop.

can you reproduce this with example code or the lib?

zekageri commented 3 years ago

I will try it as soon as possible. Thank you for your help