Skip to content

Commit 3c19f99

Browse files
committed
AsyncAbstractResponse: Use packet buffer space logic on ESP32
When under memory pressure, the ESP32 TCP stack can also stop operating, resulting in hung requests. Use the logic developed for ESP8266 to avoid allocating buffers too large for the system to actually send. Fixes stalls on low-RAM ESP32 devices, such as the S2.
1 parent 870fffc commit 3c19f99

1 file changed

Lines changed: 5 additions & 7 deletions

File tree

src/WebResponses.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
#else
3434
#define GET_MAX_BLOCK_SIZE getMaxAllocHeap
3535
#endif
36+
// When looking up available memory, leave some slack
37+
#define BLOCK_SIZE_SLACK 128
3638

3739
// Since ESP8266 does not link memchr by default, here's its implementation.
3840
void* memchr(void* ptr, int ch, size_t count)
@@ -358,29 +360,25 @@ size_t AsyncAbstractResponse::_ack(AsyncWebServerRequest *request, size_t len, u
358360
} else {
359361
outLen = std::min(space, _contentLength - _sentLength);
360362
}
361-
#ifdef ESP8266
363+
362364
// Limit outlen based on available memory
363365
// We require two packet buffers - one allocated here, and one belonging to the TCP stack
364366
{
365367
auto old_space = _packet.capacity();
366-
auto max_block_size = ESP.getMaxFreeBlockSize() - 128;
368+
auto max_block_size = ESP.GET_MAX_BLOCK_SIZE() - BLOCK_SIZE_SLACK;
367369
if ((old_space < outLen) || (outLen > max_block_size)) {
368370
DEBUG_PRINTFP("(%d) Space adjustment, have %d, want %d, avail %d\n", (intptr_t)this, old_space, outLen, max_block_size);
369371
do {
370372
dealloc_vector(_packet);
371373
outLen = std::min(outLen, max_block_size);
372374
_packet.reallocate(outLen);
373-
max_block_size = ESP.getMaxFreeBlockSize() - 128;
375+
max_block_size = ESP.GET_MAX_BLOCK_SIZE() - BLOCK_SIZE_SLACK;
374376
DEBUG_PRINTFP("(%d) Checking %d vs %d\n", (intptr_t)this, outLen, max_block_size);
375377
} while (max_block_size < outLen);
376378
} else {
377379
_packet.reallocate(outLen);
378380
}
379381
}
380-
#else
381-
//Serial.printf("[%u] %d/%d -> %d\n", (unsigned) millis(), ESP.getMaxAllocHeap(), ESP.getFreeHeap(), outLen);
382-
_packet.reallocate(outLen);
383-
#endif
384382

385383
if(_chunked){
386384
// HTTP 1.1 allows leading zeros in chunk length. Or spaces may be added.

0 commit comments

Comments
 (0)