title: spawning node.js child processes results in zombie processes on cloud foundry
body: <p>I have a node.js app, and I want to spawn child processes using the code listed below.</p>
  
  <p>When I run this app locally, each of the 'ps' commands nicely trigger the close and exit events. On our cloud foundry (pivotal.io) app however, the stdout.close is triggered, but the 'close' and 'exit' events of the child process itself never. Also, the processes remain as zombie processes in memory (so after ~500 request the server dies on a E_SPAWN error). So it seems that the exit handler in the node.js process handle is never triggered, resulting in the exit code of the child process not being read. </p>
  
  <p>Can this relate to the container warden, cgroups...? Does anybody have a solution for this or at least ran into the same issue?</p>
  
  <p><strong>The test code:</strong></p>
  
  <pre><code>var cp = require('child_process');
  
  //..create express app
  
  app.get('/foo/', function(req, res, next) {
      var child = cp.spawn("ps",["aux"]);
      child.stderr.pipe(process.stderr);
  
      child.stdout.on('data', function(data) {
          console.log('data');
          res.send("\n&lt;br&gt;OUT" + data.toString());
      });
  
      child.stdout.on('close', function() {
          console.log('close stdout');
          res.send("\n&lt;br&gt;CLOSE STDOUT");
      });
  
      child.on('close', function() {
          console.log('close');
          res.send("\n&lt;br&gt;CLOSE");
      });
  
      child.on('exit', function() {
          console.log('exit');
          res.send("\n&lt;br&gt;EXIT");
      });
  });
  
  app.listen();
  </code></pre>
  
  <p><strong>Example <code>ps aux</code> output:</strong></p>
  
  <pre><code>&lt;br&gt;OUTUSER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
  root         1  0.0  0.0   1124   308 ?        S&lt;s  14:15   0:00 wshd: 189gaonkujh    
  vcap        31  0.2  0.0 602676 21692 ?        S&lt;sl 14:15   0:00 node index.js 1234
  vcap        33  0.0  0.0      0     0 ?        Z&lt;   14:17   0:00 [ps] &lt;defunct&gt;
  vcap        34  0.0  0.0  15332  1172 ?        R&lt;   14:17   0:00 ps aux
  </code></pre>
  
  <p><strong>UPDATE</strong></p>
  
  <p>See the comments for a workaround: use a custom start command that starts with 'true;', e.g. <code>cf push myapp -c 'true;node index.js'</code></p>