Skip to content

TimeoutErrors are possible if the response is available but not processed #8

Description

@jsocol

If a request is made and a response comes back, but the response is not handled before the timeout, a TimeoutError is thrown. This can occur when CPU-bound code blocks the event loop. To demonstrate, run a fast webserver in one process, and use this example code:

'use strict';

const sleep = require('sleep');  // npm install sleep
const cu = require('copilot-util');  // npm install copilot-util

function main() {
  cu.http.request({
    'host': 'localhost',
    'port': 5000,
    'path': '/',
    'timeout': 2000  // Set to less than the "sleep"
  }).then(function (response) {
    console.log(response.statusCode);
  });
  // Need to give this enough time to let the request start,
  // setImmediate caused the event loop to block before the request
  // began, but with setTimeout(..., 100) the request was often handled
  // before the sleep began
  setTimeout(function () {
    sleep.msleep(4000);  // blocks the event loop for 4 seconds
  }, 10);
}

main();

In the webserver process, you can observe a response getting written with a 200 status code, but because the promise timeout is handled before the response, we still get a TimeoutError:

Unhandled rejection TimeoutError: Timeout of 2000ms reached (/)
    at ClientRequest.rejectWithTimeoutError (/Users/james/code/tmp/node_modules/copilot-util/lib/http/request/_timeout_listener.js:8:14)
    at ClientRequest.g (events.js:291:16)
    at emitNone (events.js:86:13)
    at ClientRequest.emit (events.js:185:7)
    at Socket.emitRequestTimeout (_http_client.js:565:42)
    at Socket.g (events.js:291:16)
    at emitNone (events.js:91:20)
    at Socket.emit (events.js:185:7)
    at Socket._onTimeout (net.js:342:8)
    at ontimeout (timers.js:380:14)
    at tryOnTimeout (timers.js:244:5)
    at Timer.listOnTimeout (timers.js:214:5)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    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