Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I close a stream that has no more data to send in node.js?
    text
    copied!<p>I am using node.js and reading input from a serial port by opening a /dev/tty file, I send a command and read the result of the command and I want to close the stream once I've read and parsed all the data. I know that I'm done reading data by and end of data marker. I'm finding that once I've closed the stream my program does not terminate.</p> <p>Below is an example of what I am seeing but uses /dev/random to slowly generate data (assuming your system isn't doing much). What I find is that the process will terminate once the device generates data <em>after</em> the stream has been closed.</p> <pre><code>var util = require('util'), PassThrough = require('stream').PassThrough, fs = require('fs'); // If the system is not doing enough to fill the entropy pool // /dev/random will not return much data. Feed the entropy pool with : // ssh &lt;host&gt; 'cat /dev/urandom' &gt; /dev/urandom var readStream = fs.createReadStream('/dev/random'); var pt = new PassThrough(); pt.on('data', function (data) { console.log(data) console.log('closing'); readStream.close(); //expect the process to terminate immediately }); readStream.pipe(pt); </code></pre> <p><strong>Update:1</strong></p> <p>I am back on this issue and have another sample, this one just uses a pty and is easily reproduced in the node repl. Login on 2 terminals and use the pty of the terminal you're not running node in the below call to createReadStream.</p> <pre><code>var fs = require('fs'); var rs = fs.createReadStream('/dev/pts/1'); // a pty that is allocated in another terminal by my user //wait just a second, don't copy and paste everything at once process.exit(0); </code></pre> <p>at this point node will just hang and not exit. This is on 10.28.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload