serverless-python-requirements: Cannot Deploy; EMFILE: too many open files

Are you certain it’s a bug?

  • Yes, it looks like a bug

Is the issue caused by a plugin?

  • It is not a plugin issue

Are you using the latest version?

  • Yes, I’m using the latest version

Is there an existing issue for this?

  • I have searched existing issues, it hasn’t been reported yet

Issue description

When running serverless deploy through CLI, I get some variation of the below error.

 Serverless Error ----------------------------------------

  Cannot read file node_modules\rxjs\operator\groupBy.d.ts due to: EMFILE: too many open files, open 'C:\Users\gcper\Code\190\Website\lambda\node_modules\rxjs\operator\groupBy.d.ts'

I cannot deploy to my lambda functions.

Service configuration (serverless.yml) content

service: team190

frameworkVersion: '2'

provider:
  name: aws
  runtime: python3.8
  iamRoleStatements:
    - Effect: "Allow"
      Action:
        - dynamodb:Query
        - dynamodb:Scan
        - dynamodb:GetItem
        - dynamodb:PutItem
        - dynamodb:UpdateItem
        - dynamodb:DeleteItem
      Resource: "*"
plugins:
  - serverless-python-requirements


custom:
  pythonRequirements:
    dockerizePip: non-linux

functions:
  login:
    handler: handler/handler.login
  choose:
    handler: handler/handler.assign_role
  request:
    handler: handler/student_handler.request
  get_requests:
    handler: handler/uber_handler.get_requests
  get_records:
    handler: handler/handler.get_records
  confirm_requests:
    handler: handler/uber_handler.confirm_requests
  create_poll:
    handler: handler/uber_handler.create_poll
  get_users:
    handler: handler/uber_handler.get_all_users
  create_event:
    handler: handler/uber_handler.create_event
  get_pending_requests:
    handler: handler/student_handler.get_pending_requests
  delete_pending_requests:
    handler: handler/student_handler.delete_pending_requests

Command name and used flags

serverless deploy

Command output

Serverless: Running "serverless" installed locally (in service node_modules)
Serverless: Deprecation warning: Resolution of lambda version hashes was improved with better algorithm, which will be used in next major release.
            Switch to it now by setting "provider.lambdaHashingVersion" to "20201221"
            More Info: https://www.serverless.com/framework/docs/deprecations/#LAMBDA_HASHING_VERSION_V2
Serverless: Generated requirements from C:\Users\gcper\Code\190\Website\lambda\requirements.txt in C:\Users\gcper\Code\190\Website\lambda\.serverless\requirements.txt...
Serverless: Using static cache of requirements found at C:\Users\gcper\AppData\Local\UnitedIncome\serverless-python-requirements\Cache\b729430ab9c5421667932cf132fc2b7c75df8e7e25338978deedaf75b5fec293_slspyc ...
Serverless: Packaging service...
Serverless: Excluding development dependencies...

 Serverless Error ----------------------------------------

  Cannot read file node_modules\rxjs\operator\groupBy.d.ts due to: EMFILE: too many open files, open 'C:\Users\gcper\Code\190\Website\lambda\node_modules\rxjs\operator\groupBy.d.ts'

  Get Support --------------------------------------------
     Docs:          docs.serverless.com
     Bugs:          github.com/serverless/serverless/issues
     Issues:        forum.serverless.com

  Your Environment Information ---------------------------
     Operating System:          win32
     Node Version:              14.16.1
     Framework Version:         2.64.1 (local)
     Plugin Version:            5.5.0
     SDK Version:               4.3.0
     Components Version:        3.17.1

Environment information

Serverless: Running "serverless" installed locally (in service node_modules)
Framework Core: 2.64.1 (local)
Plugin: 5.5.0
SDK: 4.3.0
Components: 3.17.1

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 8
  • Comments: 30 (6 by maintainers)

Most upvoted comments

