Skip to content

http.IncomingMessage.setTimeout callback will never be called? #13864

Closed

Description

@stouf
  • Version: v8.1.2
  • Platform: Darwin XXXXX.local 16.6.0 Darwin Kernel Version 16.6.0: Fri Apr 14 16:21:16 PDT 2017; root:xnu-3789.60.24~6/RELEASE_X86_64 x86_64 (Mac OS 10.12.5, installed with HomeBrew 1.2.3)
  • Subsystem: http

The documentation tells us that http.IncomingMessage.setTimeout(msecs, callback) calls message.connection.setTimeout(msecs, callback). However, by looking at the code, it seems that it indeed calls message.connection.setTimeout(msecs) (through the socket object) but callback gets attached as a listener of the timeout event coming from the IncomingMessage class.
First of all, please correct me if I'm wrong, but this behavior is not quite what the documentation describes is it?
Then, despite the fact that the timeout event apparently gets fired there, for some reason, I can't get callback in http.IncomingMessage.setTimeout(msec, callback) to be called.

'use strict';

const http = require('http');

const server = http.createServer((req, res) => {
  const start = Date.now();
  req.connection.on('timeout', () => {
    console.log(`request time: ${Date.now() - start }`);
  });


  if (req.url === '/long') {
    req.setTimeout(500, (socket) => {
      console.log('req.setTimeout callback');
      socket.destroy(new Error('timeout'));
    });
  }

  setTimeout(() => {
    res.statusCode = 200;
    res.end();
  }, 5000);
});

server.setTimeout(100, (socket) => {
  console.log('server.setTimeout callback');
  socket.destroy(new Error('timeout'));
});

server.listen(1234, () => {
  console.log('Listening');
});

With the code, above, I tried the following:

curl http://localhost:1234
# Outputs
#   server.setTimeout callback
#   request time: 104
curl http://localhost:1234/long
# Outputs
#   server.setTimeout callback
#   request time: 504

It seems that callback of request.setTimeout(msec, callback) never gets called.

Maybe am I missing/misunderstanding something here?

Activity

  1. added
    http
    on Jun 22, 2017
  2. lance commented on Jun 22, 2017

    @lance
    Member

    I believe this is because, in your example, the request is not actually timing out. You're just sending a simple GET request with curl that doesn't hang around and take more than 500ms. If you change your server.listen() callback to something like this, you'll see that the req.setTimeout function is called.

    server.listen(1234, () => {
      console.log('Listening');
      const req = http.request({ port: 1234, method: 'POST', path: '/long' });
      req.write('This could take a while...');
      setTimeout(_ => {
        req.end('See? I told you');
      }, 1000);
    });
  3. added
    invalid
    on Jun 22, 2017
  4. lance commented on Jun 22, 2017

    @lance
    Member

    Closing for now. @nodejs/collaborators feel free to reopen if I am mistaken.

  5. stouf commented on Jun 23, 2017

    @stouf
    Author

    Thanks @lance , I was indeed misunderstand pending requests on the server-side and client request timing out. Thanks for the explanation! :)

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    httpinvalid

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions