Skip to content

dns and retry with pre-built interceptors error #3901

@haydnhkim

Description

@haydnhkim

Bug Description

When I use dns and retry with pre-built interceptors, I get no response.
If I keep the process alive as I did when using express, I get the error below.

Reproducible By

import { Agent, setGlobalDispatcher, interceptors } from 'undici';

const { dns, retry } = interceptors;
const agent = new Agent({
  connect: {
    rejectUnauthorized: false,
  },
}).compose([
  dns({
    maxTTL: 60 * 60 * 1000,
    maxItems: 10000,
  }),
  retry({
    maxRetries: 2,
    maxTimeout: 5 * 1000,
  }),
]);

setGlobalDispatcher(agent);

(async () => {
  const todo = await fetch('https://jsonplaceholder.typicode.com/todos/1')
    .then((res) => res.json())
    .catch(console.error);

  console.log(JSON.stringify(todo, null, 2));
})();

Expected Behavior

A response to the request should occur and console.log should be printed.

{
  "userId": 1,
  "id": 1,
  "title": "delectus aut autem",
  "completed": false
}

Logs & Screenshots

The error below does not always occur immediately.
They occur after a period of time after multiple requests without receiving a response.

uncaughtException: TypeError: this[#handler].onError is not a function
    at DNSDispatchHandler.onError (webpack-internal:///(rsc)/../../node_modules/.pnpm/[email protected]/node_modules/undici/lib/interceptor/dns.js:267:23)
    at Request.onError (webpack-internal:///(rsc)/../../node_modules/.pnpm/[email protected]/node_modules/undici/lib/core/request.js:301:27)
    at Object.errorRequest (webpack-internal:///(rsc)/../../node_modules/.pnpm/[email protected]/node_modules/undici/lib/core/util.js:828:13)
    at Socket.onHttpSocketClose (webpack-internal:///(rsc)/../../node_modules/.pnpm/[email protected]/node_modules/undici/lib/dispatcher/client-h1.js:939:10)
    at Socket.emit (node:events:530:35)
    at TCP.<anonymous> (node:net:337:12)
    at TCP.callbackTrampoline (node:internal/async_hooks:130:17)

  Exception origin: uncaughtException
  ⨯ ../../node_modules/.pnpm/[email protected]/node_modules/undici/lib/interceptor/dns.js (267:1) @ DNSDispatchHandler.onError
  ⨯ uncaughtException: [TypeError: this[#handler].onError is not a function]
   265 |       // eslint-disable-next-line no-fallthrough
   266 |       default:
 > 267 |         this.#handler.onError(err)
       | ^
   268 |         break
   269 |     }
   270 |   }

Environment

MacOS Sequoia 15.0
Node.js v 20.17.0
Node.js v 22.11.0

7.0.0-alpha.6 ~ 7.0.0-alpha.8 → Works fine
7.0.0-alpha.9 and later (include stable v7) → Doesn't work. No output to console.log

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