@medikoo … have been able to work around it with either one or both of the following plugins: serverless-plugin-common-excludes and serverless-plugin-include-dependencies.

Just installing serverless-plugin-common-exludes worked for me. Thanks! 🙇

Hello, I got the EMFILE error when launching the serverless “deploy” script. I used graceful-fs like this to solve the issue :

  1. install graceful-fs: npm install graceful-fs
  2. go to file: node_modules/serverless/lib/plugins/package/lib/zip-service.js
  3. replace this line : const fs = BbPromise.promisifyAll(require('fs')); by this:
var realFs = require('fs')
var gracefulFs = require('graceful-fs')
gracefulFs.gracefulify(realFs)
const fs = BbPromise.promisifyAll(realFs);

@medikoo Looks like this is still an issue – I’ve run into a couple of times before and have been able to work around it with either one or both of the following plugins: serverless-plugin-common-excludes and serverless-plugin-include-dependencies.

However, this issue seems to just randomly come up when trying to run sls deploy. Have you been able to look into why this issue still persists despite saying multiple times that it should be fixed next release or that it’s currently fixed? Because, truth be told it is not fixed yet.

As I was writing this comment, I was JUST able to get past the problem by running npm install serverless-plugin-include-dependencies@4.1.0 specifically. I was using this package at version 5.0.0 earlier with no luck. However, my project is now running properly using version 4.1.0.

I hope this helps someone else who is experiencing this very frustrating issue with serverless framework!

Delete _node_modules _, package.lock.json file and .serverless . after that reinstall all dependencies by npm install. and problem fixed.

A colleague saw the EMFILE: too many files open error recently, and the error was only showing up on their computer. It took a while for us to figure it out what the issue was, but it ended up being a package glob issue.

We had manually listed exclude glob patterns in the serverless.yml like this:

