Skip to content

request with agent, timeout not work #21185

@killagu

Description

@killagu
  • Version: 10.3
  • Platform: all
  • Subsystem: http

When request with agent, set the request timeout not work, timeout is still
agent timeout.

The code:

'use strict';

const http = require('http');
const port = 8000;
const AGENT_TIMEOUT = 3000;
const HTTP_TIMEOUT = 5000;

const server = http
  .createServer((req, res) => {
    console.log('never response');
  })
  .listen({ port }, () => doRequest());

function doRequest() {
  const agent = new http.Agent({ timeout: AGENT_TIMEOUT });
  const request = http.request({
    host: 'localhost',
    port,
    agent,
    timeout: HTTP_TIMEOUT, // The HTTP_TIMEOUT will not work.
  }, res => {});

  request.end();

  const start = Date.now();
  request.on('timeout', () => {
    console.log('timeout:' + (Date.now() - start)); // It print 3000, not 5000
    request.abort();
    server.close();
  });

  request.on('error', () => {});
}

I expect request timeout will overwrite the agent time out.

If it's a bug, I can make a PR to fix it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    httpIssues or PRs related to the http subsystem.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions