14

I have the following set up:

A node.js client makes end-to-end requests to a node.js server. After less than a minute, the client fails with error ENOBUFS.

client:

(function(){ 

        var loadUrl=function(){
            var http=require('http');   
            var querystring=require('querystring'); 
            var options = {host:"localhost",port:1337,path:'/post',method:'POST'};

            var req = http.request(options, function(res){              
                res.setEncoding('utf8');
                var body='';
                res.on('data', function (chunk) {
                    body+=chunk;
                });           
                res.on('end', function (chunk) {
                    loadUrl();   
                });   
            }); 
            req.on('error', function(e) {
              console.log('problem with request: ' + e.message);
            });
            var post_data = querystring.stringify({id:0});
            req.write(post_data);
            req.end();
        }
        setTimeout(loadUrl,1000);   
    })()

server:

var http = require('http');
http.createServer(function (req, res) {
      res.writeHead(200, {'Content-Type': 'text/plain'});
      res.end('Hello World\n');
}).listen(1337, '127.0.0.1');

While this question is similar, I am posting this as a generalization of the original question (I am using post rather than get), with a test case.

0

1 Answer 1

6

The issue appears to be a problem with the Node.js HTTP client connection pool.

If you add the option agent:false to the options argument of the http.request() function it will disable connection pooling and have each request use the header Connection: close. This change seems to allow the client code to run indefinitely.

var options = {agent:false, host:"localhost", port:1337, /*...*/ };

Doing this will degrade the performance of the HTTP clients and you should see frequent pauses in the client process (presumably while the V8 runtime does garbage collection). But it does seem to solve your problem!

Per @joshp's comment, see if this issue has been addressed in a later version of Node.js or consider filing a bug report.

3
  • Nice answer. So for @Tom it's probably worth looking at issues for Node to see if it's known or addressed in a later version.
    – joshp
    Commented May 15, 2012 at 16:07
  • @maerics - Thank you! on what os did you reproduce this? Did anyone else reproduce the problem? On which operating system(s)? Wondering if this is Windows specific.
    – Tom
    Commented May 15, 2012 at 16:21
  • @Tom: I reproduced on Mac OS X 10.7 using node v0.6.10.
    – maerics
    Commented May 15, 2012 at 17:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.