#22 new
rubymaverick

Race condition with GC and Tempfile cleanup

Reported by rubymaverick | February 26th, 2008 @ 02:24 PM

I found a similar problem to #6 but with more wide-ranging possible effects than just resizing in the mini-magick processor. There is a race condition on all uploads when using the uploaded_data= method. This will cause problems where the GC will clear the tempfile before attachment_fu gets a chance to process the tempfile.

The problem stems from the fact that tempfiles are erased as the Tempfile objects are garbage collected. uploaded_data= takes a tempfile as an argument and sets some attributes on the object, such as temp_path and temp_data.

Here is code how it looks currently:

      def uploaded_data=(file_data)
        return nil if file_data.nil? || file_data.size == 0 
        self.content_type = file_data.content_type
        self.filename     = file_data.original_filename if respond_to?(:filename)
        if file_data.is_a?(StringIO)
          file_data.rewind
          self.temp_data = file_data.read
        else
          self.temp_path = file_data.path
        end
      end

As you can see there are no lasting references to file_data after uploaded_data= exits, thus it is clear for GC. Instead of throwing away the ref to file_data you can just do this:

 if file_data.is_a?(StringIO)
       file_data.rewind
       self.temp_data = file_data.read
else
       self.temp_path = file_data
end

This works (all the tests pass) and it allows the object to keep a reference to the temp_file around as long as the object is in scope.

No comments found

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

People watching this ticket

Attachments

Pages