title: NodeJS Socket.io : Many connections in state CLOSE_WAIT and FIN_WAIT2 without release
body: <p>I used ubuntu(12.04) + nodejs (v0.10.22) + socket.io (v0.9.14) to transmit messages.</p>
  
  <p>There are ~300 simultaneous connections. After some hours (about 1 or 2 hours above, it doesn't show up immediately), some connections will persistent in the state <strong>CLOSE_WAIT</strong> or <strong>FIN_WAIT2</strong>.</p>
  
  <p>And these un-dead connections grows linearly with time. The users will hard to connect socket server when the connections number reach the limit (Default 1024) , unless some connections released normally.</p>
  
  <p>The following was socket service connections status, running about 3 hours.</p>
  
  <pre><code>netstat -anl | grep &lt;PORT_OF_NODE_PROCESS&gt; | awk '/^tcp/ {t[$NF]++}END{for(state in t){print state, t[state]} }'
  
  FIN_WAIT2 23
  LISTEN 1
  CLOSE_WAIT 27
  TIME_WAIT 12
  ESTABLISHED 333
  FIN_WAIT1 12
  </code></pre>
  
  <h1>Probably Solutions</h1>
  
  <h2>1. Touch js file in regular periods</h2>
  
  <p>Using <a href="https://npmjs.org/package/nodemon">Nodemon Package</a> to run js file, when change the file's last modified time, nodemon will restart service, and release all previous un-dead connections (<strong>CLOSEWAIT</strong> or <strong>FINWAIT2</strong>)</p>
  
  <h2>2. Increase connections limit</h2>
  
  <pre><code>sudo vim /etc/security/limits.conf
  
  *       soft    nofile  1024
  *       hard    nofile  2048
  root    soft    nofile  4096
  root    hard    nofile  8192
  user1   soft    nofile  2048
  user1   hard    nofile  2048
  </code></pre>
  
  <p>Try to let connections hard to reach limit.</p>
  
  <h2>3. Decrease keep-alive timeout</h2>
  
  <p>Let operation system to close connections automatically in the short time, but I'm not try it yet.</p>
  
  <h1>Question</h1>
  
  <p>I found some probably solution to fix the problem. But the above solutions were not really solved the persistent connections with state <strong>CLOSE_WAIT</strong> or <strong>FIN_WAIT2</strong> problem. I could find this is a result of server(<strong>CLOSE_WAIT</strong>) or clients (<strong>FIN_WAIT2</strong>) not correctly closing connections. I think socket.io will force-close these incorrectly connection after some timeout. But it seems like not work correctly.</p>
  
  <p>I try to reappear the state <strong>CLOSE_WAIT</strong> or <strong>FIN_WAIT2</strong> problem in my test environment. But it never show up these connection situation.</p>
  
  <ol>
  <li>After connect socket server and disconnect network</li>
  <li>Connect socket server for a long time</li>
  </ol>
  
  <p>I found someone ask related question before (<a href="https://github.com/LearnBoost/socket.io/issues/1040">Many stale connections in state CLOSE_WAIT and FIN_WAIT2</a>), but still can't find the solution. Does anyone know how to solve this problem??</p>
  
  <p>Thanks</p>