Skip to content

Conversation

@benoitc
Copy link
Owner

@benoitc benoitc commented Jan 18, 2026

Summary

  • Fixes issue where pooled connections become stale when servers close idle connections
  • Connection now detects tcp_closed/ssl_closed via {active, once} mode when idle in pool
  • Stale connections are removed from pool, preventing {error, closed} on reuse

Implementation

  • Enable {active, once} selectively when connections become idle in pool (not on initial connect)
  • Handle tcp_closed/ssl_closed in connected state → transition to closed
  • Pooled connections in closed state terminate with {stop, normal} so pool cleans up
  • Use peername for health checks (compatible with active mode)
  • Switch to passive mode before request operations
  • Handle HTTP/3 (QUIC) connections where socket is undefined

Test plan

  • New test test_server_close_detected verifies server-initiated close detection
  • All 836 existing tests pass
  • Pool tests: 15/15
  • Connection tests: 16/16
  • Integration tests: 24/24
  • HTTP/3 tests: passing

Closes #544

When a server closes an idle connection in the pool, hackney now detects
it via tcp_closed/ssl_closed messages and removes the connection from
the pool, preventing {error, closed} on the next request.

Implementation:
- Enable {active, once} on sockets when connections become idle in pool
- Handle tcp_closed/ssl_closed in connected state to transition to closed
- Pooled connections in closed state terminate with {stop, normal}
- Use peername for health checks (works with active mode)
- Set socket to passive mode before request operations
- Handle HTTP/3 (QUIC) connections where socket is undefined

Closes #544
@benoitc benoitc merged commit 8b8b79e into master Jan 18, 2026
3 of 5 checks passed
@benoitc benoitc deleted the fix/issue-544-stale-connection-detection branch January 18, 2026 21:15
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.

Reused socket closing connection

2 participants