From 805f82cc1c5602e3d0f39eab2296e164e3e43e42 Mon Sep 17 00:00:00 2001 From: ARAYN7011 <88572149+ARAYN7011@users.noreply.github.com> Date: Fri, 20 Nov 2026 11:56:38 +0530 Subject: [PATCH 1/2] fix: cancel the keepalive timer after an HTTP/1.1 TLS upgrade A freshly dialed pooled conn arms the keepalive timer on entering connected. upgrade_to_ssl blocks the conn in ssl:connect/3, and the HTTP/1.1 branch kept the timer running, so a handshake slower than keepalive_timeout closed the conn right after the upgrade returned and the caller got {error, closed}. Cancel it as the HTTP/2 branch does. Fixes #938 Co-Authored-By: Claude Sonnet 5.5 --- src/hackney_conn.erl | 6 ++- test/hackney_conn_upgrade_idle_tests.erl | 54 ++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 test/hackney_conn_upgrade_idle_tests.erl diff --git a/src/hackney_conn.erl b/src/hackney_conn.erl index 6f0dc760..4cdd4faf 100644 --- a/src/hackney_conn.erl +++ b/src/hackney_conn.erl @@ -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}}]} diff --git a/test/hackney_conn_upgrade_idle_tests.erl b/test/hackney_conn_upgrade_idle_tests.erl new file mode 100644 index 00000000..68c86e81 --- /dev/null +++ b/test/hackney_conn_upgrade_idle_tests.erl @@ -0,0 +1,54 @@ +%%% -*- 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), + Parent = self(), + 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), + Parent ! {server_ready, self()}, + 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. From b82c973e9852a28f292d178b35fbe150e3cc7bc2 Mon Sep 17 00:00:00 2001 From: ARAYN7011 <88572149+ARAYN7011@users.noreply.github.com> Date: Sat, 21 Nov 2026 10:13:49 +0530 Subject: [PATCH 2/2] test: drop unread server_ready message from upgrade idle test The message leaked into the test process mailbox and broke a later integration test that does a bare receive. --- test/hackney_conn_upgrade_idle_tests.erl | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/hackney_conn_upgrade_idle_tests.erl b/test/hackney_conn_upgrade_idle_tests.erl index 68c86e81..44796ddc 100644 --- a/test/hackney_conn_upgrade_idle_tests.erl +++ b/test/hackney_conn_upgrade_idle_tests.erl @@ -26,13 +26,11 @@ slow_handshake_keeps_conn_open() -> peer => [{key, {rsa, 2048, 17}}]}}), {ok, LSock} = gen_tcp:listen(0, [binary, {active, false}, {ip, {127, 0, 0, 1}}]), {ok, Port} = inet:port(LSock), - Parent = self(), 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), - Parent ! {server_ready, self()}, receive stop -> ssl:close(SslSock) end end), Opts = #{host => "127.0.0.1", port => Port, transport => hackney_tcp,