Node.js Stream API

while playing with node threads, I noticed that almost every tutorial teaches something like:

// Get Google home page. require('http').get("http://www.google.com/", function(response) { // The callback provides the response readable stream. // Then, we open our output text stream. var outStream = require('fs').createWriteStream("out.txt"); // Pipe the input to the output, which writes the file. response.pipe(outStream); }); 

But, in my opinion, this is quite dangerous code. What happens if a file stream throws some kind of exception? I think the file stream may leak memory, because according to the documents the file stream is clearly not close.

Should I take care? In my node.js option, threads should handle situations ...

+5
asynchronous stream pipe
Dec 08 '13 at 2:11
source share
2 answers

To discard any errors in the Node VM, if there is an exception that interrupts the operation after the thread has been opened, I expected that eventually during the garbage collection the virtual machine will find that nothing is referencing the thread and collect it, thereby getting rid of the resources associated with it.

Therefore, I would not call it a leak.

You may still encounter problems with handling exceptions or closing threads. For example, on Unix-type systems, when a stream is created that matches a file on disk, a file descriptor is used. There is a limit to how many file descriptors can be opened at a time by a process. Therefore, if a process that does not explicitly close its threads manages to leave so many of these unclosed that it reaches the file descriptor limit before the next garbage collection, it will crash.

+2
Dec 08 '13 at 2:15
source share

To avoid leaking file descriptor, you also need to:

 var outStream = require('fs').createWriteStream("out.txt"); // Add this to ensure that the out.txt file descriptor is closed in case of error. response.on('error', function(err) { outStream.end(); }); // Pipe the input to the output, which writes the file. response.pipe(outStream); 

Another undocumented method is outStream.destroy() , which also closes the handle, but it seems that outStream.end() preferable.

+4
May 01 '14 at 2:19
source share



All Articles