got: "RequestError: socket hang up" in `.json()` within `afterResponse` hook and agent keepAlive

Describe the bug

  • Node.js version: 12.16.1
  • OS & version: lastest MacOS

Updating from working got@10 to got@11.0.2

Consider this snippet:

    const opportunitiesIterator = client.paginate<Opportunity>('Opportunities', {
      json: { filter: [{ someField: 'someValue' }] },
    });
    for await (const record of opportunitiesIterator) {
      // Some processing
    }

the client variable is a got@11.0.2 instance with the following notable options:

  • agent with Agent({ keepAlive: true }) for both http and https
  • prefixUrl with public https base url of our Sugar CRM rest api
  • allowGetBody as true for most CRM queries implies an http GET with a body full of json
  • hooks.afterResponse with an async function to acquire/renew OAuth token
  • mutableDefaults as true
  • pagination.paginate and pagination.transform

Actual behavior

Upon first evaluation of for await (const record of opportunitiesIterator) there is this error:

✖ [ERROR] RequestError: socket hang up
    at ClientRequest.<anonymous> (/Users/guillaumec/rc-dev/rcsf/node_modules/got/dist/source/core/index.js:759:25)
    at Object.onceWrapper (events.js:418:26)
    at ClientRequest.emit (events.js:323:22)
    at ClientRequest.origin.emit (/Users/guillaumec/rc-dev/rcsf/node_modules/@szmarczak/http-timer/dist/source/index.js:39:20)
    at TLSSocket.socketCloseListener (_http_client.js:400:11)
    at TLSSocket.emit (events.js:323:22)
    at net.js:668:12
    at connResetException (internal/errors.js:604:14)
    at TLSSocket.socketCloseListener (_http_client.js:400:25)
    at TLSSocket.emit (events.js:323:22)
    at net.js:668:12
    at TCP.done (_tls_wrap.js:556:7) {
  name: 'RequestError',
  code: 'ECONNRESET',
  timings: {
    start: 1587549556887,
    socket: 1587549556887,
    lookup: undefined,
    connect: undefined,
    secureConnect: undefined,
    upload: undefined,
    response: undefined,
    end: undefined,
    error: 1587549556888,
    abort: 1587549556889,
    phases: {
      wait: 0,
      dns: undefined,
      tcp: undefined,
      tls: undefined,
      request: undefined,
      firstByte: undefined,
      download: undefined,
      total: 2
    }
  }
}

Expected behavior

Well, it shouldn’t crash, should it?

Code to reproduce

// working on it...

Checklist

  • I have read the documentation.
  • I have tried my code with the latest version of Node.js and Got.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 47 (23 by maintainers)

Most upvoted comments

Sorry. I realise now how my last update must be confusing. This morning, while investigating, I was able to reproduce the error using a simple got request (i.e. not using .paginate())

Thus the issue can be simplified to a crash in .json() execution of a got request within an afterResponse hook (with agent keepAlive)

The issue occurs with any got@11 version (including last night master)

I opened #1186 to distinguish the two issues.

Can you sketch an example of this one? I’ll check this out tomorrow anyway.

Too busy and stressed at the moment to work on it. Maybe later next week. 😕