Too Many Open Files in Rails – Temp Files in Mongrel

16 Mar 2009 – Denver, CO

I’ve had a whole bunch of pain the last couple of days with mongrel dumping tmp files on me and filling up my ulimit quota causing Rails to fall over.

It started when I started posting a bunch of multi-part files to Rails and letting attachment_fu do the dirty-work, but I quickly hit upon this:

 
Processing DocumentsController#create to xml ...
...

MissingSourceFile (no such file to load -- .../git/eiger/app/models/document.rb):
	.../lib/active_support/dependencies.rb:382:in `load_without_new_constant_marking'
	.../lib/active_support/dependencies.rb:382:in `load_file'
	.../lib/active_support/dependencies.rb:521:in `new_constants_in'

A quick check meant:

 
:eiger philipmcmahon$ ulimit -n
256

and

 
:~ philipmcmahon$ lsof /dev/disk0s2 | grep mongrel |  wc -l
245

philip-mcmahons-macbook-pro:~ philipmcmahon$ lsof /dev/disk0s2 | grep mongrel
ruby      25400 philipmcmahon    6u   REG   14,2    712224 13096890 
/private/var/folders/+Z/+ZLlmYL1FlmQ5osVVIedbU+++TI/-Tmp-/mongrel25400-0
ruby      25400 philipmcmahon    7u   REG   14,2    348977 13102715 
/private/var/folders/+Z/+ZLlmYL1FlmQ5osVVIedbU+++TI/-Tmp-/mongrel25400-0
ruby      25400 philipmcmahon    8u   REG   14,2    626096 13096931 
/private/var/folders/+Z/+ZLlmYL1FlmQ5osVVIedbU+++TI/-Tmp-/mongrel25400-0
ruby      25400 philipmcmahon    9u   REG   14,2   1350716 13097079 
/private/var/folders/+Z/+ZLlmYL1FlmQ5osVVIedbU+++TI/-Tmp-/mongrel25400-0
ruby      25400 philipmcmahon   10u   REG   14,2    203294 13096875 
/private/var/folders/+Z/+ZLlmYL1FlmQ5osVVIedbU+++TI/-Tmp-/mongrel25400-0
ruby      25400 philipmcmahon   11u   REG   14,2   2699790 13097300 
/private/var/folders/+Z/+ZLlmYL1FlmQ5osVVIedbU+++TI/-Tmp-/mongrel25400-0
ruby      25400 philipmcmahon   12u   REG   14,2    178210 13096959 
/private/var/folders/+Z/+ZLlmYL1FlmQ5osVVIedbU+++TI/-Tmp-/mongrel25400-0
ruby      25400 philipmcmahon   13u   REG   14,2    217603 13097577 
/private/var/folders/+Z/+ZLlmYL1FlmQ5osVVIedbU+++TI/-Tmp-/mongrel25400-0
ruby      25400 philipmcmahon   14u   REG   14,2   1348152 13097069 
/private/var/folders/+Z/+ZLlmYL1FlmQ5osVVIedbU+++TI/-Tmp-/mongrel25400-0
ruby      25400 philipmcmahon   15u   REG   14,2    203292 13096968 
/private/var/folders/+Z/+ZLlmYL1FlmQ5osVVIedbU+++TI/-Tmp-/mongrel25400-0

Holy shit!

I have no idea what those file-pointers are, but I do know that they are caused by the file part of multi-part form upload data. For a while I thought it was related to creating Tempfile classes, but after it while it became clear that it was mongrel doing whatever.

To cut a long story short, I switched to webrick and the problem went away.