Skip to content

fix: cancel the keepalive timer after an HTTP/1.1 TLS upgrade - #1042

Merged
benoitc merged 2 commits into
benoitc:masterfrom
ARAYN7011:fix/keepalive-timer-tls-upgrade
Nov 22, 2026
Merged

benoitc merged 2 commits into
benoitc:masterfrom
ARAYN7011:fix/keepalive-timer-tls-upgrade

Conversation

@ARAYN7011

Copy link
Copy Markdown
Contributor

What

In the HTTP/1.1 branch of upgrade_to_ssl, cancel the keepalive state_timeout (as the HTTP/2 branch already does through init_h2_after_upgrade).

Why

A freshly dialed pooled conn arms idle_timeout when it enters connected, and ssl:connect/3 blocks the conn process. If the handshake takes longer than keepalive_timeout (capped at 2000 ms), the timer fires as soon as the upgrade returns. The conn goes to closed before the request is written, and the caller gets an error. #934 only covers reused conns via is_ready. The timer is re-armed when the conn re-enters connected after the request.

Fixes #938

How to test

rebar3 eunit --module=hackney_conn_upgrade_idle_tests. The test delays the server's TLS handshake past idle_timeout. It fails before the change (conn is closed) and passes after. The upgrade, pool-checkout-idle and TLS-resumption suites also pass. In the full rebar3 eunit run, the only failure is hackney_happy_tests "connect to non-existent domain fails", which needs DNS and the sandbox has none.

This change was prepared with AI assistance (Claude Code) and reviewed against the repo's tests.

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 benoitc#938

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@benoitc

benoitc commented Nov 20, 2026

Copy link
Copy Markdown
Owner

Thanks, the fix is right. CI is red because of the new test, not DNS. It sends {server_ready, Pid} to the test process and never reads it. hackney_integration_tests_async_long_headers later receives that message and fails. Removing the Parent ! {server_ready, self()} line should fix it.

Optional: a test through hackney:request with a pool (keepalive_timeout 300, handshake delayed 800 ms) would cover the case from #938 directly.

@ARAYN7011

ARAYN7011 commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for the review and for spotting the cause. You're right: the stray {server_ready, Pid} message was leaking into the test process mailbox. I removed it in b82c973. The upgrade test and hackney_integration_tests both pass locally. I can add the pooled hackney:request test you suggested as a follow-up if you'd like it in this PR.

The message leaked into the test process mailbox and broke a later
integration test that does a bare receive.
@benoitc
benoitc merged commit c6f1745 into benoitc:master Nov 22, 2026
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.

keepalive timer also closes a freshly dialed pooled connection during the HTTP/1.1 TLS upgrade

2 participants