[BUG] HTTP connection leak - creates new connection per request instead of reusing
Bug Report: HTTP Connection Leak
What's Wrong?
Claude Code creates new TCP connections for each API request instead of reusing existing connections via HTTP keep-alive. This causes TIME_WAIT connection accumulation that scales with network timeout settings.
Measured behavior:
- With standard 120s TIME_WAIT timeout: ~1800 connections accumulated to api.anthropic.com
- Connections rapidly churn: 25 ESTABLISHED → 9 → 3 in 4 seconds after activity stops
- Each connection uses a unique port number (no connection reuse observed)
- Pattern: Create connection → Single request → Immediate close
What Should Happen?
Claude Code should use HTTP keep-alive with connection pooling, maintaining 2-10 persistent connections that are reused for multiple requests (standard Node.js HTTPS behavior).
Error Messages/Logs
Connection monitoring output:
=== Connection state over time ===
Sample 1: 25 ESTABLISHED
Sample 2 (2s later): 9 ESTABLISHED
Sample 3 (2s later): 3 ESTABLISHED
=== Total accumulation (120s TIME_WAIT) ===
1677 connections to api.anthropic.com
171 connections to statsig.anthropic.com
Port number analysis showing no reuse:
Ports at T=0s: 63912 64158 64159 64160 64161 64163 64164...
Ports at T=5s: 64186 64188
(All different ports = new connections, not reused)
Steps to Reproduce
- Use Claude Code normally for 2-3 minutes (regular chat interaction)
- Monitor TCP connections in another terminal:
``bash``
netstat -an | grep api.anthropic.com | awk '{print $6}' | sort | uniq -c
- Observe during activity:
- 10-25 ESTABLISHED connections active
- 200-1800+ TIME_WAIT connections (scales with network TIME_WAIT timeout)
- Observe after activity stops:
- ESTABLISHED connections drop to 2-3 within seconds
- TIME_WAIT connections persist for timeout duration
- Repeat monitoring every 2 seconds - observe different port numbers each time (proves no connection reuse)
Claude Model
Sonnet (default)
Is this a regression?
I don't know
Claude Code Version
2.0.9 (Claude Code)
Platform
Anthropic API
Operating System
macOS
Terminal/Shell
Other
Additional Information
Impact:
- Excessive connection table consumption on routers/firewalls
- Potential connection limit exhaustion
- May trigger automated alerts on network monitoring systems
- Affects users behind NAT with connection tracking
Current workaround:
Reduced TIME_WAIT timeout on router from 120s to 10s:
sysctl -w net.netfilter.nf_conntrack_tcp_timeout_time_wait=10
This reduces accumulation from ~1800 to ~150 connections, but only treats symptoms.
Root cause analysis:
Claude Code's HTTP client appears to be missing keep-alive configuration. Each API call creates a new TCP connection instead of reusing existing ones.
Expected fix:
Configure HTTPS agent with keep-alive:
const agent = new https.Agent({
keepAlive: true,
maxSockets: 10,
keepAliveMsecs: 30000
});
Testing notes:
- Tested through both rule-based and global proxy modes - identical behavior
- Confirms issue is Claude Code's HTTP client configuration
This issue has 5 comments on GitHub. Read the full discussion on GitHub ↗