What have you found for these years?

2011-12-28

paperclip and temporary files

from: Lin Jen-Shin (godfat)
date: Wed, Dec 28, 2011 at 12:58 AM
subject: the mysterious paperclip problem

We can't upgrade paperclip to a newer version greater than
2.3.12 before, and this is finally fixed.

So, the main issue here is that after paperclip 2.3.12+,
it would try to unlink the file after uploading to S3.
But that's not the case here, because Rails has
wrapped Tempfile and doesn't provide unlink method.
(there's always a Rails' way rather than Ruby's way in Rails)

The problem is that we need to rewind the file handle
after paperclip took over the uploaded temporary file.
A single line could fix that:
# we need this for paperclip > 2.3.12
params[:collage][:image].rewind
Another issue prevented me from upgrading paperclip is
now identified that has nothing to do with paperclip.

It's very strange that during the image processing process,
the temporary file would get disappeared out of the air!
I suspected paperclip at first because if I downgraded paperclip,
it would work, and paperclip would unlink the temporary file
after image has been uploaded.

But the reality is that the file looked like disappeared for
no reason at all! Somehow between a method call doing
nothing at all, and this only happened on MY computer
but not on staging server!

Finally I realized that it's Ruby's garbage collection did
delete the temporary file! My computer might be running
too fast and the garbage collection is doing its job efficiently.
I am running Ruby 1.9.3, and on staging/production servers,
they are running 1.9.2, which is slower according to my experience.

That is, this is a real bug in our code. I should not have
written that way. I wrote that because I didn't realize Tempfile
instance would try to delete the corresponded file in its
finalize method. I thought that's the OS would do this job
at some point if the OS wants to claim some memory from
the app or something.

I am fixing this now...



Lesson taken: Side-effects depending on GC is horrible.

0 retries:

Post a Comment

All texts are licensed under CC Attribution 3.0