Skip to content

ssl: Buffer unsent encrypted data on send timeout#10916

Open
zuiderkwast wants to merge 2 commits intoerlang:masterfrom
zuiderkwast:ssl-socket-backend-timeout-buffering
Open

ssl: Buffer unsent encrypted data on send timeout#10916
zuiderkwast wants to merge 2 commits intoerlang:masterfrom
zuiderkwast:ssl-socket-backend-timeout-buffering

Conversation

@zuiderkwast
Copy link
Copy Markdown
Contributor

When using gen_tcp with {inet_backend, socket} and send_timeout, gen_tcp:send may return {error, {timeout, RestData}} with the unsent encrypted data. Previously this fell through to the generic error handler which killed the connection.

Buffer the RestData in a new #sync{} record in the tls_sender state and retry sending it together with new data on the next ssl:send call. Reply {error, timeout} to the caller to simulate {inet_backend, inet} behavior.

When alerts, post-handshake data or renegotiation need to send while a #sync{} buffer exists, attempt to flush the buffer first. If the flush succeeds, proceed normally. If it times out again, postpone the event and re-buffer the remaining data.

Note:
This PR built on top of #10908 because it relies on passing a list of gen_tcp options such as [{inet_backend, socket}, {send_timeout, 1}] where inet_backend must be the first in the list and ssl must not reorder them. I'll rebase this once that one is merged, or let me know if I shall structure or combine the PRs differently.

@IngelaAndin

The emulated_options/3 function in tls_socket and dtls_socket used a
prepend accumulator pattern without reversing the result, causing inet
options to be reversed when passed to gen_tcp:connect/gen_udp:open.

This broke the inet_backend option which must be the first option in
the list according to gen_tcp and gen_udp documentation.

Fix by reversing the Inet accumulator before returning. For
dtls_socket, also change the initial Inet accumulator from
internal_inet_values() to [] since those values are already appended
in the connect and listen call sites.

Signed-off-by: Viktor Söderqvist <viktor.soderqvist@est.tech>
When using gen_tcp with {inet_backend, socket} and send_timeout,
gen_tcp:send may return {error, {timeout, RestData}} with the unsent
encrypted data. Previously this fell through to the generic error
handler which killed the connection.

Buffer the RestData in a new #sync{} record in the tls_sender state
and retry sending it together with new data on the next ssl:send call.
Reply {error, timeout} to the caller to simulate {inet_backend, inet}
behavior.

When alerts, post-handshake data or renegotiation need to send while
a #sync{} buffer exists, attempt to flush the buffer first. If the
flush succeeds, proceed normally. If it times out again, postpone the
event and re-buffer the remaining data.

Signed-off-by: Viktor Söderqvist <viktor.soderqvist@est.tech>
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 25, 2026

CT Test Results

    2 files     66 suites   25m 55s ⏱️
  820 tests   773 ✅  46 💤 1 ❌
4 282 runs  3 324 ✅ 957 💤 1 ❌

For more details on these failures, see this check.

Results for commit 6009ad8.

♻️ This comment has been updated with latest results.

To speed up review, make sure that you have read Contributing to Erlang/OTP and that all checks pass.

See the TESTING and DEVELOPMENT HowTo guides for details about how to run test locally.

Artifacts

// Erlang/OTP Github Action Bot

@IngelaAndin IngelaAndin added team:PS Assigned to OTP team PS stalled waiting for input by the Erlang/OTP team labels Mar 25, 2026
@IngelaAndin
Copy link
Copy Markdown
Contributor

We will consider this after the OTP-29 release.

@bjorng bjorng added this to the 30.0 milestone Mar 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

stalled waiting for input by the Erlang/OTP team team:PS Assigned to OTP team PS

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants