30

After opening a file with File.new(big_file) (without closing it) 1016 times (Ubuntu) or 1017 times (CentOS), it seems there is a limit and it raises:

Too many open files @ rb_sysopen - big_file (Errno::EMFILE)

Is there any way to raise that limit?

On my systems, ulimit is set to unlimited.

Victor
  • 1,680
  • 3
  • 22
  • 40
  • Check `ulimit -n` not `ulimit`. I'm willing to bet it's `1024`. – Eli Sadoff Nov 14 '16 at 20:44
  • Also, I'd recommend following some of the instructions [here](http://stackoverflow.com/questions/24862733/difference-between-linux-errno-23-and-linux-errno-24) to fix this. – Eli Sadoff Nov 14 '16 at 20:45
  • Normal filesystem limits are 1024 open filehandles, many of those already reserved or used internally by Ruby. `ulimit` settings need to be adjusted here. One way to fix this is to fuss with `/etc/sysctl.conf` and `/etc/security/limits.conf`. – tadman Nov 14 '16 at 20:48
  • 1
    @EliSadoff `ulimit -n` is indeed `1024`, you won the bet! So how can I raise that? – Victor Nov 14 '16 at 20:51
  • Either by running `sysctl -w fs.file-max=NUMBER_OF_FILES` or edit `/etc/sysctl.conf` and change the value of `fs.file-max` and then run `sysctl -p` to reload the file settings. Read [this answer](http://stackoverflow.com/a/24862823/5021321) for more details. – Eli Sadoff Nov 14 '16 at 20:52
  • @EliSadoff didn't work, see my edit. – Victor Nov 14 '16 at 21:49
  • @Victor Did you try the second part of the answer, setting it just for your user instead of system wide? That seems to work better. – Eli Sadoff Nov 14 '16 at 21:51
  • 1
    @EliSadoff It did! Thank you. I'll edit this question and make another Q/A one to separate the two different issues: The fact `Errno::EMFILE` means per process and the issue with `file-max=` not working. – Victor Nov 14 '16 at 21:59
  • @Victor how to do the same with windows ? I am having the same problem. Can you please help me out ? – Anubhi Golechha Nov 01 '17 at 12:49

1 Answers1

36
  • EMFILE is too many files opened in your process.
  • ENFILE is too many files opened in the entire system.

So Errno::EMFILE is due to the ruby process opening too many files. This limit is probably set to the default 1024 can be seen with:

$ulimit -n
1024

Instead of:

$ulimit
unlimited

You can raise the limit using this method.

Victor
  • 1,680
  • 3
  • 22
  • 40