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

[BUG] EMFILE error in environment with low file descriptors limit #261

Open
1 task done
rschick opened this issue Feb 5, 2024 · 3 comments
Open
1 task done

[BUG] EMFILE error in environment with low file descriptors limit #261

rschick opened this issue Feb 5, 2024 · 3 comments
Labels
Bug Needs Triage needs an initial review

Comments

@rschick
Copy link

rschick commented Feb 5, 2024

Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

EMFILE error may be thrown during npm install, depending on the allowed file descriptors and the state of the cache before the install. An example error:

Error: EMFILE: too many open files, open '/Users/<user>/.npm/_cacache/index-v5/64/ee/136420e5adf6592619d25b411c7849220f30364ed8ba96dea19887a5d1f2'

Expected Behavior

npm install should succeed.

Steps To Reproduce

  1. Create a project directory and add package.json with the following:
{
  "name": "nuxt-app",
  "devDependencies": {
    "nuxt": "^3.7.0"
  }
}

(The error happens reliably with nuxt, but it's not related to nuxt, you can use other packages and get the same result.)

  1. Set a low file descriptor limit using ulimit -Hn 128 (It's possible to get the error with a higher ulimit, but using a low value helps to reliably reproduce the error)
  2. Delete the global cacache, eg: rm -rf /<home-dir>/.npm/_cacache/
  3. Delete the project's node_modules folder: rm -rf node_modules
  4. Run npm install
  5. EMFILE error is thrown

(Reset ulimit using ulimit -Hn unlimited)

Environment

  • npm: 10.4.0
  • Node: 20.11.0
  • OS: MacOS 14.1.2
  • platform: Macbook Pro 2019 (Intel)
@rschick rschick added Bug Needs Triage needs an initial review labels Feb 5, 2024
@rschick
Copy link
Author

rschick commented Feb 5, 2024

I'm not familiar with the npm codebase, but I was able to fix this issue by adding retries here: https://github.com/npm/cacache/blob/main/lib/put.js#L69. I'd be happy to submit a PR if you think this is a viable approach.

@tamoreton
Copy link

I'm not familiar with the npm codebase, but I was able to fix this issue by adding retries here: https://github.com/npm/cacache/blob/main/lib/put.js#L69. I'd be happy to submit a PR if you think this is a viable approach.

What about updating the library to use graceful-fs instead of fs? I think that essentially does the same thing – adds a backoff to prevent EMFILE errors.

I don't know if there is a performance reason cacache is using the lower-level fs API – it does have ~29 million weekly downloads and needs to be "really fast".

@k2s
Copy link

k2s commented Mar 1, 2024

I fixed this issue by using fs-extra package instead of fs/promises.
It uses graceful-fs internally and provides patched functions with support for promises.
This fixes EMFILE, also EAGAIN and potential problems with antivirus software.

There is also example of custom implementation using only p-limit package prismicio/slice-machine#1192.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Needs Triage needs an initial review
Projects
None yet
Development

No branches or pull requests

3 participants