Skip to content

MaxListenersExceededWarning: Possible EventEmitter memory leak detected. #1295

@tamtamchik

Description

@tamtamchik

Hi there,

I think there is a regression in version 3.0.

When I'm using Custom Agent (https) with keepAlive: true option and then just make a bunch of requests. Pretty soon such warnings show up:

(node:24083) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 data listeners added to [TLSSocket]. Use emitter.setMaxListeners() to increase limit
MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 data listeners added to [TLSSocket]. Use emitter.setMaxListeners() to increase limit
at _addListener (node:events:469:17)
at TLSSocket.addListener (node:events:491:10)
at TLSSocket.Readable.on (node:internal/streams/readable:876:35)
at ClientRequest. (.../node_modules/node-fetch/src/index.js:316:10)
at ClientRequest.emit (node:events:394:28)
at tickOnSocket (node:_http_client:755:7)
at onSocketNT (node:_http_client:815:5)
at processTicksAndRejections (node:internal/process/task_queues:84:21)

The same thing with http, just listeners added not to TLSSocket but to just Socket.

Reproduction

Here is a really simple example.

import https from 'node:https'
import fetch from 'node-fetch'

const agent = new https.Agent({ keepAlive: true })

async function start () {
  for (let i = 1; i < 1000; i++) {
    await fetch('https://example.com', { agent })
  }
}

process.on('warning', e => console.warn(e.stack))

start().catch(e => console.error(e))

Steps to reproduce the behavior:

  1. Run this code with node-fetch@3 installed and you will start getting warnings.
  2. Install node-fetch@2 as a dependency, run the same code, and boom, no warnings.

Expected behavior

Version 2.6.2 does not produce any warning on the same code.
Version 3.0.0 produces warnings.

Your Environment

I have been running this on node.js 14 (LTS) and 16 (latest) with the same results.

software version
node-fetch 3.0.0
node v14.17.6 & v16.9.1
npm 6.14.15 & 7.21.1
Operating System MacOS 11.5.2 (20G95) & Docker with alpine linux

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions