question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"Possible EventEmitter memory leak detected" warning coming from AWS execution environment

See original GitHub issue

Hello,

Today I noticed the following message in cloudwatch logs. It appears about 15 times a day.

ERROR	(node:8) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 beforeExit listeners added. Use emitter.setMaxListeners() to increase limit

My code (including all node_modules) doesn’t listen on the beforeExit event.

My lambda function uses aws-serverless-express and generates traffic of 918k invocations / day.

I have another lambda function that has similar traffic profile, but it’s not responding to API Gateway requests and is not using aws-serverless-express. I haven’t seen this error logged there.

According to node documentation https://nodejs.org/docs/latest-v10.x/api/process.html#process_event_beforeexit

The ‘beforeExit’ event is emitted when Node.js empties its event loop and has no additional work to schedule. Normally, the Node.js process will exit when there is no work scheduled, but a listener registered on the ‘beforeExit’ event can make asynchronous calls, and thereby cause the Node.js process to continue. The listener callback function is invoked with the value of process.exitCode passed as the only argument. The ‘beforeExit’ event is not emitted for conditions causing explicit termination, such as calling process.exit() or uncaught exceptions. The ‘beforeExit’ should not be used as an alternative to the ‘exit’ event unless the intention is to schedule additional work.

It seems like the AWS controlled code that is invoking the user provided handler functions listens on the beforeExit event. For some reason when aws-serverless-express is used the listener function is attached more than once to process. Is there anything that can be done in this library to mitigate from the issue?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
janazcommented, Jul 9, 2019

Hi Brett,

My answers are below

  1. Are you calling .listen() in your Express layer?

No.

  1. Are you initializing aws-serverless-express outside of the handler on each invocation?

Yes. (I recently reported an issue with insufficient file descriptors that was caused by initializing the server inside the handler. Now it’s definitely outside of it. I just double checked to be sure)

  1. Which runtime and resolution mode are you using (default is context.succeed)?

Runtime Node10.x. aws-serverless-express v3.3.5 I don’t specify the resolution mode, so it’s using the default.

The code looks like this (it’s TypeScript):

import awsServerlessExpress = require('aws-serverless-express');
import * as app from '../web';

const server = awsServerlessExpress.createServer(app.default);

export const handler = (event: any, context: any) => {
  return awsServerlessExpress.proxy(server, event, context);
};
0reactions
jp928commented, Jun 9, 2021

After cache the server problem solved.

Read more comments on GitHub >

github_iconTop Results From Across the Web

"Possible EventEmitter memory leak detected" warning ...
It seems like the AWS controlled code that is invoking the user provided handler functions listens on the beforeExit event. For some reason...
Read more >
Possible EventEmitter memory leak detected NodeJS
I think I found a temporary solution. The main problem is that my computer has only like 2 GB left of RAM space....
Read more >
Troubleshooting Lambda configurations
This issue affects invocations occurring in warm execution environments. For example, the following code creates a memory leak between invocations.
Read more >
How to fix possible EventEmitter memory leak detected
The warning possible EventEmitter memory leak detected happens when you have more than 10 listeners (read EventEmitter) attached to an event ...
Read more >
Process | Node.js v19.3.0 Documentation
The process object is an instance of EventEmitter . ... (node:38638) MaxListenersExceededWarning: Possible EventEmitter memory leak detected.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found