Skip to content

Commit

Permalink
Reimplemented flush in WiFiClient
Browse files Browse the repository at this point in the history
  • Loading branch information
PilnyTomas committed Aug 18, 2023
1 parent 099b432 commit 4c4a6b8
Showing 1 changed file with 9 additions and 20 deletions.
29 changes: 9 additions & 20 deletions libraries/WiFi/src/WiFiClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,13 @@ class WiFiClientRxBuffer {
size_t available(){
return _fill - _pos + r_available();
}

void flush(){
if(r_available()){
fillBuffer();
}
_pos = _fill;
}
};

class WiFiClientSocketHandle {
Expand Down Expand Up @@ -501,26 +508,8 @@ int WiFiClient::available()
// Though flushing means to send all pending data,
// seems that in Arduino it also means to clear RX
void WiFiClient::flush() {
int res;
size_t a = available(), toRead = 0;
if(!a){
return;//nothing to flush
}
uint8_t * buf = (uint8_t *)malloc(WIFI_CLIENT_FLUSH_BUFFER_SIZE);
if(!buf){
return;//memory error
}
while(a){
toRead = (a>WIFI_CLIENT_FLUSH_BUFFER_SIZE)?WIFI_CLIENT_FLUSH_BUFFER_SIZE:a;
res = recv(fd(), buf, toRead, MSG_DONTWAIT);
if(res < 0) {
log_e("fail on fd %d, errno: %d, \"%s\"", fd(), errno, strerror(errno));
stop();
break;
}
a -= res;
}
free(buf);
#ifdef ARDUINO_DEBUG_LEVEL
_rxBuffer->flush();
}

uint8_t WiFiClient::connected()
Expand Down

1 comment on commit 4c4a6b8

@fredlcore
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change leads to a Guru meditation due to Load Prohibited when calling client.flush() on a WiFiClient instance:

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

Core  1 register dump:
PC      : 0x400e3847  PS      : 0x00060f30  A0      : 0x800e3950  A1      : 0x3ffb1f50  
A2      : 0x00000000  A3      : 0x3ffbfd38  A4      : 0x00000384  A5      : 0x00000001  
A6      : 0x000000ff  A7      : 0x3f4059d4  A8      : 0x8008fccc  A9      : 0x3ffb1f40  
A10     : 0x00000001  A11     : 0x0002710d  A12     : 0x00027385  A13     : 0x00060f23  
A14     : 0x00060f20  A15     : 0x00000001  SAR     : 0x0000000c  EXCCAUSE: 0x0000001c  
EXCVADDR: 0x00000010  LBEG    : 0x4008ad85  LEND    : 0x4008ad95  LCOUNT  : 0xfffffff5  
Backtrace: 0x400e3844:0x3ffb1f50 0x400e394d:0x3ffb1f80 0x400dc1fd:0x3ffb1fa0 0x400ee93d:0x3ffb2290
ELF file SHA256: 4726c43adf41a5ad

ESP Stack Trace decoder gives me this:

PC: 0x400e387f: WiFiClientRxBuffer::r_available() at /Users/frederik/Library/Arduino15/packages/esp32/hardware/esp32/2.0.13/libraries/WiFi/src/WiFiClient.cpp line 46
EXCVADDR: 0x00000010

Decoding stack results
0x400e387c: WiFiClientRxBuffer::r_available() at /Users/frederik/Library/Arduino15/packages/esp32/hardware/esp32/2.0.13/libraries/WiFi/src/WiFiClient.cpp line 44
0x400e3985: WiFiClient::flush() at /Users/frederik/Library/Arduino15/packages/esp32/hardware/esp32/2.0.13/libraries/WiFi/src/WiFiClient.cpp line 158
0x400df411: loop() at /Users/frederik/Downloads/BSB-LAN-master/BSB_LAN/BSB_LAN.ino line 7073
0x400ee1f5: loopTask(void*) at /Users/frederik/Library/Arduino15/packages/esp32/hardware/esp32/2.0.13/cores/esp32/main.cpp line 50

The trace narrows the culprit down to line 46:

if(_fd < 0){

If I do a Serial.println(_fd); right before that, the ESP32 crashes.
It seems not to happen all the time, but the reason either seems to be if there are still characters in the incoming client buffer (for lack of a better word) or if there are no more characters coming in.
This problem only occurs in 2.0.12 and 2.0.13, once I revert back to 2.0.11 the problem is gone.
Can you please investigate this further and if possible revert to the state from 2.0.11?
Thank you!

Please sign in to comment.