Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

WiFiClientSecure: robust TLS writes (loop & chunk), avoid zero-length write -> fixes sporadic MBEDTLS_ERR_NET_CONN_RESET #11865

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
me-no-dev merged 3 commits into espressif:master from prooma:fix/chunked-tls-write
Sep 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions libraries/NetworkClientSecure/src/NetworkClientSecure.cpp
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,10 @@ size_t NetworkClientSecure::write(const uint8_t *buf, size_t size) {
return 0;
}

if (size == 0) {
return 0;
}

if (_stillinPlainStart) {
return send_net_data(sslclient.get(), buf, size);
}
Expand Down
30 changes: 23 additions & 7 deletions libraries/NetworkClientSecure/src/ssl_client.cpp
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -409,25 +409,41 @@ int data_to_read(sslclient_context *ssl_client) {
}

int send_ssl_data(sslclient_context *ssl_client, const uint8_t *data, size_t len) {
unsigned long write_start_time = millis();
int ret = -1;
if (len == 0) {
return 0; // Skipping zero-length write
}

static constexpr size_t max_write_chunk_size = 4096;
unsigned long last_progress = millis(); // Timeout since last progress
size_t sent = 0;

while (sent < len) {
size_t to_send = len - sent;
if (to_send > max_write_chunk_size) {
to_send = max_write_chunk_size;
}

while ((ret = mbedtls_ssl_write(&ssl_client->ssl_ctx, data, len)) <= 0) {
if ((millis() - write_start_time) > ssl_client->socket_timeout) {
int ret = mbedtls_ssl_write(&ssl_client->ssl_ctx, data + sent, to_send);
if (ret > 0) {
sent += ret;
last_progress = millis(); // refresh timeout window
continue;
}

if ((millis() - last_progress) > ssl_client->socket_timeout) {
log_v("SSL write timed out.");
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
log_v("Handling error %d", ret);
return handle_error(ret);
}

//wait for space to become available
vTaskDelay(2);
}

return ret;
return (int)sent;
}

// Some protocols, such as SMTP, XMPP, MySQL/Posgress and various others
Expand Down
Loading

AltStyle によって変換されたページ (->オリジナル) /