Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Errno::EMFILE: Too many open files" with local images on create

On create of an object in Rails, I want to automatically assign it a stock image from the assets directory which can be overwritten later by a user.

As a result, I execute the following private method upon creation of the object:

def save_stock_image
  image_path = Dir.glob(<list-of-images-from-directory>).sample

  File.open(image_path) do |file|
    self.image = file
    self.save!
  end
end

However, after 6 RSpec tests, I begin to receive the following error:

Failure/Error: let(:object) { create(:object) }
Errno::EMFILE:
  Too many open files - /tmp/16020130822-36578-q8j9v9.jpg
# ./app/models/object.rb:502:in `block in save_stock_image'
# ./app/models/object.rb:501:in `open'
# ./app/models/object.rb:501:in `save_stock_image'
# ./spec/controllers/object_controller_spec.rb:318:in `block (3 levels) in <top (required)>'
# ./spec/controllers/object_controller_spec.rb:344:in `block (4 levels) in <top (required)>'

The above error is on ~40 of 60 tests. I've looked at a few SO questions, as well as https://github.com/thoughtbot/paperclip/issues/1122 and https://github.com/thoughtbot/paperclip/issues/1000. The closest answer I could find was to ensure the file descriptor was closing. Before I used File.open in the block, I explicitly closed the file with file.close - this didn't work either.

Something obvious that I'm doing wrong? Is there a better way to accomplish what I'm trying to do?

UPDATE

It looks like it has something to do with the temporary files which Paperclip creates before they are uploaded to S3. Is there something with closing those tempfiles that I'm missing?

like image 258
CDub Avatar asked Aug 23 '13 02:08

CDub


1 Answers

Just ran into this myself. It looks like the master branch has a fix. See my comments here:

https://github.com/thoughtbot/paperclip/issues/1326?source=cc

like image 105
Philip Hallstrom Avatar answered Oct 14 '22 08:10

Philip Hallstrom