Skip to content

Conversation

@holbrookab-augment
Copy link

@holbrookab-augment holbrookab-augment commented Dec 20, 2025

Fix EPIPE Errors with Proper HTTP Agent Connection Pooling and Timeouts

Problem

The Node.js SDK was experiencing EPIPE (broken pipe / socket connection issues) errors for us when making HTTP requests. These errors occurred due to:

  1. Improper connection pooling: The previous implementation using https-proxy-agent didn't properly handle connection reuse
  2. Missing timeout configuration: No freeSocketTimeout was configured, causing stale connections to be reused after the server had closed them
  3. Inadequate keep-alive settings: Connection pooling wasn't configured with proper timeout values

Solution

This PR replaces https-proxy-agent with agentkeepalive and hpagent to implement proper HTTP agent connection pooling with comprehensive timeout configuration:

Key Changes

  1. Replaced dependencies:

    • Removed: https-proxy-agent@^7.0.6
    • Added: agentkeepalive@^4.5.0 and hpagent@^1.2.0
  2. Implemented shared HTTP/HTTPS agents with proper timeout configuration:

    • timeout: 30000 - Request timeout matching SDK defaults
    • freeSocketTimeout: 15000 - Prevents EPIPE errors from stale connections
    • keepAlive: true and keepAliveMsecs: 1000 - Enables connection reuse
  3. Enhanced proxy support:

    • Updated createProxyAgentcreateProxyAgents to return both HTTP and HTTPS agents
    • Both proxy agents use the same timeout configuration as direct connections
    • Added getAgent helper function to select the appropriate agent based on URL protocol

Technical Details

  • agentkeepalive: Provides freeSocketTimeout support across all Node.js versions, which is critical for preventing EPIPE errors when connections are reused after being idle
  • hpagent: Supports all standard agent options (including keepAlive and freeSocketTimeout) for proxy scenarios, ensuring consistent behavior whether using a proxy or direct connections

Benefits

  • ✅ Prevents EPIPE errors by properly handling stale connections
  • ✅ Improves connection reliability through proper connection pooling
  • ✅ Maintains backward compatibility - no breaking changes to the API
  • ✅ Works consistently with or without proxy configuration

Testing

  • All existing tests pass (120 tests)
  • No breaking changes to the public API
  • Backward compatible with existing proxy configurations
  • Tested it separately in our own infrastructure: this package contains the changed code). We don't use the proxy but the other changes have not resulted in any prior socket issues in 2 days of current test.

Files Changed

  • statsig-node/package.json - Updated dependencies
  • statsig-node/src/lib/index.ts - Core HTTP agent implementation
  • statsig-node/pnpm-lock.yaml - Lock file updates

- Replace https-proxy-agent with agentkeepalive and hpagent
- Add proper timeout configuration (timeout: 30000, freeSocketTimeout: 15000)
- Implement shared HTTP/HTTPS agents with keep-alive settings
- Enhance proxy support with both HTTP and HTTPS agents
- Maintain backward compatibility
@holbrookab-augment
Copy link
Author

@weihao-statsig not urgent, but I made this PR based on some issues we've had recently. Hope it helps!

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.

1 participant