Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tempfile not deleted when using "format" #188

Closed
wants to merge 2 commits into from

Conversation

mrkamel
Copy link

@mrkamel mrkamel commented Dec 13, 2013

I'm using 3.7.0.
Minimagick leaves an additional tempfile in /tmp when using the "format" command.

# test.rb

require "mini_magick"

image = MiniMagick::Image.open("test.tif")
image.format "jpg"

such that my disk fills up.

Before i run ruby test.rb: ls /tmp/mini_magick* gives 0 results
Afterwards, i have a /tmp/mini_magick20131127-18504-ula55j.jpg that doesn't get deleted when the script terminates, etc.

@mrkamel
Copy link
Author

mrkamel commented Dec 13, 2013

I wrote a fix. Maybe somebody from core could review it, escpecially because it uses convert instead of mogrify (is this ok? - it's imho better suited for Image#format) - and moreover, someone could explain the use case for the existing yield to me:

def format(...)
   ...
   command = CommandBuilder.new(...)
   yield(command) if block_given?
   ...
   run command
   ...
end

as it yields the command builder instance, which probably should not be made accessible to minimagick users directly imho?!

However, here's the pull request

@mrkamel
Copy link
Author

mrkamel commented Jan 13, 2014

Hi, what do you think about this PR?

@kaspergrubbe
Copy link
Contributor

I thought it was up to the garbage collector and/or the operating system to clean up temporary files?

Maybe we could make use of the open-block to help the GC clean up, eg.:

# Short-term use
 Tempfile.open('my_program') do |f|
   puts "Tempfile opened at #{f.path}"
   f.puts "Putting some text into the file"
 end

from http://ruby.about.com/od/beginningruby/a/dir4.htm

Why do you think that convert is better than mogrify in a format situation?

@mrkamel
Copy link
Author

mrkamel commented Jan 15, 2014

I thought it was up to the garbage collector and/or the operating system to clean up temporary files?

Yes, but take a look to:

require "mini_magick"

image = MiniMagick::Image.open("test.tif") # a)
image.format "jpg" # b)

Here, we want minimagick to behave similar to:
a) copy test.tif to a tempfile
b) convert the tif-tempfile to a jpg-tempfile

However, in master, minimagick currently does the following:
a) copy test.tif to a tempfile
b) convert/mogrify the tif-tempfile to a jpg-file (no tempfile!)

Take a look into the source, no tempfile is touched in Image#format.
Therefore, the jpg file will live forever, ... and fill up the disk

Why do you think that convert is better than mogrify in a format situation?

Generally speaking, Image#format receives an input file e.g. input.tif and must create e.g. output.jpg.
However, mogrify does not accept a parameter [outfile], such that convert is better suited
for this sitation. Otherwise, we introduce a dependency and lean on the fact the mogrify will only change the suffix from .tif to .jpg to choose a outfile-path. This dependency is currently present in Image#format.

Moreover, if we use tempfiles (like we do for Image#open), Image#format must create
a new tempfile for the new format (.jpg). However, ruby's tempfile library is afaik not able
to accept an arbitrary input path (only prefix and suffix can be chosen). Therefore, we must
use Tempfile.new to let the tempfile library choose a distinct temporary filename. As we
can't hand this new destination tempfile-path to mogrify, we actually have to use convert
instead of mogrify.

@sockmonk
Copy link

+1 for accepting this pull request. I'm also seeing my /tmp directory fill up with mini_magick .jpg files in the same circumstances. Looks like this solution is well researched and works.

@mjgiarlo
Copy link

This is problematic for me as well. Have the owners of this repo commented on this PR yet?

@scande3
Copy link

scande3 commented Jul 31, 2014

+1 on having this merged. When dealing with tens of thousands of large objects, the leftover temporary files filled 60GB of space causing my image processing server to run out of storage.

@jcoyne
Copy link

jcoyne commented Aug 4, 2014

@mrkamel can you rebase this? It doesn't merge cleanly. Then, 👍 for merging into master.

@mrkamel
Copy link
Author

mrkamel commented Aug 5, 2014

Done.

However, Travis currently does not seem to work in general for rbx and jruby-head.

@mrkamel
Copy link
Author

mrkamel commented Aug 5, 2014

oops, don't know what happened in commit 7af2cd4. i'll create a new, fresh PR.

@mrkamel mrkamel closed this Aug 5, 2014
@mrkamel mrkamel mentioned this pull request Aug 5, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants