title: readstream pipe does not close
body: <p>I am using sax-js to read large xml files. I cannot get the program to exit when the parser is finished. Here is the shape of the script, with parser logic removed.</p>
  
  <pre><code>    var fs = require('fs');
      var sax = require('sax');
  
      var feedFile = 'foo.xml';
      var saxStream = sax.createStream(true)
      .on('opentag', function(node) { // do stuff })
      .on('end', function() {
           console.log("parser end event");
      });
  
      var options = {
           flags: 'r',
           encoding: 'utf8',
           mode: 0666,
           bufferSize: 1024
      };
  
      fs.createReadStream(feedFile, options, function(err) {
          throw err;
      })
      .on('end', function() {
          console.log("read stream end event");
      })
      .pipe(saxStream);
  </code></pre>
  
  <p>Everything works properly. And the console gets the messages like this</p>
  
  <pre><code>    read stream end event
      parser end event
  </code></pre>
  
  <p>And then the process should exit. It does not. What am I missing?</p>
  
  <hr>
  
  <p><strong>EDIT: What I am missing.</strong>
  There was a resource still open so that node could not exit. The temptation is to start trying to call process.exit() somewhere, anywhere. I tried it myself in this case and it broke stuff.</p>
  
  <p>My script could not exit because I had an open mongodb connection (managed by mongoose). Here is essentially (not in detail) what fixed it:</p>
  
  <pre><code>    saxStream.on('end', function() {
          console.log("parser end event");
          mongoose.disconnect();
      };
  </code></pre>