aws-sdk-js-v3: [Bug client-s3]: MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 close listeners added to [TLSSocket]. Use emitter.setMaxListeners() to increase limit

Checkboxes for prior research

Describe the bug

I was following this guide: https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/javascriptv3/example_code/nodegetstarted

and I got the following warning: MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 close listeners added to [TLSSocket]. Use emitter.setMaxListeners() to increase limit.

The code succeeded though. Is the warning critical?

SDK version number

@aws-sdk/client-s3@3.370.0

Which JavaScript Runtime is this issue in?

Node.js

Details of the browser/Node.js/ReactNative version

v20.4.0

Reproduction Steps

Follow the instruction in this README file.

Observed Behavior

Warning message: (node:35080) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 close listeners added to [TLSSocket]. Use emitter.setMaxListeners() to increase limit (Use node --trace-warnings ... to show where the warning was created)

Expected Behavior

No warning

Possible Solution

The client-s3 module might be leaking memory.

This is a minimal code to reproduce the issue:

import { CreateBucketCommand, S3Client } from '@aws-sdk/client-s3'
const s3Client = new S3Client({ region: 'us-east-1' })

const data = await s3Client.send(
  new CreateBucketCommand({ Bucket: 'PUT_BUCKET_NAME_HERE' })
)
console.log(data)

You can try to run it and will see the warning even if the bucket creation succeeds.

Additional Information/Context

No response

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 7
  • Comments: 16 (2 by maintainers)

Most upvoted comments

I am also facing the same issue with Node v20.7.0

(node:15116) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 close listeners added to [TLSSocket]. Use emitter.setMaxListeners() to increase limit

A year since this issue was opened, and the issue is still there with the latest NPM install and Node 20.

facing the same issue with Node v20.9.0

@yenfryherrerafeliz I’m still getting the error:

(node:20971) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 close listeners added to [TLSSocket]. Use emitter.setMaxListeners() to increase limit
    at _addListener (node:events:588:17)
    at TLSSocket.addListener (node:events:606:10)
    at Readable.on (node:internal/streams/readable:904:35)
    at TLSSocket._wrapHandle (node:_tls_wrap:674:8)
    at TLSSocket.reinitializeHandle (node:_tls_wrap:683:22)
    at internalConnectMultiple (node:net:1123:30)
    at Timeout.internalConnectMultipleTimeout (node:net:1687:3)
    at listOnTimeout (node:internal/timers:575:11)
    at process.processTimers (node:internal/timers:514:7)

This is the code I tried:

// test-s3-upload.js
import { PutObjectCommand, S3Client } from '@aws-sdk/client-s3';

const client = new S3Client({
  region: '<region>',
  credentials: {
    accessKeyId: '<ID>',
    secretAccessKey: '<KEY>',
  },
});

//Upload a file to s3
const run = async () => {
  const buffer = Buffer.from('Hello World!');

  try {
    const data = await client.send(
      new PutObjectCommand({
        Bucket: '<SOME_BUCKET>',
        Key: 'hello.txt',
        Body: buffer,
      }),
    );
    console.log('Success', data);
  } catch (err) {
    console.log('Error', err);
  }
};

run();

Then run with:

node --trace-warnings test-s3-upload.js

Versions: @aws-sdk/client-s3: v3.398.0 node: v20.5.1

UPDATE: When testing on Node.js version 18, no problems were encountered. Not sure if this will help!

Hello, the same problem with Node v20.9.0 in:

await App.s3Client.send(new PutObjectCommand({ Body: blob, Bucket: bucket, Key: filename}));

I have the same exact error, but I’m not using AWS. I’m doing an npm install. Maybe some package (or npm itself?) is trying to use network during install and causing the warning.

It seems harmless though. Like, even if code is written properly, if some code is, for example, sending more than 10 requests and has properly added more than 10 event handlers for each of those requests (just guessing here) then it is plausible to see this error, but it won’t actually mean anything is wrong.

This is as if a DOM API like window.addEventListener('whatever', listener) would automatically log a warning to console after an arbitrary 10 event listeners were added, even if they were all totally valid listeners.

There’s probably nothing to do here, and just hope the codes that are adding listeners (could be more than one package in the system, so it could add up) are properly removing listeners when they’re done.

If you are the one adding listeners in your own code, then this warning could actually be useful if you didn’t expect listeners to keep growing.

This warning is probably just useless noise most likely.