Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save thomas-riccardi/312b4471c955572ed3b7 to your computer and use it in GitHub Desktop.
Save thomas-riccardi/312b4471c955572ed3b7 to your computer and use it in GitHub Desktop.
HTTP Server request.setTimeout(n, cb) does sometimes call socket.destroy(), contradicting documentation
var http = require('http');
var mode;
mode = 'requestTimeout';
// mode = 'responseTimeout';
// mode = 'serverTimeout';
// mode = 'serverTimeoutExternal';
var timeout = 100;
var port = 3000;
console.log('Test HTTP Server with timeout on Server, Request, Response & socket destroy: unexpected destroy on req.setTimeout(n, cb);');
var server = http.createServer(function(req, res) {
console.log('server', 'got request');
if (mode === 'requestTimeout') {
req.setTimeout(timeout, function() {
console.log('server', 'request timeout');
res.end('server hello from request timeout');
});
}
if (mode === 'responseTimeout') {
res.setTimeout(timeout, function() {
console.log('server', 'request timeout');
res.end('server hello from request timeout');
});
}
if (mode === 'serverTimeout' || mode === 'serverTimeoutExternal') {
server.setTimeout(timeout, function() {
console.log('server', 'request timeout');
res.end('server hello from request timeout');
});
}
}).listen(port, function() {
console.log('client', 'sending request');
var req = http.request({port: port}, function(res) {
console.log('client', 'got response');
res.on('data', function (chunk) {
console.log('client', 'reponse', 'data:', chunk.toString());
});
res.on('finish', function () {
console.log('client', 'response', 'finish');
});
});
req.on('error', function(e) {
console.log('client', 'error', e);
});
req.end();
});
if (mode === 'serverTimeoutExternal') {
server.setTimeout(timeout);
}
// cleanup
setTimeout(function() {
console.log('exit');
process.exit();
}, timeout + 100);
/** Results:
* same results for the following node versions: v0.10.41, v0.12.9, v4.2.3, v5.2.0
** mode RequestTimeout
client sending request
server got request
client error { [Error: socket hang up] code: 'ECONNRESET' } <-- unexpected
** mode ResponseTimeout
client sending request
server got request
server request timeout
client got response
client reponse data: server hello from request timeout
** mode ServerTimeout
client sending request
server got request
** mode ServerTimeoutExternal
client sending request
server got request
server request timeout
client got response
client reponse data: server hello from request timeout
**/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment