Skip to content

MB-74905: Don't lose peer close with {exit_on_close, false} - #36

Merged
timofey-barmin merged 2 commits into
couchbase-28.5.0.8from
MB-74905
Oct 30, 2026
Merged

timofey-barmin merged 2 commits into
couchbase-28.5.0.8from
MB-74905

Conversation

@timofey-barmin

Copy link
Copy Markdown

Fixes a lost peer close on TLS sockets using {exit_on_close, false}.

If the peer closes the TCP connection while the ssl socket is passive and no ssl:recv is pending, ssl defers the close until the next activation and relies on setopts on the TCP socket failing to rediscover it. With {exit_on_close, false} the TCP port survives the peer close, so setopts succeeds and the close is lost: {active, once} never delivers ssl_closed, and ssl:recv hangs until its timeout.

The fix records the transport close explicitly and delivers it on the next activation.

Tested with a standalone repro on OTP 28.5.0.6, TLS 1.2 and 1.3:

exit_on_close=true exit_on_close=false
before ssl_closed no ssl_closed / recv timeout
after ssl_closed ssl_closed / {error, closed}

Data sent before the FIN is still delivered before the close, and ssl:send after the peer's FIN still works.

Reported upstream to erlang/otp.

To Reproduce

-module(repro).
-export([run/0]).

run() ->
    {ok, _} = application:ensure_all_started(ssl),
    Chain = #{root => [{digest, sha256}, {key, {namedCurve, secp256r1}}], intermediates => [],
              peer => [{digest, sha256}, {key, {namedCurve, secp256r1}}]},
    #{server_config := SOpts, client_config := COpts} =
        public_key:pkix_test_data(#{server_chain => Chain, client_chain => Chain}),
    [io:format("exit_on_close=~p: ~p~n", [EOC, test(EOC, SOpts, COpts)])
     || EOC <- [true, false]],
    ok.

test(ExitOnClose, SOpts, COpts) ->
    {ok, L} = ssl:listen(0, [{active, false}, {exit_on_close, ExitOnClose} | SOpts]),
    {ok, {_, Port}} = ssl:sockname(L),
    Self = self(),
    spawn(fun() ->
                  {ok, C} = ssl:connect("localhost", Port,
                                        [{active, false}, {verify, verify_none} | COpts]),
                  ok = ssl:shutdown(C, write),        %% TCP FIN, no close_notify
                  Self ! done,
                  receive after infinity -> ok end
          end),
    {ok, S0} = ssl:transport_accept(L),
    {ok, S} = ssl:handshake(S0),
    receive done -> ok end,
    timer:sleep(200),                                 %% let the FIN reach ssl
    ok = ssl:setopts(S, [{active, once}]),
    receive
        {ssl_closed, S} -> ssl_closed
    after 2000 ->
        no_ssl_closed
    end.
$ erlc repro.erl && erl -noshell -s repro run -s init stop
exit_on_close=true: ssl_closed
exit_on_close=false: no_ssl_closed

@stevewatanabe stevewatanabe left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few concerns with this fix. Inline comments cover the main ones; the rest are below.

Possible duplicate close messages. The PassiveTag handler (around L296) calls do_activate_socket whether or not active_n_toggle is set. With transport_closed set, a queued {tcp_passive, S} and the flow_ctrl -> activate_socket path can each send {tcp_closed, S} to self. The first one stops the process, so it's harmless today, but it depends on that ordering.

Two separate ways to redeliver a close. There's now the transport_closed flag and the existing setopts-error branch (around L737). Every activation path has to know about both, and kTLS is one that was missed (see inline). It would be more robust to always record the close at the point it's deferred and stop relying on setopts failing to detect it.

No regression test. Nothing in lib/ssl/test (e.g. ssl_api_SUITE / ssl_socket_SUITE) covers {exit_on_close, false} with a passive socket and a peer FIN. The repro in the PR description would make a good test, and without one a future upstream OTP merge into this branch could silently drop the fix, with ssl:recv hanging until timeout again.

Comment thread lib/ssl/src/tls_gen_connection.erl Resolved
Comment thread lib/ssl/src/tls_gen_connection.erl Resolved
Comment thread lib/ssl/src/tls_gen_connection.erl Resolved
Comment thread lib/ssl/src/tls_gen_connection.erl Resolved
@timofey-barmin
timofey-barmin changed the base branch from couchbase-28.5.0.6 to couchbase-28.5.0.8 October 30, 2026 16:45
@timofey-barmin

Copy link
Copy Markdown
Author

Possible duplicate close messages.

I don't think this sequence can happen. transport_closed is set while handling the deferred tcp_closed, and inet_drv never sends tcp_passive after tcp_closed: tcp_passive is only sent while delivering data, and once the active count runs out the port stops reading, so it only sees the FIN after being re-armed. I checked the message order for an active-N gen_tcp socket with the peer sending data and then a FIN: it's always [Data, tcp_passive] or [Data, tcp_closed], never tcp_passive after tcp_closed, for both exit_on_close values. After tcp_closed, re-arming produces nothing at all, which is the original bug. The connection handles messages in order, so any tcp_passive has been handled before the flag is set.

It also doesn't depend on ordering. A redelivered close either stops the connection or is deferred again, and deferring only sets flags that are already set, so a second copy gives the same result whichever is handled first. The two paths can't both fire within one event either: do_activate_socket/2 resets active_n_toggle, and the flow_ctrl -> activate_socket path only runs when it's true.

This also isn't new with this change: with exit_on_close = true, the existing setopts-error branch already sends {CloseTag, Socket} to self from both paths, since setopts fails on the dead port in each.

Two separate ways to redeliver a close.

I think the patch already does what you're suggesting. The close is recorded where it's deferred (transport_closed is set in the deferred branch, the only place that defers a close), and do_activate_socket/2 checks it before calling setopts, so a deferred close no longer depends on setopts failing, for either exit_on_close value.

Activation paths don't need to know about either mechanism: both are inside do_activate_socket/2, the single function that re-arms the inner socket. Its only callers are the passive handler and activate_socket/2, and neither looks at the flag or the setopts result. kTLS isn't an activation path that was missed; it deliberately never re-arms the socket (see the inline thread).

I've left the setopts-error branch as is. It predates the deferred-close logic (it's from the ERL-420 fix, before erlang#2875), and after this change it's only a fallback for setopts failing for some other reason, e.g. a port that has died while its exit or close message is still queued. A direct close of the inner port is handled by the 'EXIT' handler instead. Removing it would mean changing upstream code for no functional gain, and I want this fork patch to stay additive so it's easy to carry across OTP merges and to drop if upstream fixes this differently.

No regression test.

Added

timofey-barmin and others added 2 commits October 30, 2026 11:22
If the peer closes the TCP connection while the ssl socket is passive
and nobody is in ssl:recv, the close is deferred until the next socket
activation. The connection relied on the subsequent setopts on the tcp
socket failing in order to re-trigger the close. With
{exit_on_close, false} the tcp port survives the peer close and doesn't
report it again, so setopts succeeds and the close is lost: a later
{active, once} never yields ssl_closed and ssl:recv hangs until
timeout.

Remember the transport close explicitly and deliver it on the next
activation instead of relying on the state of the tcp port.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The peer sends data followed by a TCP FIN without close_notify, which
reaches the connection while the socket is passive and nobody is in
ssl:recv. Check that a later ssl:recv and {active, once} deliver the
data and then report the close, with {exit_on_close, false}. Also check
that data can still be sent to the half-closed peer before that.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@timofey-barmin
timofey-barmin merged commit eae91ee into couchbase-28.5.0.8 Oct 30, 2026
4 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants