Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

fd leakage while using readable.pipe #8232

Closed
darren opened this issue Aug 22, 2014 · 3 comments
Closed

fd leakage while using readable.pipe #8232

darren opened this issue Aug 22, 2014 · 3 comments

Comments

@darren
Copy link

darren commented Aug 22, 2014

src.pipe(dst) , what happens if dst closes before src ends? there's some leakage lurking

The steps to make it happen:

start a server test.js

var http = require('http');
var fs = require('fs');

var server = http.createServer(function (req, res) {
    var stream = fs.createReadStream(__dirname + '/data.txt');
    stream.pipe(res);
});
server.listen(8000);

make sure that data.txt is not so small in size, 100MB should do.

$ node test.js

then start this shell script

$ while :; do                                                      
    curl http://localhost:8000/ --limit-rate  10k  >/dev/null &  
    sleep 1                                                       
    kill -9 $!                                                    
done

wait for a while the server exits with:

$ node test.js
events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: EMFILE, open '/Users/darren/data.txt'

Although I can cleanup resource manually by adding this

    res.on('close', function() {
    stream.destroy();
    });

I think pipe should at least provide an option when user specifies it should do the housekeeping for the user.

@omeid
Copy link

omeid commented Aug 22, 2014

The script is same as this:

$ while true; do                                                      
    curl http://localhost:8000/ --limit-rate  100k  >/dev/null &  
    sleep 1                                           
    kill -9 $!  # Kill the previous process: curl                                               
done

@vkurchatkin
Copy link

it's a known issue. see #5134, #6041, #6220. https://github.com/jshttp/on-finished is a solution

@indutny
Copy link
Member

indutny commented Aug 27, 2014

Yeah, a duplicate. Thanks @vkurchatkin

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants