Skip to content

Memory leak in Agent #4424

@hexchain

Description

@hexchain

Bug Description

There is still a memory leak in Agent.

Reproducible By

const { Agent, fetch, setGlobalDispatcher } = require('undici');

const agent = new Agent({
  keepAliveMaxTimeout: 1000,
});
setGlobalDispatcher(agent);

globalThis.agent = agent;

async function main() {
  const rs = await Promise.all([
    fetch('http://www.example.com/123'),
    fetch('http://example.com/234'),
    fetch('https://www.example.com/345'),
    fetch('https://example.com/456'),
  ]);

  console.log('done')
  setTimeout(() => undefined, 1000000)
}

void main();

Run the script with --inspect. After 'done', inspect agent[kClients]. There will be 4 items in the map.

Expected Behavior

All clients should have been destroyed.

Logs & Screenshots

Image

Environment

Undici 7.14.0, Nodejs 22.16.0.

Additional context

In Agent::[kDispatch] the key of this[kClients] is a string, but in Agent::[kOnConnect] and Agent::[kOnDisconnect] the origin is a URL object. Even if it is coerced into a string, there will be a trailing slash which makes it different from the original key.

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