Skip to content

Instantly share code, notes, and snippets.

@isaacs
Last active December 13, 2015 20:58
Show Gist options
  • Save isaacs/4973669 to your computer and use it in GitHub Desktop.
Save isaacs/4973669 to your computer and use it in GitHub Desktop.
var http = require('http');
// good thing we're using the same actual buffer over and over,
// or else this would blow up. Can you imagine if it was real
// data, and not just the same chunk over and over again?
var chunk = new Buffer(65536);
var server = http.createServer(function(req, res) {
res.write('start it off...');
// give the client time to kill the connection,
// then write a metric ton of junk.
setTimeout(write, 1000);
function write() {
// eat until full
while (false !== res.write(chunk));
// eat some more! you're too skinny!
for (var i = 0; i < 64; i++) {
res.write(chunk);
}
// try again in 100ms
setTimeout(write, 100);
setTimeout(checkLeaky, 100);
}
function checkLeaky() {
var l = res.output.map(function(c) {
return c.length;
}).reduce(function(a, b) {
return a + b;
});
console.error('leaking: %d', l);
if (l > 16 * 1024 * 1024) {
throw new Error('too much leaking!');
}
}
});
server.listen(1337, function() {
var req = http.request({
port: 1337,
path: '/',
method: 'GET'
});
req.on('response', function(res) {
console.error('kill connection');
// srsly, I'm full, stop.
res.socket.destroy();
res.on('end', function() {
console.error('got end');
server.close();
});
});
req.end();
});
@isaacs
Copy link
Author

isaacs commented Feb 17, 2013

v0.8.19:

$ ./node leaky.js
kill connection
got end
leaking: 8520850
leaking: 12781275
leaking: 17041700

timers.js:103
            if (!process.listeners('uncaughtException').length) throw e;
                                                                      ^
Error: too much leaking!
    at Object.checkLeaky (/Users/isaacs/node/leaky.js:37:13)
    at Timer.list.ontimeout (timers.js:101:19)

v0.8.20:

$ ./node leaky.js
kill connection
got end

timers.js:103
            if (!process.listeners('uncaughtException').length) throw e;
                                                                      ^
Error: socket hang up
    at createHangUpError (http.js:1360:15)
    at ServerResponse.OutgoingMessage._writeRaw (http.js:507:26)
    at ServerResponse.OutgoingMessage._send (http.js:476:15)
    at ServerResponse.OutgoingMessage.write (http.js:744:12)
    at Object.write [as _onTimeout] (/Users/isaacs/node/leaky.js:17:26)
    at Timer.list.ontimeout (timers.js:101:19)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment