Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7040309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:54:11+00:00 2026-05-28T01:54:11+00:00

I’m playing with node and (at the moment) simply trying to stream some files

  • 0

I’m playing with node and (at the moment) simply trying to stream some files from the filesystem over HTTP.

The Apache Bench (ab) app on my OS X Lion machine is buggy and seems to abort connections prematurely. This seems to have highlighted an issue with my node.js app where it leaks filehandles if the connection gets aborted.

I’ve reduced this to a simple node.js test case. When I launch this and run ‘ab’ against it, I eventually get an EMFILE exception due to too many open file handles:

// app.js 

var count = 0;
require('http').createServer(function(req,res){

    console.log('request ' + ++count);

    res.writeHead(200);
    require('fs').createReadStream(
        '/Users/tom/files/8339cdf73594d8f0aab87da123e9e0380723b471'
    ).pipe(res);

}).listen('3000');


$ node app.js
request 1
...
request 317
request 318
request 319

stream.js:105
      throw er; // Unhandled stream error in pipe.
            ^
Error: EMFILE, too many open files '/Users/tom/files/8339cdf73594d8f0aab87da123e9e0380723b471'

I assume what is happening is the HTTP connection is aborted but node’s .pipe() mechanism doesn’t know to stop reading from the readable stream and close the FD, so I tried to handle the ‘close’ event on the HTTP ServerRequest and destroy the FD, but something seems to be still reading from it as I get a bad FD error (EBADF) instead:

// app.js

var count = 0;
require('http').createServer(function(req,res){

    console.log('request ' + ++count);

    res.writeHead(200);
    var file = require('fs').createReadStream(
        '/Users/tom/dev/cloudstore2/files/8339cdf73594d8f0aab87da123e9e0380723b471'
    );

    req.on('close', function(){
        console.log('request received close - destroying read FD');
        file.destroy();
    });

    file.pipe(res);

}).listen('3000');

$ node app.js
...
request 112
request 113
request received close - destroying read FD
request received close - destroying read FD
request received close - destroying read FD
request received close - destroying read FD
request received close - destroying read FD
request received close - destroying read FD
request received close - destroying read FD

events.js:48
        throw arguments[1]; // Unhandled 'error' event
                       ^
Error: EBADF, bad file descriptor

Is there a ‘correct’ way to handle these aborted HTTP connections and close out my associated fs read stream, or am I seeing a bug in node.js’ .pipe() handling?


Update:
By catching the ‘error’ event on my file reader, I can avoid the app dying during this process, but is that the correct way to do this? I feel like I’m missing some way to avoid throwing a bad FD error in the first place.

<snip>
    var file = require('fs').createReadStream(
        '/Users/tom/dev/cloudstore2/files/8339cdf73594d8f0aab87da123e9e0380723b471'
    );

    file.on('error', function(err){
        console.log('error handled in file reader: ' + err);
    });

    req.on('close', function(){
        console.log('request received close - destroying read FD');
        file.destroy();
    });

    file.pipe(res);
</snip>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:54:12+00:00Added an answer on May 28, 2026 at 1:54 am

    According to the nodejs documentation

    http.ServerRequest

    Event: ‘close’

    Indicates that the underlaying connection was terminated before
    response.end() was called or able to flush.

    Just like ‘end’, this event occurs only once per request, and no more
    ‘data’ events will fire afterwards.

    Note: ‘close’ can fire after ‘end’, but not vice versa.

    I think that end() kills the destination fh of your pipe which means if end is fired first there is a small window where the pipe could be trying to write to the request fh after it’s closed. Try putting your file.destroy() call in the end() event.

    I tried to test my idea but I can’t get the error so please let me know if that worked for you. I’m worried I will run into this problem when I open my app up to other users.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a bunch of posts stored in text files formatted in yaml/textile (from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.