Skip to content

Commit e4b008e

Browse files
authored
Handle stream timeouts properly, for slow HTTP/HTTPS links (#3752)
This patch fixes update timeouts (error #6) on slow HTTP/HTTPS links.
1 parent 76afaf2 commit e4b008e

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

Diff for: libraries/Update/src/Updater.cpp

+18-7
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,8 @@ size_t UpdateClass::write(uint8_t *data, size_t len) {
323323
size_t UpdateClass::writeStream(Stream &data) {
324324
size_t written = 0;
325325
size_t toRead = 0;
326+
int timeout_failures = 0;
327+
326328
if(hasError() || !isRunning())
327329
return 0;
328330

@@ -344,15 +346,24 @@ size_t UpdateClass::writeStream(Stream &data) {
344346
bytesToRead = remaining();
345347
}
346348

347-
toRead = data.readBytes(_buffer + _bufferLen, bytesToRead);
348-
if(toRead == 0) { //Timeout
349-
delay(100);
350-
toRead = data.readBytes(_buffer + _bufferLen, bytesToRead);
351-
if(toRead == 0) { //Timeout
352-
_abort(UPDATE_ERROR_STREAM);
353-
return written;
349+
/*
350+
Init read&timeout counters and try to read, if read failed, increase counter,
351+
wait 100ms and try to read again. If counter > 300 (30 sec), give up/abort
352+
*/
353+
toRead = 0;
354+
timeout_failures = 0;
355+
while(!toRead) {
356+
toRead = data.readBytes(_buffer + _bufferLen, bytesToRead);
357+
if(toRead == 0) {
358+
timeout_failures++;
359+
if (timeout_failures >= 300) {
360+
_abort(UPDATE_ERROR_STREAM);
361+
return written;
362+
}
363+
delay(100);
354364
}
355365
}
366+
356367
if(_ledPin != -1) {
357368
digitalWrite(_ledPin, !_ledOn); // Switch LED off
358369
}

0 commit comments

Comments
 (0)