Skip to content

Commit 5d609fd

Browse files
johnm545devyte
authored andcommitted
Fix WiFiClientSecure::available() blocking on dropped connections (#6449)
* Fix WiFiClientSecure::available blocking Added a check of WiFiClient::availableForWrite to prevent blocking writes when the _run_until blocking flag is false * change availForWrite from int to size_t * add timeout to _run_until loop fixes #6464 * use polledTimeout with _timeout millis
1 parent f2de9e1 commit 5d609fd

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

libraries/ESP8266WiFi/src/WiFiClientSecureBearSSL.cpp

+22-5
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ extern "C" {
3232
}
3333
#include "debug.h"
3434
#include "ESP8266WiFi.h"
35+
#include "PolledTimeout.h"
3536
#include "WiFiClient.h"
3637
#include "WiFiClientSecureBearSSL.h"
3738
#include "StackThunk.h"
@@ -437,12 +438,17 @@ int WiFiClientSecure::_run_until(unsigned target, bool blocking) {
437438
DEBUG_BSSL("_run_until: Not connected\n");
438439
return -1;
439440
}
440-
for (int no_work = 0; blocking || no_work < 2;) {
441-
if (blocking) {
442-
// Only for blocking operations can we afford to yield()
443-
optimistic_yield(100);
441+
442+
esp8266::polledTimeout::oneShotMs loopTimeout(_timeout);
443+
444+
for (int no_work = 0; blocking || no_work < 2;) {
445+
optimistic_yield(100);
446+
447+
if (loopTimeout) {
448+
DEBUG_BSSL("_run_until: Timeout\n");
449+
return -1;
444450
}
445-
451+
446452
int state;
447453
state = br_ssl_engine_current_state(_eng);
448454
if (state & BR_SSL_CLOSED) {
@@ -461,8 +467,19 @@ int WiFiClientSecure::_run_until(unsigned target, bool blocking) {
461467
unsigned char *buf;
462468
size_t len;
463469
int wlen;
470+
size_t availForWrite;
464471

465472
buf = br_ssl_engine_sendrec_buf(_eng, &len);
473+
availForWrite = WiFiClient::availableForWrite();
474+
475+
if (!blocking && len > availForWrite) {
476+
/*
477+
writes on WiFiClient will block if len > availableForWrite()
478+
this is needed to prevent available() calls from blocking
479+
on dropped connections
480+
*/
481+
len = availForWrite;
482+
}
466483
wlen = WiFiClient::write(buf, len);
467484
if (wlen <= 0) {
468485
/*

0 commit comments

Comments
 (0)