Skip to content

Fix WiFiClientSecure::available() blocking on dropped connections #6449

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Sep 17, 2019
Merged
27 changes: 22 additions & 5 deletions libraries/ESP8266WiFi/src/WiFiClientSecureBearSSL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ extern "C" {
}
#include "debug.h"
#include "ESP8266WiFi.h"
#include "PolledTimeout.h"
#include "WiFiClient.h"
#include "WiFiClientSecureBearSSL.h"
#include "StackThunk.h"
Expand Down Expand Up @@ -437,12 +438,17 @@ int WiFiClientSecure::_run_until(unsigned target, bool blocking) {
DEBUG_BSSL("_run_until: Not connected\n");
return -1;
}
for (int no_work = 0; blocking || no_work < 2;) {
if (blocking) {
// Only for blocking operations can we afford to yield()
optimistic_yield(100);

esp8266::polledTimeout::oneShotMs loopTimeout(_timeout);

for (int no_work = 0; blocking || no_work < 2;) {
optimistic_yield(100);

if (loopTimeout) {
DEBUG_BSSL("_run_until: Timeout\n");
return -1;
}

int state;
state = br_ssl_engine_current_state(_eng);
if (state & BR_SSL_CLOSED) {
Expand All @@ -461,8 +467,19 @@ int WiFiClientSecure::_run_until(unsigned target, bool blocking) {
unsigned char *buf;
size_t len;
int wlen;
size_t availForWrite;

buf = br_ssl_engine_sendrec_buf(_eng, &len);
availForWrite = WiFiClient::availableForWrite();

if (!blocking && len > availForWrite) {
/*
writes on WiFiClient will block if len > availableForWrite()
this is needed to prevent available() calls from blocking
on dropped connections
*/
len = availForWrite;
}
wlen = WiFiClient::write(buf, len);
if (wlen <= 0) {
/*
Expand Down