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

3

13

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.

Converter answered 4/1, 2011 at 2:41 Comment(3)
For now I'm handling the tempfile issue using cron and tmpreaper. CRON runs tmpreaper every 10 minutes to clear my tmp directory. Would still like a more elegant fix but this seems to be working for now.Converter
What server are you using? Puma perhaps?Ushaushant
a relevant Thread in Rack devel group: groups.google.com/forum/#!searchin/rack-devel/temp/rack-devel/…Ushaushant
D
5

I don't know if this is anymore elegant but this is what I am doing after the file is saved"

tempfile = params[:file].tempfile.path
if File::exists?(tempfile)
  File::delete(tempfile)
end
Diptych answered 11/3, 2011 at 20:20 Comment(1)
I'm seeing two identical temporary files being created, above will delete only one of them (github.com/rack/rack/issues/641)Ushaushant
F
14

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
Fredi answered 2/7, 2015 at 15:4 Comment(1)
This is the correct answer, as these temp files are the domain of Rack (obviously from their file name). This happens for any form multipart upload, not just when using Paperclip. The TempfileReaper middleware was added to Rack in 1.6.0 but apparently doesn't get enabled by Rails as a default.Streetman
D
5

I don't know if this is anymore elegant but this is what I am doing after the file is saved"

tempfile = params[:file].tempfile.path
if File::exists?(tempfile)
  File::delete(tempfile)
end
Diptych answered 11/3, 2011 at 20:20 Comment(1)
I'm seeing two identical temporary files being created, above will delete only one of them (github.com/rack/rack/issues/641)Ushaushant
U
0

UPDATE: Problem should be resolved in rack-1.6.0.beta2. I see it's already being used in Rails 4.2.0.rc2.

Below workaround served me well for almost a year:

I've added this at the end of controller action that accepts file uploads:

Thread.new { GC.start }

This triggers Garbage Collection of unused Rack::Request objects which also deletes associated temp files. Note it doesn't sweep temp file of current request, but it does remove previous files, and prevents them from accumulating.

Ushaushant answered 7/1, 2014 at 14:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.