package:
  individually: true
  exclude:
    - .git/**
    - node_modules/**
    - .venv/**

The colleague had created their virtual environment in a directory called venv instead of .venv, so serverless didn’t know to skip their virtual environment when packaging the lambda. It was trying to read everything in their virtual environment which blew up the memory.

We updated the glob pattern to the following:

package:
  individually: true
  include:
    - '!*'
    - '!**'
    - src/*.py

This pattern prevents anything other than python files in the src dir from getting packaged. It turns out we got speed benefits with this new syntax as well, as the glob pattern is much faster when you use ! in an include rather than listing files in the exclude because the glob takes a while to find every matching file that you want to exclude.

@medikoo I don’t believe that’s the case - this issue can be encountered without having serverless-python-requirements installed. It seems more closely related to the combination of serverless with a virtual environment in the directory being packaged.

FWIW - I was/am experiencing this with Python. I created a virtual environment for each Lambda handler - and it looks like serverless is including all of the /Lib and /Scripts directories for each virtual environment, thereby inflating the file count needed to package.

Adding exclusions “solved” this for me.

A colleague saw the EMFILE: too many files open error recently, and the error was only showing up on their computer. It took a while for us to figure it out what the issue was, but it ended up being a package glob issue.

We had manually listed exclude glob patterns in the serverless.yml like this:

package:
  individually: true
  exclude:
    - .git/**
    - node_modules/**
    - .venv/**

The colleague had created their virtual environment in a directory called venv instead of .venv, so serverless didn’t know to skip their virtual environment when packaging the lambda. It was trying to read everything in their virtual environment which blew up the memory.

We updated the glob pattern to the following:

package:
  individually: true
  include:
    - '!*'
    - '!**'
    - src/*.py

This pattern prevents anything other than python files in the src dir from getting packaged. It turns out we got speed benefits with this new syntax as well, as the glob pattern is much faster when you use ! in an include rather than listing files in the exclude because the glob takes a while to find every matching file that you want to exclude.

It worked like a charm 🥇

Same here:

Cannot read file node_modules\rxjs\dist\types\internal\operators\sequenceEqual.d.ts due to: EMFILE: too many open files, open 'C:\Users\...\Desktop\...\...\node_modules\rxjs\dist\types\internal\operators\sequenceEqual.d.ts'

can someone look at this issue?

So i closed my project on VS code, and opened a terminal window in the project directory. I Ran sls deploy and everything worked fine. Could it possibly be a compatibility issue with VS code ?

@medikoo Checking now if I can replicate it if I roll back my project a few commits…

Yup… replicable. And I can trigger it without any Serverless Plugins at all:

org: bananaweb
app: bananarego
service: ${file(./config.js):common.service}
frameworkVersion: '2'
provider:
  name: aws
  runtime: nodejs14.x
  region: us-west-2
functions:
  api:
    handler: api/index.apiHandler

That api/index.js file can be empty, BTW. Nothing in there affects what I’m experiencing.

Currently looking through the package.json to see if anything in there affects it. Stripping the serverless-plugins from devDependencies (with clean npm install of course) … no effect. Stripping ALL the other devDependencies… also no effect.

So… starting to strip out dependency dependencies… also no effect. Well this is annoying.

{
  "name": "bananarego",
  "version": "0.0.0-alpha",
  "private": true,
  "dependencies": {
    "serverless": "^2.65.0"
  },
  "devDependencies": {
  }
}

Is it a problem with Serverless 2.65? If it is… 2.72.2 has the same exact bug in it. So that’s not it probably. So far all we’ve managed to change is which node module shows up in the error message.

Serverless Error ---------------------------------------- Cannot read file node_modules\archiver-utils\node_modules\safe-buffer\index.js due to: EMFILE: too many open files, open ‘C:\Users.…\node_modules\archiver-utils\node_modules\safe-buffer\index.js’

I guess I’ll start randomly removing files from the the project’s base folder… Maybe it’s trying to include something weird like include the frontend that’s explicitly blocked in the original code.

Serverless Error ----------------------------------------

Cannot read file node_modules\aws-sdk\lib\services\cloudfront.d.ts due to: EMFILE: too many open files, open ‘C:\Users.…\node_modules\aws-sdk\lib\services\cloudfront.d.ts’

Okay… wiped the project down to package-lock.json, package.json, api/index.js, and node_modules containing only a serverless v2.27.2 install…

Serverless Error ----------------------------------------

Cannot read file node_modules\kafka-node\node_modules\debug.travis.yml due to: EMFILE: too many open files, open ‘C:\Users.…\node_modules\kafka-node\node_modules\debug.travis.yml’

Wow. Seriously? Still broken? Am I doing something crazy? Okay fine… blow away the package-lock and package.json just in case… so bare node_modules with an api/index.js (empty) and the serverless.yml above.

Serverless Error ----------------------------------------

Cannot read file node_modules@sindresorhus\is\dist\index.d.ts due to: EMFILE: too many open files, open ‘C:\Users.…\node_modules@sindresorhus\is\dist\index.d.ts’

Still broken… Last shot. Gonna try it all in a completely different folder on a completely different hard drive. I’m also going to start from scratch in a totally new folder.

Serverless Error ----------------------------------------

Cannot read file node_modules\traverse\examples\stringify.js due to: EMFILE: too many open files, open ‘C:\Users.…\node_modules\traverse\examples\stringify.js’

Still? I’m not nuts am I? Okay… let’s get out of the Users folder entirely. Maybe Windows is doing something weird like trying to sync OneDrive (even though I’m pretty sure that second SSD isn’t sunk). Oh also, I figured out you don’t actually need that api/index.js file. It’ll still break on “Excluding development dependencies…”

Serverless Error ----------------------------------------

Cannot read file node_modules\d\node_modules\type\CHANGELOG.md due to: EMFILE: too many open files, open ‘C:\test\node_modules\d\node_modules\type\CHANGELOG.md’

Okay… I’m at a total loss. All I can think of is that a bug got introduced to Serverless at some point. That or I’m a bizarre edge case.