Skip to content

Commit 010e6a0

Browse files
committed
fix(ssl_client): Chunk TLS writes and reset timeout after progress
Chunk TLS writes and reset timeout after progress to reduce mid-body resets Send large TLS payloads in moderate chunks (4 KiB) instead of a single large write, and measure the write timeout from the last successful progress. This significantly reduces sporadic MBEDTLS_ERR_NET_CONN_RESET (-0x0050) observed during long HTTP bodies (e.g., multipart uploads). - write loop remains intact; now caps per-call size to 4096 bytes - updates timeout window after each positive write to avoid false timeouts on slow links - no API changes; handshake/verification paths unaffected Sources Ask ChatGPT
1 parent c18ff4b commit 010e6a0

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

libraries/NetworkClientSecure/src/ssl_client.cpp

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -413,26 +413,36 @@ int send_ssl_data(sslclient_context *ssl_client, const uint8_t *data, size_t len
413413
return 0; // Skipping zero-length write
414414
}
415415

416-
const unsigned long write_start_time = millis();
417-
416+
const size_t kChunk = 4096;
417+
unsigned long last_progress = millis(); // Timeout since last progress
418418
size_t sent = 0;
419+
419420
while (sent < len) {
420-
const size_t to_send = len - sent;
421-
const int ret = mbedtls_ssl_write(&ssl_client->ssl_ctx, data + sent, to_send);
421+
size_t to_send = len - sent;
422+
if (to_send > kChunk) {
423+
to_send = kChunk;
424+
}
425+
426+
int ret = mbedtls_ssl_write(&ssl_client->ssl_ctx, data + sent, to_send);
422427
if (ret > 0) {
423428
sent += ret;
429+
last_progress = millis(); // refresh timeout window
424430
continue;
425431
}
426-
if ((millis() - write_start_time) > ssl_client->socket_timeout) {
427-
log_v("SSL write timed out.");
428-
return -1;
432+
433+
if ((millis() - last_progress) > ssl_client->socket_timeout) {
434+
log_v("SSL write timed out.");
435+
return -1;
429436
}
437+
430438
if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE && ret < 0) {
431-
log_v("Handling error %d", ret);
432-
return handle_error(ret);
439+
log_v("Handling error %d", ret);
440+
return handle_error(ret);
433441
}
442+
434443
vTaskDelay(2);
435444
}
445+
436446
return (int)sent;
437447
}
438448

0 commit comments

Comments
 (0)