title: Gracefully shutdown UNIX-socket server on NodeJS running under Forever
body: <p>I have an NodeJS application which sets up a UNIX-socket to expose some interprocess communication channel (some kind of monitoring stuff). UNIX-socket file is placed in <code>os.tmpdir()</code> folder (i.e. <code>/tmp/app-monitor.sock</code>).</p>
  
  <pre><code>var net = require('net');
  var server = net.createServer(...);
  server.listen('/tmp/app-monitor.sock', ...);
  </code></pre>
  
  <p>I use a signal handling (SIGINT, SITERM, etc...) to gracefully shutdown my server and remove a socket file.</p>
  
  <pre><code>function shutdown() {
      server.close(); // socket file is automatically removed here
      process.exit();
  }
  
  process.on('SIGINT', shutdown);
  // and so on
  </code></pre>
  
  <p>My application is running with <code>forever start ...</code> to monitor it's lifecycle.</p>
  
  <p>I have a problem with <code>forever restartall</code> command. When forever doing <code>restartall</code> it's using a <code>SIGKILL</code> to terminate all child processes. <code>SIGKILL</code> can't be handled by a process so my app dies without any shutdown procedures. </p>
  
  <p>The problem is a socket file which is not removed when <code>SIGKILL</code> is used. After the child process is restarted, new server can't be started cause' a <code>listen</code> call will cause a <code>EADDRINUSE</code> error.</p>
  
  <p>I can't remove a existing socket file during an app startup cause' I don't know if it a real working socket or some traces of a previous unclean shutdown.</p>
  
  <p>So, the question is... What is the better way to handle such situation (SIGKILL and UNIX-socket server)?</p>