jnicklas/carrierwave — Issue #550: Rails 3.1.3, fog s3, save fails when trying to delete file
Author: amalagaura
Created: 2011-12-04 18:05:02+00:00
State: open

I have a very simple uploader with fog. It fails when I save because it says:
Errno::ETXTBSY: Text file busy

Below are the config files:

initializer/carrier_wave.rb

```
CarrierWave.configure do |config|
config.fog_credentials = {
:provider => 'AWS',
:aws_access_key_id => 'access',
:aws_secret_access_key => 'secret',
}
config.fog_directory = 'mybucket'
config.fog_public = true
config.cache_dir = "#{Rails.root}/tmp/uploads"
end
```

uploader

```

class PictureUploader < CarrierWave::Uploader::Base

storage :fog

def store_dir
"#{Rails.env}/uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end

def extension_white_list
%w(jpg jpeg gif png)
end

end
```

I run:

```
i = Item.last
i.picture = File.open 'mypictureondisk.png'
i.save
```

and i get:

```
Errno::ETXTBSY: Text file busy - /vagrant/tmp/uploads/20111204-0951-15360-1736/debian-logo.png
from /usr/lib/ruby/1.9.1/fileutils.rb:1314:in `unlink'
from /usr/lib/ruby/1.9.1/fileutils.rb:1314:in `block in remove_file'
from /usr/lib/ruby/1.9.1/fileutils.rb:1319:in `platform_support'
from /usr/lib/ruby/1.9.1/fileutils.rb:1313:in `remove_file'
from /usr/lib/ruby/1.9.1/fileutils.rb:781:in `remove_file'
from /usr/lib/ruby/1.9.1/fileutils.rb:559:in `block in rm'
from /usr/lib/ruby/1.9.1/fileutils.rb:558:in `each'
from /usr/lib/ruby/1.9.1/fileutils.rb:558:in `rm'
from /usr/lib/ruby/gems/1.9.1/gems/carrierwave-0.5.8/lib/carrierwave/sanitized_file.rb:217:in `delete'
from /usr/lib/ruby/gems/1.9.1/gems/carrierwave-0.5.8/lib/carrierwave/uploader/store.rb:60:in `block in store!'
from /usr/lib/ruby/gems/1.9.1/gems/carrierwave-0.5.8/lib/carrierwave/uploader/callbacks.rb:17:in `with_callbacks'
from /usr/lib/ruby/gems/1.9.1/gems/carrierwave-0.5.8/lib/carrierwave/uploader/store.rb:58:in `store!'
from /usr/lib/ruby/gems/1.9.1/gems/carrierwave-0.5.8/lib/carrierwave/mount.rb:347:in `store!'
from /usr/lib/ruby/gems/1.9.1/gems/carrierwave-0.5.8/lib/carrierwave/mount.rb:217:in `store_picture!'
from /usr/lib/ruby/gems/1.9.1/gems/activesupport-3.1.3/lib/active_support/callbacks.rb:419:in `_run_save_callbacks'
from /usr/lib/ruby/gems/1.9.1/gems/activesupport-3.1.3/lib/active_support/callbacks.rb:81:in `run_callbacks'
```

I checked the S3 bucket and the file is saved. So it looks like it errors out when trying to delete the file because it still has an open handle. I checked `lsof` and the rails console has the open lock on the file which it's tryign to delete. This is an open lock on the file in the `/tmp/` directory, not the original file that I opened.

When I switch to :s3 instead of :fog it works fine!