title: Hanging file descriptors in nodejs with simple web server
body: <p>I have had a very interesting problem that I cannot seem to solve. It actually may not be a problem at all but rather something built into node.js. I am having an issue with file descriptors staying around longer than expected after a response has been sent. They seem to persist in the ESTABLISHED state for up to 2 minutes after any data has been sent. This is causing issues with our production servers. Even though we are serving static files, the number of file descriptors that can be open at one time can be very high. They frequently hit our system limit (linux) and cause EMFILE issues. I realize we could modify our hard ulimit above 1024, but this seems like a hacky fix to a different issue. It would seem to me as soon as the socket is closed, the file descriptor should be released. </p>
  
  <p>I have node.js version: 0.10.24</p>
  
  <p>I have been able to replicate this issue with a small amount of code. </p>
  
  <p>http server:</p>
  
  <pre><code>var http = require("http"),
      url = require("url"),
      path = require("path"),
      fs = require("fs")
      port = process.argv[2] || 8888;
  
  http.createServer(function(request, response) {
  
    var uri = url.parse(request.url).pathname
      , filename = path.join(process.cwd(), uri);
  
    var contentTypesByExtension = {
      '.html': "text/html",
      '.css':  "text/css",
      '.js':   "text/javascript"
    };
  
    path.exists(filename, function(exists) {
      fs.readFile(filename, "binary", function(err, file) {
        if(err) {        
          response.writeHead(500, {"Content-Type": "text/plain"});
          response.write(err + "\n");
          response.end();
          return;
        }
  
        var headers = {};
        var contentType = contentTypesByExtension[path.extname(filename)];
        if (contentType) headers["Content-Type"] = contentType;
        response.writeHead(200, headers);
        response.write(file, "binary");
  
        response.end();
      });
    });
  }).listen(parseInt(port, 10));
  
  console.log("Static file server running..."); 
  ```
  </code></pre>
  
  <p>Make a request to the server. I made a simple request for a static js file. Monitor the file descriptors before and after making the request.</p>
  
  <pre><code>$ lsof -i -n -P | grep node
  </code></pre>
  
  <p>My results:</p>
  
  <p>After starting server and before request:</p>
  
  <p><code>node      16787 ncswenson   11u  IPv4 0x884c32306a467c65      0t0    TCP *:8888 (LISTEN)</code></p>
  
  <p>After request completes (up to 2 minutes after request):</p>
  
  <p><code>node      16787 ncswenson   11u  IPv4 0x884c32306a467c65      0t0    TCP *:8888 (LISTEN)
  node      16787 ncswenson   12u  IPv4 0x884c32306a2773b5      0t0    TCP 127.0.0.1:8888-&gt;127.0.0.1:49399 (ESTABLISHED)</code></p>
  
  <p>Is this how node should properly behave? Leave the file descriptor open for minutes after a request? Is there a way to circumvent this issue? Is there a way to better investigate this issue?</p>