Skip to content

Commit 28a4463

Browse files
authored
Merge pull request #10477 from alexandrejbr/alexandrejbr/improve-keep-alive-renegotiation-timeout
ssh: improve renegotiation alive timeout calculation OTP-19750
2 parents f5f7ebb + 176f245 commit 28a4463

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

lib/ssh/src/ssh_connection_handler.erl

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2228,11 +2228,20 @@ triggered_alive(StateName, D0 = #data{},
22282228
%% feature acts as a keep-alive and a timeout, an equivalent timeout is
22292229
%% established for the renegotiation procedure if alive is enabled.
22302230
%% For simplicity the timeout value is derived from alive_interval and
2231-
%% alive_count.
2232-
renegotiation_alive_timeout(#ssh{opts = Opts}) ->
2231+
%% alive_count and takes in consideration the probes that may have already
2232+
%% been sent.
2233+
renegotiation_alive_timeout(#ssh{opts = Opts} = Ssh) ->
22332234
case ?GET_ALIVE_OPT(Opts) of
2234-
{_AliveCount, infinity} -> infinity;
2235-
{AliveCount, AliveInterval} -> AliveCount * AliveInterval
2235+
{_AliveCount, infinity} ->
2236+
infinity;
2237+
{AliveCount, AliveInterval} ->
2238+
#ssh{alive_last_sent_at = AliveLastSentAt,
2239+
alive_probes_sent = AliveProbesSent} = Ssh,
2240+
Now = erlang:monotonic_time(milli_seconds),
2241+
TimeSinceLastAlive = Now - AliveLastSentAt,
2242+
TotalElapsedTimeWithoutAlive =
2243+
AliveProbesSent * AliveInterval + TimeSinceLastAlive,
2244+
AliveCount * AliveInterval - TotalElapsedTimeWithoutAlive
22362245
end.
22372246

22382247
%%%################################################################

lib/ssh/test/ssh_protocol_SUITE.erl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1636,8 +1636,7 @@ alive_reneg_eserver_tclient(Config) ->
16361636
[{match, #ssh_msg_userauth_success{_='_'}, receive_msg},
16371637
{match, #ssh_msg_global_request{name = <<"[email protected]">>,
16381638
want_reply = true,
1639-
data = <<>>}, receive_msg},
1640-
{send, #ssh_msg_request_failure{}}],
1639+
data = <<>>}, receive_msg}],
16411640
State)
16421641
end,
16431642
{ok, TrptState1} = CheckAlive(TrptState0),
@@ -1647,8 +1646,7 @@ alive_reneg_eserver_tclient(Config) ->
16471646
ssh_trpt_test_lib:exec(
16481647
[{send, start_incomplete_renegotiation},
16491648
{match, #ssh_msg_kexinit{_='_'}, receive_msg},
1650-
{match, disconnect(), receive_msg}],
1651-
TrptState1),
1649+
{match, disconnect(), receive_msg}], TrptState1),
16521650
?CT_LOG("[OK] triggering incomplete, client triggered remotely key renegotiation"),
16531651
?CT_LOG("[starting] Alive feature - normal conditions 2"),
16541652
{ok, TrptState2} = connect_and_userauth_request(Host, Port, User, Pwd, UserDir),
@@ -1669,11 +1667,13 @@ alive_reneg_eserver_tclient(Config) ->
16691667
[CHandlerPid] = CHandler(ssh_info:get_subs_tree(sshd_sup), []),
16701668
?CT_LOG("Server side connection handler PID: ~p", [CHandlerPid]),
16711669
ssh_connection_handler:renegotiate(CHandlerPid),
1670+
%% The disconnect is received under 2 seconds since the tclient already
1671+
%% failed to reply to one of the probles from eserver.
16721672
{ok, _} =
16731673
ssh_trpt_test_lib:exec(
16741674
[{match, #ssh_msg_kexinit{_='_'}, receive_msg},
16751675
{match, disconnect(), receive_msg}],
1676-
TrptState3),
1676+
ssh_trpt_test_lib:set_timeout(TrptState3, 2000)),
16771677
?CT_LOG("[OK] triggering incomplete, server triggered locally key renegotiation"),
16781678
ssh:stop_daemon(DaemonPid),
16791679
?CT_LOG("[OK] test case finished"),

lib/ssh/test/ssh_trpt_test_lib.erl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
instantiate/2,
2828
format_msg/1,
2929
server_host_port/1,
30-
return_value/1
30+
return_value/1,
31+
set_timeout/2
3132
]
3233
).
3334

@@ -830,3 +831,6 @@ save_prints({Fmt,Args}, S) ->
830831

831832
return_value(#s{return_value = ReturnValue}) ->
832833
ReturnValue.
834+
835+
set_timeout(S, Timeout) ->
836+
S#s{timeout = Timeout}.

0 commit comments

Comments
 (0)