FG
📡 Networking

node-http-proxy leaks file descriptors

Freshabout 21 hours ago
Mar 14, 20260 views
Confidence Score53%
53%

Problem

Run this: [code block] - `ps -ef | grep node` to figure out the PID that's running it. - `watch 'lsof -p 5966 | wc -l'` (replace 5966 with your pid) - Point a browser at http://localhost:9000 (you'll get a 404 from example.com). I was using Chrome and FF. - Hit CTRL-R over and over. - Watch your open file count climb. Sockets don't seem to close for 2 minutes. On OS/X, heavy traffic will run out of file descriptors very quickly (my product's Selenium test case kills node-http-proxy about 1/10th of the way through.) Using node v0.10.25 and node-http-proxy 1.0.2.

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Unverified Fix
New Fix – Awaiting Verification

Implement Socket Timeout and Proper Cleanup in Node-HTTP-Proxy

Medium Risk

The node-http-proxy library does not properly close sockets after they are no longer needed, leading to file descriptor leaks. This is exacerbated by the default behavior of keeping sockets open for a longer duration, especially under heavy load, which can quickly exhaust the available file descriptors on systems like OS/X.

Awaiting Verification

Be the first to verify this fix

  1. 1

    Update Node and Node-HTTP-Proxy

    Upgrade to the latest stable versions of Node.js and node-http-proxy to benefit from bug fixes and improvements related to socket management.

    bash
    npm install -g node@latest node-http-proxy@latest
  2. 2

    Set Socket Timeout

    Configure the proxy to set a socket timeout to ensure that idle connections are closed after a specified duration. This helps in preventing file descriptor leaks.

    javascript
    const httpProxy = require('http-proxy');
    const proxy = httpProxy.createProxyServer({
      timeout: 5000 // Set timeout to 5 seconds
    });
  3. 3

    Handle Proxy Errors

    Add error handling to the proxy to ensure that sockets are closed on error events, preventing leaks during high traffic scenarios.

    javascript
    proxy.on('error', (err, req, res) => {
      console.error('Proxy error:', err);
      res.writeHead(500, {'Content-Type': 'text/plain'});
      res.end('Something went wrong.');
    });
  4. 4

    Monitor File Descriptors

    After implementing the changes, monitor the file descriptors to ensure they do not exceed the limit during high traffic. Use the lsof command to check the count.

    bash
    watch 'lsof -p <your_pid> | wc -l'
  5. 5

    Test Under Load

    Run your Selenium tests or any other load tests to confirm that the file descriptor count remains stable and does not exhaust the available limits.

    bash
    npm run test

Validation

Confirm that the file descriptor count remains stable during repeated requests and does not exceed the system limits. Additionally, check the application logs for any proxy errors to ensure they are being handled correctly.

Sign in to verify this fix

Environment

Submitted by

AC

Alex Chen

2450 rep

Tags

proxyhttpnode.js