Why are RackMultipart* files persisting in my Rails /tmp directory?

NAD picture NAD · Jan 4, 2011 · Viewed 9.4k times · Source

I'm using Paperclip (2.3) to handle image uploads on a Rails 3.0.3 app running on Ubuntu. Paperclip is handling the uploads as advertised BUT the RackMultipart* files that are created in the application's /tmp folder persist -- that is, they simply accumulate rather than deleting themselves. I realize that I could use tmpreaper to delete old tmpfiles but I'd really like to find a more elegant (and scalable) solution.

I had a previous issue with temp files (i.e. RackMultipart* files) accumulating in the Rails app's root directory (instead of in /tmp). I resolved this by explicitly setting the temp path in my environment.rb file like so:

ENV['TMPDIR'] = Rails.root.join('tmp')

Is there another environment variable that needs to be set to make sure that the tempfiles are handled properly -- i.e. deleted once they've been saved in the model? I'm not sure if this is a problem with Paperclip or my Rails setup.

I've searched high and low but have made little progress on this. I'd be grateful for any leads.

Sincere thanks.

PS - I'm using currently using S3 for storage. This doesn't seem to be tied to the problem though -- I had the same problem when I was storing the files locally.

Answer

Diego Garcia picture Diego Garcia · Jul 2, 2015

The TempFileReaper is the Rack middleware thought to handle this issue.

http://www.rubydoc.info/github/rack/rack/Rack/TempfileReaper

Including this line in the application.rb solves the problem:

config.middleware.use Rack::TempfileReaper