GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — errors from node:internal leak · Issue #3987 · nodejs/undici

errors from node:internal leak #3987

New issue
Copy link
New issue
Copy link
Closed
#4472
Closed
errors from node:internal leak#3987
#4472
Copy link
Labels
bugSomething isn't working

Description

@zuozp8
zuozp8
opened on Jan 3, 2025
Issue body actions

Bug Description

When importing undici (from node_modules) after using fetch (nodejs built-in), then globalDispatcher is filled by built-in undici.
Therefore using instanceof errors.UndiciError is unreliable

Reproducible By

Promise.resolve().then(async () => {
  fetch('http://httpstat.us/200') // if fetch is commented out, then it works properly

  require('net').createServer((c) => {
    setTimeout(() => c.destroy(), 100)
  }).listen(8888)

  const { request, errors } = require('undici')
  try {
    await request('http://127.0.0.1:8888/')
  } catch (err) {
    console.info(err instanceof errors.UndiciError)
  }
  process.exit(0)
})

Expected Behavior

true would be printed, as error should be instance of SocketError that inherits UndiciError

Logs & Screenshots

Image

Environment

Checked on Node v20.13.1 and v23.5.0 using undici 7.2.0 and 5.28.4, on ubuntu 22.04 and in node:20.13-alpine3.19 container

Context

It seems natural to use instanceof, because error classes are exported. npm allows to have multiple versions of undici exported, so it's easy to have built-in undici, node_modules/undici and undici nested inside node_modules libraries.

To make it worse: ResponseStatusCodeError (which is easiest to test) is thrown outside dispatcher, so when I checked it, instanceof worked properly.

Activity

  1. zuozp8
    added
    bugSomething isn't working
    on Jan 3, 2025
  2. metcoder95 commented on Jan 5, 2025

    @metcoder95
    metcoder95
    on Jan 5, 2025
    Member
    More actions

    This is not the same, but seems to go under the same line as #3973.

    Again, don't believe is right but the behaviour seems like expected given how the globalDispatcher setter is designed.

    I'm starting to believe that we should implement a way for undici when a dispatcher is made from built-in vs node_module; otherwise this kind of problems will continue to occur.

    cc: @mcollina @ronag

  3. zuozp8 commented on Jan 7, 2025

    @zuozp8
    zuozp8
    on Jan 7, 2025
    ContributorAuthor
    More actions

    Sadly I don't see a clear solution. We could either:

    • discourage using instanceof by deprecating errors export and exporting only error codes
      • for typescript we could export interfaces instead of classes in undici-types
    • undo making dispatcher truly global
    • make errors truly global
    • move errors into dispatcher
  4. mcollina commented on Jan 7, 2025

    @mcollina
    mcollina
    on Jan 7, 2025
    SponsorMember
    More actions

    never use instanceof, it's already unreliable due to the nature of npm.

  5. zuozp8 commented on Jan 7, 2025

    @zuozp8
    zuozp8
    on Jan 7, 2025
    ContributorAuthor
    More actions

    never use instanceof

    Documentation doesn't advice against it. What would be a point of exporting Error classes from the library in the first place if developer is forbidden from using them?
    I see no other DRY (typo-proof) way of checking error kind. instanceof also serves as type-guard and simplifies typescript code.


    it's already unreliable due to the nature of npm

    Duplicating dependencies is (controversial) feature of npm that aims to improve robustness. Of course it's irritating when it causes problems, but that happens in more complex projects, usually when objects are passed from one library (with own nested dependency) to another, or when errors are caught after passing though many layers.

    In the example, request and errors are pulled together from the same undici module, but they already aren't compatible.

  6. mcollina commented on Jan 7, 2025

    @mcollina
    mcollina
    on Jan 7, 2025
    SponsorMember
    More actions

    Of course it's irritating when it causes problems, but that happens in more complex projects, usually when objects are passed from one library (with own nested dependency) to another, or when errors are caught after passing though many layers.

    Exactly. Don't use instanceof if you don't know what is going to be thrown. Note that some test libraries (like jest) even mess with Node.js internal errors. It's really unreliable.

    Documentation doesn't advice against it. What would be a point of exporting Error classes from the library in the first place if developer is forbidden from using them?

    To each its own. If someone wants to use them, it have its own uses if you can guarantee it. Anyhow, every single time we are being prescriptive, people complain.

    instanceof also serves as type-guard and simplifies typescript code.

    That's the root cause. However this is fundamentally in conflict with the nature of npm.


    Anyhow, if you would like to send a PR to improve the doc, it's 100% welcomed!
    I would love to see if there is a good solution to provide error-type safety for error codes. It's used by Node.js internals and many libraries.

  7. zuozp8
    added a commit that references this issue on Jan 19, 2025

    docs about global dispatcher and errors (nodejs#3987)

    c00e498
  8. zuozp8 commented on Jan 19, 2025

    @zuozp8
    zuozp8
    on Jan 19, 2025
    ContributorAuthor
    More actions

    I made PR to improve docs

    Type safety of errors could be achieved by exporting functions that would serve as type-guards, like

    const isUndiciError = (err: Error): err is UndiciError => {
      return err.code === 'UND_ERR' || err.code?.startsWith('UND_ERR')
    }
    
    const isConnectTimeoutError = (err: Error): err is ConnectTimeoutError => {
      return err.code === 'UND_ERR_CONNECT_TIMEOUT'
    }
  9. mcollina
    added a commit that references this issue on Jan 24, 2025

    docs: document about global dispatcher and errors (#3987) (#4014)

    5e234ac
  10. ronag
    added a commit that references this issue on Mar 14, 2025

    Merge remote-tracking branch 'nodejs/main'

    cd214d4
  11. Uzlopak
    closed this as completedin #4472on Aug 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.