Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[HOLD for payment 2023-10-10] [$500] MaxListenersExceededWarning: Possible EventEmitter memory leak detected #26986

Closed
marcaaron opened this issue Sep 7, 2023 · 36 comments
Assignees
Labels
Awaiting Payment Auto-added when associated PR is deployed to production Bug Something is broken. Auto assigns a BugZero manager. External Added to denote the issue can be worked on by a contributor Weekly KSv2

Comments

@marcaaron
Copy link
Contributor

marcaaron commented Sep 7, 2023

This has been showing for over 2 months on dev. Can we fix it?

events.js:46 MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 onKeyCommand listeners added. Use emitter.setMaxListeners() to increase limit
    at _addListener (webpack-internal:///./node_modules/events/events.js:211:15)
    at EventEmitter.addListener (webpack-internal:///./node_modules/events/events.js:227:10)
    at EventEmitter.addListener (webpack-internal:///./node_modules/react-native-key-command/lib/module/KeyCommand/index.js:20:16)
    at Module.addListener (webpack-internal:///./node_modules/react-native-key-command/lib/module/index.js:110:30)
    at eval (webpack-internal:///./src/libs/KeyboardShortcut/index.js:84:57)
    at Function.each (webpack-internal:///./node_modules/underscore/modules/each.js:26:7)
    at eval (webpack-internal:///./src/libs/KeyboardShortcut/index.js:77:52)
    at ./src/libs/KeyboardShortcut/index.js (http://localhost:8082/main-8a767871f57b57b1baee.bundle.js:8113:1)
    at __webpack_require__ (http://localhost:8082/runtime-17296f4671db2b0d6f5f.bundle.js:36:33)
    at fn (http://localhost:8082/runtime-17296f4671db2b0d6f5f.bundle.js:352:21)

2023-09-07_09-50-01

Upwork Automation - Do Not Edit
  • Upwork Job URL: https://www.upwork.com/jobs/~01961f084c190662b2
  • Upwork Job ID: 1701152779444531200
  • Last Price Increase: 2023-09-11
  • Automatic offers:
    • hungvu193 | Contributor | 26743727

Slack convo if necessary: https://expensify.slack.com/archives/C049HHMV9SM/p1694421370612589

@marcaaron marcaaron added Daily KSv2 Bug Something is broken. Auto assigns a BugZero manager. labels Sep 7, 2023
@melvin-bot
Copy link

melvin-bot bot commented Sep 7, 2023

Triggered auto assignment to @garrettmknight (Bug), see https://stackoverflow.com/c/expensify/questions/14418 for more details.

@melvin-bot
Copy link

melvin-bot bot commented Sep 7, 2023

Bug0 Triage Checklist (Main S/O)

  • This "bug" occurs on a supported platform (ensure Platforms in OP are ✅)
  • This bug is not a duplicate report (check E/App issues and #expensify-bugs)
    • If it is, comment with a link to the original report, close the issue and add any novel details to the original issue instead
  • This bug is reproducible using the reproduction steps in the OP. S/O
    • If the reproduction steps are clear and you're unable to reproduce the bug, check with the reporter and QA first, then close the issue.
    • If the reproduction steps aren't clear and you determine the correct steps, please update the OP.
  • This issue is filled out as thoroughly and clearly as possible
    • Pay special attention to the title, results, platforms where the bug occurs, and if the bug happens on staging/production.
  • I have reviewed and subscribed to the linked Slack conversation to ensure Slack/Github stay in sync

@akinwale
Copy link
Contributor

akinwale commented Sep 7, 2023

Proposal

Please re-state the problem that we are trying to solve in this issue.

"MaxListenersExceededWarning: Possible EventEmitter memory leak detected" warning showing up on dev.

What is the root cause of that problem?

This is due to the number of keyboard shortcuts being setup exceeding the default number of max listeners.

What changes do you think we should make in order to solve the problem?

Increase the number of max listeners by setting the EventEmitter's defaultMaxListeners value. This can be done in the KeyboardShortcut/index.js file before the _.each iterator that sets up the shortcuts.

  1. Import EventEmitter.
import EventEmitter from 'events';
  1. Set the value for defaultMaxListeners.
EventEmitter.defaultMaxListeners = Object.keys(CONST.KEYBOARD_SHORTCUTS).length;

We can also consider setting the value to be higher than this if required (+5 extra or double the length?) in cases where we try to set up more keyboard shortcuts so the warning does not show up in those instances.

Finally, if we don't care about having a limit, we can set it to Infinity.

EventEmitter.defaultMaxListeners = Infinity;

What alternative solutions did you explore? (Optional)

None.

@neonbhai
Copy link
Contributor

neonbhai commented Sep 7, 2023

Proposal

Please re-state the problem that we are trying to solve in this issue.

"MaxListenersExceededWarning: Possible EventEmitter memory leak detected" warning showing up on dev.

What is the root cause of that problem?

The default value of maxEventListeners is 10. We currently have 11 event listeners on general app usage

What changes do you think we should make in order to solve the problem?

We should increase the ceiling of defaultMaxEventListeners.
In App.js, we should add:

...
// Import defaultMaxListeners from events module 
import {defaultMaxListeners} from 'events';

// Set defaultMaxListeners 
defaultMaxListeners = 20;

Let's not set it to infinity as this takes away the ability for us to identify emitter memory leaks.

What alternative solutions did you explore? (Optional)

xx

@melvin-bot
Copy link

melvin-bot bot commented Sep 11, 2023

@garrettmknight Uh oh! This issue is overdue by 2 days. Don't forget to update your issues!

@melvin-bot melvin-bot bot added the Overdue label Sep 11, 2023
@melvin-bot
Copy link

melvin-bot bot commented Sep 11, 2023

@garrettmknight Whoops! This issue is 2 days overdue. Let's get this updated quick!

@garrettmknight garrettmknight added the External Added to denote the issue can be worked on by a contributor label Sep 11, 2023
@melvin-bot melvin-bot bot changed the title MaxListenersExceededWarning: Possible EventEmitter memory leak detected [$500] MaxListenersExceededWarning: Possible EventEmitter memory leak detected Sep 11, 2023
@garrettmknight
Copy link
Contributor

I don't have a dev to test this on, but I'll believe @marcaaron. Checking whether it's External in this slack thread

@melvin-bot melvin-bot bot removed the Overdue label Sep 11, 2023
@hungvu193
Copy link
Contributor

hungvu193 commented Sep 11, 2023

Proposal

Please re-state the problem that we are trying to solve in this issue.

MaxListenersExceededWarning: Possible EventEmitter memory leak detected

What is the root cause of that problem?

We're using events for EventEmitter which limit the listener to 10.

What changes do you think we should make in order to solve the problem?

The events library is a little bit outdated, we can upgrade it to use eventemitter3 which is faster and there's no limit for listener.
https://github.com/Expensify/react-native-key-command/blob/8e91f6d9381325eacbfd316a0baed496b29a2f22/src/KeyCommand/index.js#L4

import Events from 'eventemitter3';

const EventEmitter = new Events();

I created a fork and tested, it seemed that everything worked as expected (https://github.com/hungvu193/App/tree/bump-to-ee3).

What alternative solutions did you explore? (Optional)

N/A

@b4s36t4
Copy link
Contributor

b4s36t4 commented Sep 11, 2023

Proposal

Please re-state the problem that we are trying to solve in this issue.

MaxListenersExceededWarning: Possible EventEmitter memory leak detected

What is the root cause of that problem?

We have a limit of 10 max listener for the events package and we're registering total 11 events which is raising this wanring.

What changes do you think we should make in order to solve the problem?

Instead of going with global override we can update the maxilistener count using listenerCount and setMaxListenerCount for the event emitter exported by KeyCommand package which is isolated and specific limit set to only KeyCommand package.

An example of 20 max listeners

KeyCommand.eventEmitter.setMaxListener(20)

What alternative solutions did you explore? (Optional)

Or whenever we're adding a new listener we can check the max and current listener count and increment using a threshold way.

Like if we have 15 listeners we can update it to 20 listeners etc so, Just an alternative approach minding that we might add few more listeners in future so that it won't get clogged and no need to change everytime we reach the limit.

Tested, this only overrides the listener count of KeyCommand.

@b4s36t4
Copy link
Contributor

b4s36t4 commented Sep 11, 2023

@garrettmknight if it's external, you forget to apply Help Wanted and now there's no C+ to review the proposals.

@marcaaron marcaaron added Help Wanted Apply this label when an issue is open to proposals by contributors External Added to denote the issue can be worked on by a contributor and removed External Added to denote the issue can be worked on by a contributor labels Sep 12, 2023
@melvin-bot
Copy link

melvin-bot bot commented Sep 12, 2023

Current assignee @garrettmknight is eligible for the External assigner, not assigning anyone new.

@melvin-bot
Copy link

melvin-bot bot commented Sep 12, 2023

Triggered auto assignment to Contributor-plus team member for initial proposal review - @thesahindia (External)

@thesahindia
Copy link
Member

@hungvu193's proposal looks good to me! Let's upgrade!

🎀 👀 🎀 C+ reviewed

@melvin-bot
Copy link

melvin-bot bot commented Sep 12, 2023

Triggered auto assignment to @roryabraham, see https://stackoverflow.com/c/expensify/questions/7972 for more details.

@danieldoglas
Copy link
Contributor

hey, I think we found out why this is happening.

We're adding a new event listener everytime you access a report, and never removing those. So over time, if you access 10s of reports, it will pass the default limit from the browser.

I think we need to hold this a bit

@melvin-bot melvin-bot bot added the Overdue label Sep 15, 2023
@roryabraham
Copy link
Contributor

@danieldoglas are you working on a solution in some other context where I can follow along?

@melvin-bot melvin-bot bot added Overdue and removed Overdue labels Sep 21, 2023
@melvin-bot
Copy link

melvin-bot bot commented Sep 25, 2023

@garrettmknight, @hungvu193, @roryabraham, @thesahindia Whoops! This issue is 2 days overdue. Let's get this updated quick!

@garrettmknight
Copy link
Contributor

PR is merged!

@melvin-bot melvin-bot bot removed the Overdue label Sep 25, 2023
@melvin-bot melvin-bot bot added Reviewing Has a PR in review Weekly KSv2 and removed Daily KSv2 labels Sep 28, 2023
@melvin-bot
Copy link

melvin-bot bot commented Oct 2, 2023

Based on my calculations, the pull request did not get merged within 3 working days of assignment. Please, check out my computations here:

  • when @hungvu193 got assigned: 2023-09-19 01:29:05 Z
  • when the PR got merged: 2023-10-02 04:14:29 UTC
  • days elapsed: 9

On to the next one 🚀

@melvin-bot melvin-bot bot added Weekly KSv2 Awaiting Payment Auto-added when associated PR is deployed to production and removed Weekly KSv2 labels Oct 3, 2023
@melvin-bot melvin-bot bot changed the title [$500] MaxListenersExceededWarning: Possible EventEmitter memory leak detected [HOLD for payment 2023-10-10] [$500] MaxListenersExceededWarning: Possible EventEmitter memory leak detected Oct 3, 2023
@melvin-bot
Copy link

melvin-bot bot commented Oct 3, 2023

Reviewing label has been removed, please complete the "BugZero Checklist".

@melvin-bot melvin-bot bot removed the Reviewing Has a PR in review label Oct 3, 2023
@melvin-bot
Copy link

melvin-bot bot commented Oct 3, 2023

The solution for this issue has been 🚀 deployed to production 🚀 in version 1.3.76-6 and is now subject to a 7-day regression period 📆. Here is the list of pull requests that resolve this issue:

If no regressions arise, payment will be issued on 2023-10-10. 🎊

After the hold period is over and BZ checklist items are completed, please complete any of the applicable payments for this issue, and check them off once done.

  • External issue reporter
  • Contributor that fixed the issue
  • Contributor+ that helped on the issue and/or PR

For reference, here are some details about the assignees on this issue:

As a reminder, here are the bonuses/penalties that should be applied for any External issue:

  • Merged PR within 3 business days of assignment - 50% bonus
  • Merged PR more than 9 business days after assignment - 50% penalty

@melvin-bot
Copy link

melvin-bot bot commented Oct 3, 2023

BugZero Checklist: The PR fixing this issue has been merged! The following checklist (instructions) will need to be completed before the issue can be closed:

  • [@thesahindia] The PR that introduced the bug has been identified. Link to the PR:
  • [@thesahindia] The offending PR has been commented on, pointing out the bug it caused and why, so the author and reviewers can learn from the mistake. Link to comment:
  • [@thesahindia] A discussion in #expensify-bugs has been started about whether any other steps should be taken (e.g. updating the PR review checklist) in order to catch this type of bug sooner. Link to discussion:
  • [@thesahindia] Determine if we should create a regression test for this bug.
  • [@thesahindia] If we decide to create a regression test for the bug, please propose the regression test steps to ensure the same bug will not reach production again.
  • [@garrettmknight] Link the GH issue for creating/updating the regression test once above steps have been agreed upon:

@melvin-bot melvin-bot bot added Daily KSv2 and removed Weekly KSv2 labels Oct 10, 2023
@garrettmknight
Copy link
Contributor

garrettmknight commented Oct 11, 2023

Summary of payments for this issue:

Upwork job: https://www.upwork.com/en-gb/jobs/~01961f084c190662b2

@melvin-bot melvin-bot bot added the Overdue label Oct 16, 2023
@garrettmknight
Copy link
Contributor

Dropping to weekly while @thesahindia finishes the checklist/submits for payment.

@garrettmknight garrettmknight added the Weekly KSv2 label Oct 16, 2023
@melvin-bot melvin-bot bot removed the Overdue label Oct 16, 2023
@garrettmknight garrettmknight removed the Daily KSv2 label Oct 16, 2023
@thesahindia
Copy link
Member

Just made the request.

We don't have to add any test case and we can skip the checklist. We can't call it a regression.

@garrettmknight
Copy link
Contributor

Sounds good, closing.

@JmillsExpensify
Copy link

$500 payment approved for @thesahindia based on BZ summary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting Payment Auto-added when associated PR is deployed to production Bug Something is broken. Auto assigns a BugZero manager. External Added to denote the issue can be worked on by a contributor Weekly KSv2
Projects
None yet
Development

No branches or pull requests

10 participants