-
Notifications
You must be signed in to change notification settings - Fork 299
Description
- Node.js Version 8.15.2:
- Linux OpenSuSE LEAP 15:
Consider the following HTTP client which receives HTTP data stream using data event. This stream contains commands which have to be executed in a sequence. Thus the client pauses the receiver while processing the received data buffer, and then resumes it after it is ready again.
Here is the sample client code, where processing delay is implemented using timeouts:
const http = require('http');
let agent_options = { host: "127.0.0.1", port: 7777, path: "/", method: 'GET' };
let request = http.request( agent_options, function(res) {
res.on('data', (data)=> {
res.pause();
setTimeout( ()=>{ res.resume(); }, 1 );
});
});
request.end();
setInterval( ()=>{ console.log( "Memory use: %sMb", process.memoryUsage().heapUsed >> 20); }, 1000 );
This works fine with the server on Internet. However with a server running locally it starts eating memory, and very fast runs out of memory and crashes:
node test.js
Memory use: 47Mb
Memory use: 93Mb
Memory use: 140Mb
Memory use: 189Mb
Memory use: 235Mb
Memory use: 283Mb
Memory use: 340Mb
Memory use: 385Mb
Memory use: 429Mb
Memory use: 487Mb
Memory use: 526Mb
Memory use: 574Mb
Here is an example of the simple server which reproduces the issue:
const http = require('http');
const { Readable } = require("stream");
const zerostream = new Readable({
read(size) {
this.push( Buffer.alloc( 128 ) );
}
});
// Create the web service
const webserver = http.createServer( function( request, response ) {
request.on('data', (chunk) => {} );
request.on('end', () => {
response.writeHead( 200, { 'Content-Type' : 'application/octet-stream', 'Transfer-Encoding' : 'chunked' } );
zerostream.pipe( response );
});
});
webserver.listen( { "host" : "127.0.0.1", "port" : 7777 } );