question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

UND_ERR_SOCKET thrown when fetching certain endpoints of the GitHub API

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
mcollinacommented, Sep 29, 2022

I know why this is happening: the socket is getting closed before we have time to complete parsing the response. It’s a timing issue. We should defer this error if we expect the socket to be closed.

2reactions
rijkvanzantencommented, Sep 29, 2022

@mcollina Happy to try my hand at a PR if you have some pointers on where to tackle this in the codebase 🙂

Read more comments on GitHub >

github_iconTop Results From Across the Web

fetch throws UND_ERR_SOCKET error #1412 - nodejs/undici
Bug Description A request on this page of the Daily Mail throws an UND_ERR_SOCKET error. There is no problem with https.
Read more >
Troubleshooting - GitHub Docs
Learn how to resolve the most common problems people encounter in the REST API. If you're encountering some oddities in the API, here's...
Read more >
Resources in the REST API - GitHub Docs
Learn how to navigate the resources provided by the GitHub API. ... Here, we fetch the list of repositories owned by the octokit...
Read more >
Getting started with the REST API - GitHub Docs
Although some REST API operations are accessible without authentication, you must authenticate to GitHub CLI in order to use the api subcommand.
Read more >
Endpoints available for GitHub Apps
Your app can make requests to the following REST endpoints. ... get /orgs/{org}/actions/permissions/selected-actions ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found