Skip to content

Event listeners should be cleaned up (MaxListenersExceededWarning - possible memory leak detected) #597

@prokopsimek

Description

@prokopsimek

We develop an app called DX Scanner using npm-check-updates to detect if the user has updated all dependencies or not. The DX Scanner can scan many repositories/projects simultaneously (see Promise.all) (ncu.run call).

I run the DX Scanner with a cmd yarn start https://github.com/yarnpkg/yarn and the app throws a warning
MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 unhandledRejection listeners added to [process]. Use emitter.setMaxListeners() to increase limit
so I was looking for the reason. The reason is

process.on('unhandledRejection')

here: https://github.com/tjunnone/npm-check-updates/blob/50b444ebec7f970bef34cb6d36eb1ace821116d0/lib/npm-check-updates.js#L389-L391
It can happen that more than 20 parallel scans run at the same time so the ncu runs out all 11 event listeners.

More about the behavior of the process.on in a for loop: https://stackoverflow.com/a/38225520/10826693 and https://stackoverflow.com/a/44446859/10826693.
I tried to change .on to .once but it didn't help.

I really don't want to set maxListeners to a higher value. I believe that it has a good reason to be set just for 11.

I was searching for some solutions and I have found an article about the removal of all listeners.

I would recommend to add process.removeAllListeners('uncaughtException'); to the end of run function in npm-check-updates but I am not sure about it. It's not possible to guarantee that all uncaughtException in a process is really from ncu.

I tried to remove those 3 lines in my node_modules/npm-check-updates folder and it works correctly.

Do you have any other idea how to solve this issue on the side of npm-check-updates?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions