serverless: "EMFILE - too many open file" error
This is a meta-issue to gather all reports about the following error:
EMFILE - too many open files error
This is a very tough issue to solve because it happens randomly. It is caused by having too many files open on the whole machine (not just the serverless process).
A possible root cause could be synchronous usage of the fs API. A solution might be to use graceful-fs and/or the async API to access the filesystem.
We use graceful-fs and reduced the use of sync calls in Serverless Framework to a minimum, but some plugins might not. We’re trying to identify which plugins might cause the issue.
Workarounds
This isn’t ideal, but it is possible to increase the limit of files open on Linux and macOS with ulimit (https://stackoverflow.com/a/21070580/245552), but not on Windows.
If you have information about more workarounds, please post them here and we’ll update the issue.
How to help?
- Add a 👍 on this issue (so that we can estimate the impact)
- Investigate if some plugins might benefit from using
graceful-fsand/or async filesystem calls - Fill the following form and post it as a comment:
OS:
Serverless Framework version ('sls --version'):
Node version ('node --version'):
Plugins used:
Let’s try and see if there’s a pattern (e.g. a plugin that comes up often).
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 127
- Comments: 50 (4 by maintainers)
Hello, I got the EMFILE error when launching the serverless “deploy” script. I used graceful-fs like this to solve the issue :
npm install graceful-fsnode_modules/serverless/lib/plugins/package/lib/zip-service.jsconst fs = BbPromise.promisifyAll(require('fs'));by this:Thank you for continuing to investigate this. After a bunch of digging, I was able to resolve my issue by using the
serverless-plugin-common-excludesandserverless-plugin-include-dependenciesplugins. This immediately resulted in a necessary not being included, but that issue appears to be specific to theknexORM library and is probably unrelated to the issue within serverless. Ultimately, that was resolved by manually importing a sub-dependency in my own project.Update: I got to the root of the issue. Here’s the full fix for NodeJS:
yarn add -D graceful-fs@4.2.6.node_modules/serverless/bin/serverless.jsand add this right afteruse strict;:this one worked on a windows 11 machine, thank you!
serverless version: 3.25.0 nodejs: 18.12.1
@shierro @tobelesa
I tried all of the solution But still I am sruggling with the issue, Anyone have update to solve this issue? On trying with graceful lib this error occured
I had the same issue. I was able to resolve it and significantly improve packaging speeds by moving plugin packages such as
serverless-domain-managerto thedevDependenciessection of mypackage.json. Serverless excludes dev dependencies by default unlesspackage.excludeDevDependenciesis set to false.Environment: win32, node 20.10.0, framework 3.38.0 (local), plugin 7.2.0, SDK 4.5.1 Credentials: Local, “default” profile Docs: docs.serverless.com Support: forum.serverless.com Bugs: github.com/serverless/serverless/issues
I’m using windows 11 I also encountered this issue will running the serverless deploy command on windows command prompt, but when I run the same command with Git Bash it worked for me.
FWIW, I had already done this in the past and the issue came back. I took a fresh look at my zip-service.js file and it was back to original version. Re-implemented this code change and now my deploys work properly again. So, if you have already implemented graceful-fs, double check this!
Thanks for sharing this. I was able to fix this issue by using only
serverless-plugin-include-dependenciesEnvironment
@tobelesa this patch shouldn’t have any real effect, as we ensure
fsis patched globally before any modules are loaded: https://github.com/serverless/serverless/blob/ed111e021b286eb4a68f9b2fce7fb641e03340bd/scripts/serverless.js#L7-L9