title: nodejs: worker disconnect() not working as expected
body: <p>I am trying to disconnect worker from cluster when there is a request to route "/disconnect". Worker is not getting disconnected even though master is receiving the disconnect event from worker.</p>
  
  <p><strong>Worker</strong></p>
  
  <pre><code>var express=require("express"),
  app=express(),
  http=require("http"),
  server = null;
  
  app.get('/disconnect',function(req,res){
    var worker = require('cluster').worker;    
    setTimeout(function(){        
      // disconnect from the cluster            
      if(worker) worker.disconnect();                
     }, 2000);
    res.send("Worker " +   worker.id +  " is disconnecting ");
  });
  
  server = http.createServer(app).listen(3000);
  
  server.on('close', function() {
     console.log("server asked to close");
  });
  </code></pre>
  
  <p><strong>LOG</strong></p>
  
  <pre><code>&gt; node app/app_cluster.js
  
  CLUSTER: Worker 1 started
  CLUSTER: Worker 2 started
  CLUSTER: Worker 3 started
  CLUSTER: Worker 4 started
  Yay, the worker responded after it was forked
  Yay, the worker responded after it was forked
  Yay, the worker responded after it was forked
  Yay, the worker responded after it was forked
  A worker is now connected to null:3000
  A worker is now connected to null:3000
  A worker is now connected to null:3000
  A worker is now connected to null:3000
  CLUSTER: Worker 1 disconnected from the cluster.
  events.js:85
    throw er; // Unhandled 'error' event
          ^
  Error: IPC channel is already disconnected
  at process.target.disconnect (child_process.js:510:26)
  at Worker.disconnect (cluster.js:650:13)
  at null._onTimeout (/home/user/nodejs/node-hawkeye/app/app.js:18:31)
  at Timer.listOnTimeout (timers.js:110:15)
  CLUSTER: Worker 1 died with exit code 1 (null)
  </code></pre>
  
  <p>I have two doubts here</p>
  
  <ol>
  <li><p>For the first request to "/disconnect" route I see "CLUSTER: Worker 1 disconnected from the cluster." in the log. I wait for about 10 secs before sending request to "/disconnect" route again. When I send for the second time I got error "Error: IPC channel is already disconnected".</p></li>
  <li><p>According to the documentation <a href="https://nodejs.org/api/cluster.html#cluster_worker_disconnect" rel="nofollow">https://nodejs.org/api/cluster.html#cluster_worker_disconnect</a></p>
  
  <p><strong><em>In a worker, this function will close all servers, wait for the 'close' event on those servers, and then disconnect the IPC channel.</em></strong></p>
  
  <p>I am not seeing close event on the server in my worker code. Will worker call server.close() internally on worker.disconnect() ??  </p></li>
  </ol>