Skip to content
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
6 changes: 5 additions & 1 deletion src/hackney_conn.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1226,7 +1226,11 @@ connected({call, From}, {upgrade_to_ssl, SslOpts, UpgradeOpts}, #conn_data{socke
http2 ->
init_h2_after_upgrade(SslSocket, NewData, From);
http1 ->
{keep_state, NewData, [{reply, From, ok}]}
%% The handshake blocked this process, so the keepalive timer
%% armed on entering `connected' may be spent or about to fire
%% on a conn that has not carried a request yet (#938).
{keep_state, NewData, [{state_timeout, infinity, idle_timeout},
{reply, From, ok}]}
end;
{error, Reason} ->
{keep_state_and_data, [{reply, From, {error, Reason}}]}
Expand Down
52 changes: 52 additions & 0 deletions test/hackney_conn_upgrade_idle_tests.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
%%% -*- erlang -*-
%%%
%%% This file is part of hackney released under the Apache 2 license.
%%% See the NOTICE for more information.
%%%
%%% Regression for #938: the keepalive timer armed by connected(enter) kept
%%% running across an HTTP/1.1 TLS upgrade. ssl:connect/3 blocks the conn, so a
%%% handshake slower than the keepalive timeout let the timer fire as soon as
%%% the upgrade returned, closing a conn that had not yet carried a request.
-module(hackney_conn_upgrade_idle_tests).

-include_lib("eunit/include/eunit.hrl").

-define(IDLE_MS, 300).
-define(HANDSHAKE_DELAY_MS, 800).

slow_handshake_keeps_conn_open_test_() ->
{timeout, 30, fun slow_handshake_keeps_conn_open/0}.

slow_handshake_keeps_conn_open() ->
{ok, _} = application:ensure_all_started(hackney),
#{server_config := ServerOpts} =
public_key:pkix_test_data(#{server_chain => #{root => [{key, {rsa, 2048, 17}}],
peer => [{key, {rsa, 2048, 17}}]},
client_chain => #{root => [{key, {rsa, 2048, 17}}],
peer => [{key, {rsa, 2048, 17}}]}}),
{ok, LSock} = gen_tcp:listen(0, [binary, {active, false}, {ip, {127, 0, 0, 1}}]),
{ok, Port} = inet:port(LSock),
Server = spawn_link(fun() ->
{ok, Sock} = gen_tcp:accept(LSock, 5000),
%% Hold the ClientHello unanswered for longer than the idle timeout.
timer:sleep(?HANDSHAKE_DELAY_MS),
{ok, SslSock} = ssl:handshake(Sock, ServerOpts, 5000),
receive stop -> ssl:close(SslSock) end
end),
Opts = #{host => "127.0.0.1", port => Port, transport => hackney_tcp,
connect_timeout => 5000, idle_timeout => ?IDLE_MS},
{ok, Pid} = hackney_conn:start_link(Opts),
try
ok = hackney_conn:connect(Pid, 2000),
?assertEqual({ok, connected}, hackney_conn:get_state(Pid)),
?assertEqual(ok, hackney_conn:upgrade_to_ssl(Pid, [{verify, verify_none},
{versions, ['tlsv1.2']}],
#{final => true})),
%% Before the fix the stale timer fired right after the upgrade returned.
timer:sleep(?IDLE_MS + 200),
?assertMatch({connected, _}, sys:get_state(Pid))
after
Server ! stop,
_ = (try hackney_conn:stop(Pid) catch _:_ -> ok end),
gen_tcp:close(LSock)
end.
Loading