Rocket.Chat icon indicating copy to clipboard operation
Rocket.Chat copied to clipboard

File handle leak?

Open codyaray opened this issue 10 years ago • 6 comments

Your Rocket.Chat version: 0.41.0

We've run out of open file handles a couple times now (on two different hosts running Rocket.Chat). We notice because nginx starts returning a 502 Bad Gateway error. Here's the kind of messages that appear in the logs when this occurs.

cordova.coffee:153 ^[[31mSystem ➔ error^[[39m Error sending push to gateway (5 try) -> { [Error: getaddrinfo EMFILE gateway.rocket.chat:443]
  code: 'EMFILE',
  errno: 'EMFILE',
  syscall: 'getaddrinfo',
  hostname: 'gateway.rocket.chat',
  host: 'gateway.rocket.chat',
  port: 443 }
{"line":"419","file":"webapp_server.js","message":"Error serving static file Error: EMFILE: too many open files, open '/alldata/home/rocketchatsvcs/Rocket.Chat.041/programs/web.browser/app/images/logo/icon.svg'","time":{"$date":1478547106897},"level":"error"}
cordova.coffee:153 ^[[31mSystem ➔ error^[[39m Error sending push to gateway (0 try) -> { [Error: getaddrinfo EMFILE gateway.rocket.chat:443]
  code: 'EMFILE',
  errno: 'EMFILE',
  syscall: 'getaddrinfo',
  hostname: 'gateway.rocket.chat',
  host: 'gateway.rocket.chat',
  port: 443 }

If we just count the number of open files we see that there are a lot open.

cray@rocketchat:~$ sudo lsof | wc -l
176305

Looking through them, we notice that most of them are of the form "/var/tmp/{user.name}.jpg", and many of them are marked as deleted too.

cray@rocketchat:~$ sudo lsof | tail -10
<snip>
node       1687  1970           root  242r      REG              252,1       4843      12355 /var/tmp/<user.name1>.jpg
node       1687  1970           root  243r      REG              252,1       3724       3705 /var/tmp/<user.name2>.jpg
node       1687  1970           root  244r      REG              252,1       4191      10750 /var/tmp/<user.name3>.jpg (deleted)
node       1687  1970           root  245r      REG              252,1       9121      10745 /var/tmp/<user.name4>.jpg
<snip>

To confirm our casual observation, we can see how many are actually images in /var/tmp:

cray@rocketchat:~$ sudo lsof | grep "/var/tmp/" | grep jpg | wc -l
159060

And finally we can even confirm that these are from our rocketchat processes:

cray@rocketchat:~$ sudo lsof | awk '{ print $2; }' | uniq -c | sort -rn | head
  41090 1710
  41090 1697
  41090 1696
  41090 1687
   3640 1355
   2046 1516
    385 1135
    290 677
    284 1202
    260 34515
cray@rocketchat:~$ ps aux | grep 1710 | grep -v grep
root      1710  1.2  4.1 1604608 335912 ?      Sl   Oct10 524:53 /alldata/local/bin/node /alldata/home/rocketchatsvcs/Rocket.Chat.041/main.js

When this happens, it can be fixed by bouncing the rocketchat process.

for i in 1 2 3 4; do sudo service rocketchat$i restart; sleep 5; done

We eventually specified an increased ulimit in the upstart job.

limit nofile 1000000 1048576

We can look at the box with the increased ulimit currently.

cray@rocket2:~$ sudo lsof | wc -l
317703
cray@rocket2:~$ sudo lsof | awk '{ print $2; }' | uniq -c | sort -rn | head
  83800 19748
  78230 19895
  73760 19822
  71560 19953
   4340 1296
   2535 9241
    480 13133
    370 29629
    312 25180
    185 1356
cray@rocket2:~$ ps aux | egrep "19748|19895|19822|19953"
rocketc+ 19748  2.3  4.0 1632604 318044 ?      Sl   Oct24 479:40 /alldata/usr/local/bin/node /alldata/Rocket.Chat.0.41/main.js
rocketc+ 19822  2.7  4.3 1660576 334516 ?      Sl   Oct24 556:57 /alldata/usr/local/bin/node /alldata/Rocket.Chat.0.41/main.js
rocketc+ 19895  2.3  4.0 1612896 318820 ?      Sl   Oct24 472:59 /alldata/usr/local/bin/node /alldata/Rocket.Chat.0.41/main.js
rocketc+ 19953  2.3  3.7 1587716 291124 ?      Sl   Oct24 471:41 /alldata/usr/local/bin/node /alldata/Rocket.Chat.0.41/main.js

codyaray avatar Nov 07 '16 20:11 codyaray

same here, 0.46

ghost avatar Dec 02 '16 14:12 ghost

Same in the latest release.

Eh1k0 avatar May 30 '17 05:05 Eh1k0

are you guys still experiencing this? If so please re-open

geekgonecrazy avatar Jul 23 '17 01:07 geekgonecrazy

I just faced this issue with file-uploads on local filesystem. It seems like RC is not properly closing file descriptors after uploading files to the filesystem. Causing Too many open files errors if ulimit (max files) is reached. Can somebody please take a look at:

https://github.com/RocketChat/Rocket.Chat/blob/develop/packages/rocketchat-file-upload/server/config/FileSystem.js

I am not a node developer, but the get callback FileUploadClass looks like the file-handles are only closed on exception (in the catch block).

Thank you guys!

darnux avatar Oct 20 '17 09:10 darnux

There is workaround on it. You need to specify limits in systems units or in pm2.

Ex:

LimitNOFILE=1024000

Eh1k0 avatar Oct 20 '17 09:10 Eh1k0

rocket.chat is still leaking file handles. I have rocket.chat 2.3.0 with local FS file storage for file uploads. Most of the open file handles are uploaded files in the local FS.

photoninger avatar Dec 16 '19 09:12 photoninger