Skip to content
This repository was archived by the owner on Apr 22, 2023. It is now read-only.
This repository was archived by the owner on Apr 22, 2023. It is now read-only.

HTTP Server Response Socket Timeout After Two Minutes Does Not Allow Catching timeout event and respond to socket #3460

Closed
@dscape

Description

@dscape

Currently node sets a two minute timeout on a socket by default

socket.setTimeout(2 * 60 * 1000); // 2 minute timeout
socket.once('timeout', function() {
  socket.destroy();
});

http.js

Problem is that once the timeout event is fired the socket is destroyed, making it impossible for modules using http to intercept the event:

var http = require('http');

http.createServer(function (req, res) {
  res.socket.setTimeout(10);
  res.socket.once('timeout', function () {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello World\n');
  });
}).listen(1337, '127.0.0.1');

However when you curl you get:

$ curl localhost:1337
curl: (52) Empty reply from server

Which is not the expected behavior.

A work-around is to do:

var http = require('http');

http.createServer(function (req, res) {
  res.socket.setTimeout(10);
  res.socket.removeAllListeners('timeout');
  res.socket.once('timeout', function () {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello World\n');
  });
}).listen(1337, '127.0.0.1');

Which works correctly, but at impossible to predict consequences as this is userland code and node-core might break this in the future:

$ curl localhost:1337
Hello World

A solution was indicated to me by @isaacs stating changing to process.nextTick semantics could easily solve this:

socket.setTimeout(2 * 60 * 1000); // 2 minute timeout
socket.once('timeout', function() {
  process.nextTick(socket.destroy);
});

http-modified.js

+@mmalecki

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions