@samwen/lambda-emfiles
v1.1.1
Published
solve node AWS lambda EMFILE issue
Downloads
21,267
Maintainers
Readme
lambda-emfiles
A solution to node AWS lambda EMFILE issue. If you spot following errors in your lambda function logs:
- getaddrinfo EMFILE
- EMFILE, too many open files
- with ioredis, Failed to refresh slots cache
- with mongodb, MongoError: no connection available, MongoServerSelectionError
...
etc
Most likely, it is caused by exceeding the file descriptors limit of AWS lambda.
lambda-emfiles provides solution to the problem.
how to use
install
npm install @samwen/lambda-emfiles
in your lambda code
what does it do:
- report file descriptor leaks to help debug.
- prevent it. if the file descriptors will reach the max limit in next run, it exits the process.
detail of the issue and solution
AWS lambda process runs within a docker container in Amazon Linux environment. The maximum limit on file descriptors is 1000. Normally, it is very hard for a lambda function to exceed the limit.
However, the lambda process within the container may be reused for performance optimization.
This is the reason for most cases of exceeding file descriptors limit.
Here is how it happens:
A lambda function leaks 100 file descriptors each time. It will hit the limit in abut 10 runs.
The chance that the lambda process is reused 10 times is really low.
This is why the lambda runs OK for most of times.
But you can spot few errors caused by exceeding file descriptors limit after a while, it depends on how frequently the lambda is running and the concurrency level of the lambda.
The best solution to the problem is to fix file descriptor leakage. lambda-emfiles provides report for this.
It takes time to fix file descriptor leakage, specially it works most of times.
Alternatively, lambda-emfiles calls process.exit(1) when it predicts a deficit of file descriptors in next run. Once the process is gone, it will not reused.
Tunning of parameters: max_emfiles_needed and exit_process
The 2 public methods come with default values for max_emfiles_needed and exit_process. The default values should work for most scenarios.
max_emfiles_needed: is the estimated max file descriptors will open in the same time.
exit_process: if it is true, it instructs lambda-emfiles to call process.exit(1), when it sees a deficit of file descriptors.