Skip to content

Commit 45fc796

Browse files
committed
Fix timeout in WebServer::_uploadReadByte and set timeout handleClient()
Fixes: #9990
1 parent 4e3523c commit 45fc796

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

Diff for: libraries/WebServer/src/Parsing.cpp

+33-5
Original file line numberDiff line numberDiff line change
@@ -345,13 +345,41 @@ void WebServer::_uploadWriteByte(uint8_t b) {
345345

346346
int WebServer::_uploadReadByte(NetworkClient &client) {
347347
int res = client.read();
348-
348+
349349
if (res < 0) {
350-
while (!client.available() && client.connected()) {
351-
delay(2);
352-
}
350+
// keep trying until you either read a valid byte or timeout
351+
const unsigned long startMillis = millis();
352+
const long timeoutIntervalMillis = client.getTimeout();
353+
bool timedOut = false;
354+
for(;;) {
355+
if (!client.connected()) return -1;
356+
// loosely modeled after blinkWithoutDelay pattern
357+
while(!timedOut && !client.available() && client.connected()){
358+
delay(2);
359+
timedOut = (millis() - startMillis) >= timeoutIntervalMillis;
360+
}
353361

354-
res = client.read();
362+
res = client.read();
363+
if(res >= 0) {
364+
return res; // exit on a valid read
365+
}
366+
// NOTE: it is possible to get here and have all of the following
367+
// assertions hold true
368+
//
369+
// -- client.available() > 0
370+
// -- client.connected == true
371+
// -- res == -1
372+
//
373+
// a simple retry strategy overcomes this which is to say the
374+
// assertion is not permanent, but the reason that this works
375+
// is elusive, and possibly indicative of a more subtle underlying
376+
// issue
377+
378+
timedOut = (millis() - startMillis) >= timeoutIntervalMillis;
379+
if (timedOut) {
380+
return res; // exit on a timeout
381+
}
382+
}
355383
}
356384

357385
return res;

Diff for: libraries/WebServer/src/WebServer.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -432,10 +432,8 @@ void WebServer::handleClient() {
432432
case HC_WAIT_READ:
433433
// Wait for data from client to become available
434434
if (_currentClient.available()) {
435+
_currentClient.setTimeout(HTTP_MAX_SEND_WAIT); /* / 1000 removed, WifiClient setTimeout changed to ms */
435436
if (_parseRequest(_currentClient)) {
436-
// because HTTP_MAX_SEND_WAIT is expressed in milliseconds,
437-
// it must be divided by 1000
438-
_currentClient.setTimeout(HTTP_MAX_SEND_WAIT); /* / 1000 removed, WifiClient setTimeout changed to ms */
439437
_contentLength = CONTENT_LENGTH_NOT_SET;
440438
_handleRequest();
441439

0 commit comments

Comments
 (0)