S3 write timeout (Ruby 2.0)
This is an odd one. The following code generates a timeout for me when run with ruby 2.0.0-p0 but it's OK with 1.9.3-p385. Using aws-sdk 1.9.2.
require 'aws'
AWS.config(access_key_id: 'KEY', secret_access_key: 'SECRET')
s3 = AWS::S3.new
emr = AWS::EMR.new
bucket_name = 'some-bucket'
s3.buckets[bucket_name].exists?
emr.job_flows.each { }
puts "Uploading file"
s3.buckets[bucket_name].objects['foo.sh'].write(file: '/path/to/file.txt')
The calls to bucket.exists? and job_flows.each are significant - if you remove either of these the write succeeds.
The error generated:
Uploading file
/home/charles/.rbenv/versions/2.0.0-p0/gemsets/awstest/gems/aws-sdk-1.9.2/lib/aws/core/client.rb:360:in `return_or_raise': Your socket connection to the server was not read from or written to within the timeout period. Idle connections will be closed. (AWS::S3::Errors::RequestTimeout)
from /home/charles/.rbenv/versions/2.0.0-p0/gemsets/awstest/gems/aws-sdk-1.9.2/lib/aws/core/client.rb:461:in `client_request'
from (eval):3:in `put_object'
from /home/charles/.rbenv/versions/2.0.0-p0/gemsets/awstest/gems/aws-sdk-1.9.2/lib/aws/s3/s3_object.rb:1669:in `write_with_put_object'
from /home/charles/.rbenv/versions/2.0.0-p0/gemsets/awstest/gems/aws-sdk-1.9.2/lib/aws/s3/s3_object.rb:608:in `write'
from awstest.rb:15:in `<main>'
Actually it turns out that the failure is somewhat intermittent. After leaving my computer idling for a while the above script actually works a few times before starting to fail again. Once it starts failing it fails pretty consistently.
Note that it always succeeds on ruby 1.9, so this is definitely something 2.0 related.
Is this the entire script or are you doing anything else (threading perhaps)?
Hi Trevor, that's the entire script. This is on a Linux system if that's significant. I'll try and replicate it on another box when I get some time to see if it's a local oddity.
OK, so I can replicate this on an EC2 Ubuntu Server 12.04 instance set up as follows:
sudo apt-get update
sudo apt-get install git
sudo apt-get install build-essential readline-dev libssl-dev libxml2-dev libxslt1-dev
git clone git://github.com/sstephenson/rbenv.git ~/.rbenv
git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
bash -l
rbenv install 2.0.0-p0
rbenv local 2.0.0-p0
gem install aws-sdk
ruby awstest.rb
It doesn't happen as frequently as it does on my local box, but it does happen. Most times it happens when I rerun after leaving the box idle for a few minutes.
Edit: now I've managed to get it into a state where it's happening pretty much every time I run it.
I've been receiving this error alot, it turned out for me it was threading (too many concurrent CPU heavy jobs in my worker queue).
I do wonder if this may have had a hand in some of the errors I logged whilst only having one concurrent job queue (one thread per process) but they certainly weren't as common as you've mentioned. I'll be doing a big upload again very soon and will carefully monitor the failures.
One of the differences I can see between some of my code and yours is that i'm parsing in my own IO object (built on top of StringIO with some convenience methods to implement tempfiles if a path is requested). You could attempt to read the file into memory as a string if space allows and parse that in, which would remove any potential issues caused by the file from the equation, everything else in your case is vanilla, so if it's not your file it likely is the library.
G
The error still occurs even if you write a simple string instead of a file, so it doesn't seem to be related to the file being uploaded at all.
Also experiencing intermittent failures. Currently just using the following workaround in place of calls to S3Object's write method:
# path - The String path of the file to be uploaded.
# s3_obj - The S3Object handle to be written to.
def upload_to_s3(path, s3_obj)
retries = S3CONFIG[:upload_retries]
begin
s3_obj.write(File.open(path, 'rb', :encoding => 'BINARY'))
rescue => ex
retries -= 1
if retries > 0
puts "ERROR during S3 upload: #{ex.inspect}. Retries: #{S3CONFIG[:upload_retries] - retries}"
retry
else
# oh well, we tried...
raise
end
end
end
@ampedandwired The error message you are getting, Your socket connection to the server was not read from or written to within the timeout period. Idle connections will be closed. (AWS::S3::Errors::RequestTimeout) indicates that Amazon S3 is closing the http connection because it waited to long for data. This is caused when a request indicates the content-length will by X bytes, but then sends fewer. S3 keeps waiting for the remainder bytes that aren't coming and then eventually kills the connection.
You mentioned you were able to reproduce this issue using strings (and not just files). It also does not appear to be threading related (your example does not create multiple threads).
I'm not able to reproduce this issue locally. It could be helpful if you could enable some detailed logging so I can get some more insight into what is going on. Can you try this:
# when creating s3, enable a few loggers
s3 = AWS::S3.new(:logger => Logger.new($stdout), :http_wire_trace => true)
I'm hoping this will help shed some light on what is happening so we can get this fixed. Thanks!
Here's a full trace from two consecutive tests, the first one succeeded the second did not:
$ ruby test.rb
opening connection to xxxxx.s3.amazonaws.com:443...
opened
starting SSL for xxxxx.s3.amazonaws.com:443...
SSL established
<- "GET /?versioning HTTP/1.1\r\nContent-Type: \r\nUser-Agent: aws-sdk-ruby/1.10.0 ruby/2.0.0 x86_64-linux\r\nDate: Thu, 16 May 2013 03:38:02 GMT\r\nAuthorization: AWS XXXXXXXXX:xxxxxxxxxx=\r\nAccept-Encoding: gzip;q=1.0,deflate;q=0.6,identity;q=0.3\r\nAccept: */*\r\nHost: xxxxx.s3.amazonaws.com\r\n\r\n"
-> "HTTP/1.1 200 OK\r\n"
-> "x-amz-id-2: xxxxxxxxxx\r\n"
-> "x-amz-request-id: xxxxxxxxxx\r\n"
-> "Date: Thu, 16 May 2013 03:38:06 GMT\r\n"
-> "Transfer-Encoding: chunked\r\n"
-> "Server: AmazonS3\r\n"
-> "\r\n"
-> "71\r\n"
reading 113 bytes...
-> "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<VersioningConfiguration xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\"/>"
read 113 bytes
reading 2 bytes...
-> "\r\n"
read 2 bytes
-> "0\r\n"
-> "\r\n"
Conn keep-alive
I, [2013-05-16T13:38:05.396659 #12764] INFO -- : [AWS S3 200 2.933784 0 retries] get_bucket_versioning(:bucket_name=>"xxxxx")
Bucket exists: true
Uploading file
Conn close because of keep_alive_timeout
opening connection to xxxxx.s3.amazonaws.com:443...
opened
starting SSL for xxxxx.s3.amazonaws.com:443...
SSL established
<- "PUT /foo HTTP/1.1\r\nContent-Type: \r\nContent-Length: 19\r\nUser-Agent: aws-sdk-ruby/1.10.0 ruby/2.0.0 x86_64-linux\r\nDate: Thu, 16 May 2013 03:38:07 GMT\r\nAuthorization: AWS XXXXXXXXX:xxxxxxxxxx=\r\nAccept-Encoding: gzip;q=1.0,deflate;q=0.6,identity;q=0.3\r\nAccept: */*\r\nHost: xxxxx.s3.amazonaws.com\r\n\r\n"
-> "HTTP/1.1 200 OK\r\n"
-> "x-amz-id-2: xxxxxxxxxx\r\n"
-> "x-amz-request-id: xxxxxxxxxx\r\n"
-> "Date: Thu, 16 May 2013 03:38:11 GMT\r\n"
-> "ETag: \"8622b9718771d75e07734684d6efa1dd\"\r\n"
-> "Content-Length: 0\r\n"
-> "Server: AmazonS3\r\n"
-> "\r\n"
reading 0 bytes...
-> ""
read 0 bytes
Conn keep-alive
I, [2013-05-16T13:38:10.843515 #12764] INFO -- : [AWS S3 200 3.394982 0 retries] put_object(:bucket_name=>"xxxxx",:content_length=>19,:data=>#<StringIO:0x007feabcba6dd8>,:key=>"foo")
$ ruby test.rb
opening connection to xxxxx.s3.amazonaws.com:443...
opened
starting SSL for xxxxx.s3.amazonaws.com:443...
SSL established
<- "GET /?versioning HTTP/1.1\r\nContent-Type: \r\nUser-Agent: aws-sdk-ruby/1.10.0 ruby/2.0.0 x86_64-linux\r\nDate: Thu, 16 May 2013 03:38:12 GMT\r\nAuthorization: AWS XXXXXXXXX:xxxxxxxxxx=\r\nAccept-Encoding: gzip;q=1.0,deflate;q=0.6,identity;q=0.3\r\nAccept: */*\r\nHost: xxxxx.s3.amazonaws.com\r\n\r\n"
-> "HTTP/1.1 200 OK\r\n"
-> "x-amz-id-2: xxxxxxxxxx\r\n"
-> "x-amz-request-id: xxxxxxxxxx\r\n"
-> "Date: Thu, 16 May 2013 03:38:16 GMT\r\n"
-> "Transfer-Encoding: chunked\r\n"
-> "Server: AmazonS3\r\n"
-> "\r\n"
-> "71\r\n"
reading 113 bytes...
-> "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<VersioningConfiguration xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\"/>"
read 113 bytes
reading 2 bytes...
-> "\r\n"
read 2 bytes
-> "0\r\n"
-> "\r\n"
Conn keep-alive
I, [2013-05-16T13:38:15.724986 #12863] INFO -- : [AWS S3 200 2.919649 0 retries] get_bucket_versioning(:bucket_name=>"xxxxx")
Bucket exists: true
Uploading file
<- "PUT /foo HTTP/1.1\r\nContent-Type: \r\nContent-Length: 19\r\nUser-Agent: aws-sdk-ruby/1.10.0 ruby/2.0.0 x86_64-linux\r\nDate: Thu, 16 May 2013 03:38:17 GMT\r\nAuthorization: AWS XXXXXXXXX:xxxxxxxxxx=\r\nAccept-Encoding: gzip;q=1.0,deflate;q=0.6,identity;q=0.3\r\nAccept: */*\r\nHost: xxxxx.s3.amazonaws.com\r\n\r\n"
Conn close because of error end of file reached, and retry
opening connection to xxxxx.s3.amazonaws.com:443...
opened
starting SSL for xxxxx.s3.amazonaws.com:443...
SSL established
<- "PUT /foo HTTP/1.1\r\nContent-Type: \r\nContent-Length: 19\r\nUser-Agent: aws-sdk-ruby/1.10.0 ruby/2.0.0 x86_64-linux\r\nDate: Thu, 16 May 2013 03:38:17 GMT\r\nAuthorization: AWS XXXXXXXXX:xxxxxxxxxx=\r\nAccept-Encoding: gzip;q=1.0,deflate;q=0.6,identity;q=0.3\r\nAccept: */*\r\nHost: xxxxx.s3.amazonaws.com\r\n\r\n"
-> "HTTP/1.1 400 Bad Request\r\n"
-> "x-amz-request-id: 9606878C35487466\r\n"
-> "x-amz-id-2: xxxxxxxxxx\r\n"
-> "Content-Type: application/xml\r\n"
-> "Transfer-Encoding: chunked\r\n"
-> "Date: Thu, 16 May 2013 03:38:40 GMT\r\n"
-> "Connection: close\r\n"
-> "Server: AmazonS3\r\n"
-> "\r\n"
-> "15c\r\n"
reading 348 bytes...
-> "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Error><Code>RequestTimeout</Code><Message>Your socket connection to the server was not read from or written to within the timeout period. Idle connections will be closed.</Message><RequestId>9606878C35487466</RequestId><HostId>xxxxxxxxxx</HostId></Error>"
read 348 bytes
reading 2 bytes...
-> "\r\n"
read 2 bytes
-> "0\r\n"
-> "\r\n"
Conn close
I, [2013-05-16T13:38:40.555424 #12863] INFO -- : [AWS S3 400 22.893523 0 retries] put_object(:bucket_name=>"xxxxx",:content_length=>19,:data=>#<StringIO:0x007fbe45eaad98>,:key=>"foo") AWS::S3::Errors::RequestTimeout Your socket connection to the server was not read from or written to within the timeout period. Idle connections will be closed.
/home/charles/.rbenv/versions/2.0.0-p0/gemsets/awstest/gems/aws-sdk-1.10.0/lib/aws/core/client.rb:360:in `return_or_raise': Your socket connection to the server was not read from or written to within the timeout period. Idle connections will be closed. (AWS::S3::Errors::RequestTimeout)
from /home/charles/.rbenv/versions/2.0.0-p0/gemsets/awstest/gems/aws-sdk-1.10.0/lib/aws/core/client.rb:461:in `client_request'
from (eval):3:in `put_object'
from /home/charles/.rbenv/versions/2.0.0-p0/gemsets/awstest/gems/aws-sdk-1.10.0/lib/aws/s3/s3_object.rb:1704:in `write_with_put_object'
from /home/charles/.rbenv/versions/2.0.0-p0/gemsets/awstest/gems/aws-sdk-1.10.0/lib/aws/s3/s3_object.rb:608:in `write'
from test.rb:9:in `<main>'
The code being run is as follows:
require 'aws'
s3 = AWS::S3.new(:logger => Logger.new($stdout), :http_wire_trace => true)
emr = AWS::EMR.new
bucket_name = 'xxxxx'
puts "Bucket exists: #{s3.buckets[bucket_name].exists?}"
emr.job_flows.each { |jf| puts jf.name }
puts "Uploading file"
s3.buckets[bucket_name].objects['foo'].write("I'm a little teapot")
In terms of replicating the problem, did you try running it on an EC2 Ubuntu instance? I was able to replicate the problem there on a clean box (using the setup documented in my comment above), so hopefully you'll be able to duplicate my results that way.
I'm just spun up a clean EC2 Ubuntu instance and I'm going to try to replicate the issue again.
I'm still yet unable to reproduce the issue. Here are the exact steps I took
- Started a fresh EC2 Instance (
AWS.ec2.instances.create(:image_id => 'ami-856f02ec', :key_name => '...', :security_groups => ['...'])) -- this is a 64 bit EBS backed instance running Ubuntu 12.04.2 LTS (Precise Pangolin) - SSH'd into the instance and ran the following commands:
sudo apt-get updatesudo apt-get install gitsudo apt-get install build-essential libreadline6 libreadline6-dev libssl-dev libxml2-dev libxslt1-dev(readline-dev was unavaialble, so used libreadline6-dev)git clone git://github.com/sstephenson/rbenv.git ~/.rbenvgit clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-buildecho 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrcecho 'eval "$(rbenv init -)"' >> ~/.bashrcbash -lrbenv install 2.0.0-p0rbenv local 2.0.0-p0gem install aws-sdk(this installed v1.10.0)- exported my credentials to
AWS_ACCESS_KEY_IDandAWS_SECRET_ACCESS_KEY cat > awstest.rb(pasted the following code)
require 'aws'
s3 = AWS::S3.new(:logger => Logger.new($stdout), :http_wire_trace => true)
emr = AWS::EMR.new
bucket_name = 'aws-sdk'
puts "Bucket exists: #{s3.buckets[bucket_name].exists?}"
emr.job_flows.each { |jf| puts jf.name }
puts "Uploading file"
s3.buckets[bucket_name].objects['foo'].write("I'm a little teapot")
I've run this 50+ times with a few pauses. I will come back to this after I've let the instance idle.
We also ran into this problem after upgrading to ruby-2.0.0-p0. We saw a significant increase in s3 upload reties immediately after the upgrade. It doesn't seem to be related to the data as our retries eventually succeed.
This test case will fail for me sometimes on ruby-2.0:
require 'aws'
s3 = AWS::S3.new(
:access_key_id => ENV['AWSID'],
:secret_access_key => ENV['AWSKEY'],
)
bucket = ENV['BUCKET']
pid = Process.pid
size = 1024 * 1024 * 25
0.upto(100).each do |i|
name = "#{pid}.#{i}"
puts "generating #{name}"
File.open('/dev/urandom', 'rb') do |urand|
File.open("/tmp/#{name}", 'wb') do |out|
out.write(urand.read(size))
end
end
puts "uploading #{name}"
File.open("/tmp/#{name}", 'rb') do |f|
s3.buckets[bucket].objects[name].write(f,
:multipart_threshold => 1024*1024*20,
)
end
puts "finished #{name}"
end
Since this seems to be specific to Ruby 2.0, I wonder if those who are experiencing this issue on p0 can also reproduce it on 2.0.0-p195 which was released last week. There may have been some related changes that could have fixed this issue.
Some further notes:
- I can readily replicate the problem using the exact steps and test code posted by Trevor above. I am using AMI ami-04ea7a3e (Ubuntu 12.04 Server) on a t1.micro instance in the Sydney region.
- The region of the S3 bucket seems to make a difference for me. I was only able to produce the error when using a bucket in the Sydney region. Other regions (standard, California, Singapore) worked fine.
- Using Ruby 2.0.0-p195 gives the same result
- Using a different AWS account and newly created bucket gives the same result
I wonder what regions everybody else is using?
Hi guys. Not sure if this is related, or a separate issue: https://github.com/thoughtbot/paperclip/issues/751#issuecomment-18214277
Those of us using Paperclip have been encountering timeouts caused the aws-sdk gem after version 1.6.2. This is a major issue, and one that's been around for some time. I don't know if there has been any coordination on fixing it yet.
@uberllama Thank you for looping us in. I'll chime in on the paperclip thread. After a quick scan of the other issue, they do not appear to be related, but I could be wrong.
I think I got the same issue, my test case is to upload a bunch (~40) of smaller files:
- Heroku + Ruby 1.9.3: does work
- Heroku + Ruby 2.0.0: does not work and gives a timeout, most of the time after the 2nd file
- local + Ruby 2.0.0: does work
I had the error also locally, but increasing the timeouts using
AWS.config(:http_open_timeout => 120, :http_read_timeout => 120)
helped. The curious thing is, that on Heroku the error clearly comes before the timeout runs out.
I'll now test what happens if I exchange aws-sdk with some other s3 upload gem (fog).
Ok, if I exchange aws-sdk with fog, it works for me also on Heroku + Ruby 2.0.0