Closed
Description
No details about the device required as whole ecosystem is affected.
Affected component: WiFiClientSecure, ssl_client
Description
Function send_ssl_data, called by WiFiClientSecure doesn't respect timeout set on socket.
After disconnecting the network (not WiFi directly, just turn off LTE or disconnect network cable), having some small piece of code sending periodically few messages to MQTT broker via SSL, I've encountered freeze inside send_ssl_data function.
After some investigation, it appears to hang inside while loop.
Temporary working solution, but 5000 is a magic constant:
int send_ssl_data(sslclient_context *ssl_client, const uint8_t *data, size_t len)
{
log_v("Writing HTTP request with % bytes...", len); //for low level debug
int ret = -1;
unsigned long send_start_time=millis();
while ((ret = mbedtls_ssl_write(&ssl_client->ssl_ctx, data, len)) <= 0) {
if((millis()-send_start_time)>5000)
return -1;
if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE && ret < 0) {
log_v("Handling error %d", ret); //for low level debug
return handle_error(ret);
}
//wait for space to become available
vTaskDelay(2);
}
return ret;
}
The best way to fix this will be to introduce send_timeout inside sslclient_context and set it to socket timeout (passed in connect method by WiFiClientSecure).