Skip to content

UND_ERR_SOCKET thrown when fetching certain endpoints of the GitHub API #1670

@rijkvanzanten

Description

@rijkvanzanten

Bug Description

When requesting certain endpoints of the GitHub API, undici crashes with SocketError: closed

Full Error
/path/to/node_modules/undici/lib/client.js:967
  const err = this[kError] || new SocketError('closed', util.getSocketInfo(this))
                              ^

SocketError: closed
    at TLSSocket.onSocketClose (/path/to/node_modules/undici/lib/client.js:967:31)
    at TLSSocket.emit (node:events:525:35)
    at node:net:757:14
    at TCP.done (node:_tls_wrap:584:7)
Emitted 'error' event on BodyReadable instance at:
    return super.emit(ev, ...args)
                 ^

Error
    at Object.emit (/path/to/node_modules/undici/lib/api/readable.js:66:18)
    at emitErrorNT (node:internal/streams/destroy:157:8)
    at emitErrorCloseNT (node:internal/streams/destroy:122:3)
    at processTicksAndRejections (node:internal/process/task_queues:83:21) {
  code: 'UND_ERR_SOCKET',
  socket: {
    localAddress: undefined,
    localPort: undefined,
    remoteAddress: undefined,
    remotePort: undefined,
    remoteFamily: undefined,
    timeout: undefined,
    bytesWritten: 75,
    bytesRead: 556
  }
}

I'm assuming there's a certain header or combination of response headers that's causing this behavior, but I don't know enough about the Undici codebase or http headers to flag the problem. I've copied the full raw response this endpoint gives using other API debugging tools

Response headers from aforementioned endpoint
HTTP/1.1 401 Unauthorized
Server: GitHub.com
Date: Wed, 28 Sep 2022 16:02:23 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 152
X-GitHub-Media-Type: github.v3; format=json
Access-Control-Expose-Headers: ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset
Access-Control-Allow-Origin: *
Strict-Transport-Security: max-age=31536000; includeSubdomains; preload
X-Frame-Options: deny
X-Content-Type-Options: nosniff
X-XSS-Protection: 0
Referrer-Policy: origin-when-cross-origin, strict-origin-when-cross-origin
Content-Security-Policy: default-src 'none'
Vary: Accept-Encoding, Accept, X-Requested-With
X-RateLimit-Limit: 60
X-RateLimit-Remaining: 55
X-RateLimit-Reset: 1664384459
X-RateLimit-Resource: core
X-RateLimit-Used: 5
X-GitHub-Request-Id: F68E:1A6C:47DAE65:925FC3C:63347011
connection: close

{"message":"Requires authentication","documentation_url":"https://docs.github.com/rest/reference/users#list-email-addresses-for-the-authenticated-user"}

Reproducible By

import { request } from 'undici';

const { body } = await request('https://api.github.com/user/emails');

console.log(await body.json());

on the latest version at time of writing 5.10.0.

Minimal repro repo: https://github.com/rijkvanzanten/undici-socket-err

Expected Behavior

The above code snippet should console.log the JSON output of the endpoint.

Environment

macOS 12.6, Node v16.17.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Status: help-wantedThis issue/pr is open for contributionsbugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions