question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error: EMFILE: too many open files

See original GitHub issue

Hello, how can i fix it?

[Error: EMFILE: too many open files, open 'C:\Users\HP\AppData\Roaming\gcloud\application_default_credentials.json'] {
  errno: -4066,
  code: 'EMFILE',
  syscall: 'open',
  path: 'C:\\Users\\HP\\AppData\\Roaming\\gcloud\\application_default_credentials.json'
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
bcoecommented, Dec 6, 2019

@chsnt the problem isn’t the Translate client, I believe it’s this code:

     getFiles(dir, function (err, files) {

          pbSubr.start(files.length, 0);        

            files.forEach( file => {
              fs.pathExists(file.replace('data\\boards\\', 'data\\boards-ru\\'))
              .then(exists => { 
                if(!exists) { 
                    translatorWithErrorHandle (file); 
                } else {                  
                  pbSubr.increment()
                  pbSubr.update(undefined, {dir: file.split('\\')[3]});
                }
              })
                                
            });

        });

This is going to open all files in parallel, so if there are many files in dir you might hit a maximum amount of concurrent open files at the operating system level.

I would potentially rewrite this like:

     getFiles(dir, async function (err, files) {

          pbSubr.start(files.length, 0);        
          for (const file of files) {
              const exists = await fs.pathExists(file.replace('data\\boards\\', 'data\\boards-ru\\'))
              . if(!exists) { 
                  await translatorWithErrorHandle (file); 
                } else {                  
                  pbSubr.increment()
                  await pbSubr.update(undefined, {dir: file.split('\\')[3]});
                }
              })
          } 
        });

☝️ this is pseudo code that would translate a file one at a time, assuming that pbSubr.update returns a promise.

As an alternative to async/await you might also look at p-queue which allows you to control the amount of concurrency happening in your program.

In general, you don’t want to perform a large amount of async work in a loop, because you’ll create a lot of open handles at once.

0reactions
chsntcommented, Dec 6, 2019

@bcoe Thanks for an answer I use translate like this:

const translateG = require('./translate');
...
translateG (text, targetLang)  // multiple call (its wrong?)
...

(full code https://gist.github.com/chsnt/0d669b0a9509086dce829f8d0ae7257c )

translate.js

const {Translate} = require('@google-cloud/translate').v2;
const translate = Translate({projectId: 'your-project-id'})
const tr = (text, target) => {
  return new Promise ((resolve, reject)=>{
    translate.translate(text, target)
    .then(([result])=>resolve(result))
    .catch(err=>{
        console.error(err); 
        reject(err)
    })
  })      
}

module.exports = tr;
Read more comments on GitHub >

github_iconTop Results From Across the Web

node and Error: EMFILE, too many open files - Stack Overflow
After a number of searches I found a work around for the "too many open files" problem: var requestBatches = {}; function batchingReadFile(filename,...
Read more >
EMFILE: too many open files, watch · Issue #923 - GitHub
i'm facing below issue while generating archive in xcode node:events:371 throw er; // Unhandled 'error' event ^ Error: EMFILE: too many open ......
Read more >
How to fix the: "EMFILE: too many open files, watch" error in ...
A quick guide to how I solved a confusing React Native error. ... How to fix the: "EMFILE: too many open files, watch"...
Read more >
SFDX Deploy error EMFILE: too many open files
SFDX Deploy error EMFILE: too many open files. Salesforce DX. Last updated 2021-10-12 ·Reference W-7848515 ·Reported By 73 users.
Read more >
Backup failure due to OS error EMFILE "Too many open files"
Various symptoms may happen due to OS error EMFILE "Too many open files". Backup failure is one of them. You may see following...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found