5

I have a Rails app where I'm parsing an uploaded CSV. I just discovered that the parsing has stopped working and am investigating.

I've tracked it back to the fact that I call .read on an instance of ActionDispatch::Http::UploadedFile, and that returns an empty string.

The uploaded file is not empty, and when I inspect the UploadedFile object, it has an attribute like @tempfile=#<File:/tmp/RackMultipart20130530-34976-6jaqt5. If I cat that file on the filesystem, it contains what I expect.

Why might .read return an empty string?

I'm using Rails 3.2.13.

2
  • Please post your parsing code. May 30, 2013 at 16:25
  • @PauloFidalgo - My parsing code starts with CSV.parse(input_string) then maps over the returned rows. The problem is that input_string is blank, and that's because this call to read is blank. May 30, 2013 at 16:28

2 Answers 2

11

Try rewinding

My colleague suggested calling rewind before read, and that works. Apparently some other code is reading the file first.

Update

Nobody's reading the file before I am, as far as I can tell. I created an initializer to spy on the UploadedFile:

# config/initializers/uploaded_file.rb
class ActionDispatch::Http::UploadedFile

  alias_method :old_read, :read
  def read(*args)
    Rails.logger.info "file #{self} is getting read by #{caller.first}!"
    old_read(*args)
  end
end

I didn't see any other calls to read. Thinking there might be some other method called that would advance the read pointer, I went further and replaced the class entirely:

# config/initializers/uploaded_file.rb
ActionDispatch::Http::UploadedFile = Class.new do

  def initialize(*args)
    Rails.logger.info "initilized with #{args}"
  end

  def method_missing(method_name, *args, &block)
    Rails.logger.info "file got #{method_name} with #{args}.\
      Block? #{block_given?} caller? #{caller.first}"
  end

end

Still nothing. I see a call to initialize, my call to rewind, and my call to read.

Seems like this file comes pre-wound to the end. I'm not sure why that might be, but rewind is still the fix for me.

2
  • I ran into the same issue locally (MacOS) but the upload seems to work fine on our linux servers. Rewind worked for me as well. My first suspicion was that it was Pow as I had recently updated that but it fails in the same way when run through unicorn. If I discover a smoking gun, I will post here. It is curious that this problem occurred for me suddenly and around the same time as it started for you -- perhaps a recently updated gem is responsible?
    – Erik G.
    Jun 9, 2013 at 19:42
  • Same weird issue cropped up on me today. I traced it back using git-bisect to a commit from May 30th where I did a bundle update. Unfortunately haven't had time to pick it apart further yet (a lot of gems were updated), but I put the Gemfile.lock and bundle exec gem list diffs in a gist: gist.github.com/abevoelker/5750689 Jun 10, 2013 at 17:42
0

If the file was just uploaded and written, be sure to close it before proceeding. For example:

out = File.open(path, 'w')
out.write(stuff)
out.close # omitting this may cause the issue described!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.