aws-sdk-ruby icon indicating copy to clipboard operation
aws-sdk-ruby copied to clipboard

errors thrown when sending emails within multiple threads

Open desheikh opened this issue 13 years ago • 7 comments

tested using ruby 1.9.3-p327

require 'aws-sdk'
pool = Array.new(10) do |i|
  Thread.new do
    AWS::SimpleEmailService.new(:access_key_id => '1234567890', :secret_access_key => '1234567890').send_email(
      :subject => 'subject',
      :body_html => 'body_html',
      :body_text => 'body_text',
      :to => '[email protected]',
      :from => '[email protected]',
    )
  end
end

pool.each(&:join)

~/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/aws-sdk-1.7.1/lib/aws/core/client.rb:499:in build_request': undefined methodadd_authorization!' for #AWS::SimpleEmailService::Request:0x007feac892bd90 (NoMethodError)

~/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/aws-sdk-1.7.1/lib/aws/simple_email_service.rb:281:in send_email': undefined methodsend_email' for #AWS::SimpleEmailService::Client:0x007fdf7c1465c8 (NoMethodError)

desheikh avatar Dec 03 '12 22:12 desheikh

Thank you for reporting this issue. There appears to be a thread-safety issue with how the service interfaces (e.g. AWS::SimpleEmailService) construct their client. I will try to resolve this issue soon, but you should be able to work around this by constructing the ses interface outside the loop:

require 'aws-sdk'
ses = AWS::SimpleEmailService.new(...)
pool = Array.new(10) do |i|
Thread.new do
  ses.send_email(
    :subject => 'subject',
    :body_html => 'body_html',
    :body_text => 'body_text',
    :to => '[email protected]',
    :from => '[email protected]',
  )
  end
end

pool.each(&:join)

Please watch this issue for updates.

trevorrowe avatar Dec 10 '12 19:12 trevorrowe

@trevorrowe, thanks for the response, but please note that keeping the initialization out of the loop will still have the same issue.

~/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/aws-sdk-1.7.1/lib/aws/core/signature/version_4.rb:87:in `service': NotImplementedError (NotImplementedError)

~/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems/aws-sdk-1.7.1/lib/aws/core/client.rb:499:in build_request': undefined methodadd_authorization!' for #AWS::SimpleEmailService::Request:0x007fdff13558d0 (NoMethodError)

desheikh avatar Dec 11 '12 06:12 desheikh

Given the following example

require 'aws-sdk'
ses = AWS::SimpleEmailService.new # loading credentials from environment
pool = Array.new(10) do |i|
Thread.new do
  ses.send_email(
    :subject => 'subject',
    :body_html => 'body_html',
    :body_text => 'body_text',
    :to => '[email protected]',
    :from => '[email protected]', # replace this with a verified email address
  )
  end
end

pool.each(&:join)

I was able to execute this many times without issue. If I moved the ses assignment inside the Thread.new block, then it would fail every time due to thread safety issues. Is this what your test example looks like?

trevorrowe avatar Dec 11 '12 19:12 trevorrowe

@trevorrowe, I've been able to execute it multiple times without issue as well, but If I keep at it I do get the occasional add_authorization error. Havn't been able to reproduce the error at "version_4.rb:87" again though.

My test file is exactly the same except for the array size which I've set to 90 and of course the from email.

desheikh avatar Dec 11 '12 19:12 desheikh

@desheikh I just pushed a commit that resolves an issue with AWS::Core::Configuration. I then took your original example and added one line. Between the two, this resolved the issue for me.

AWS.eager_autoload!

I realize this is not an ideal fix. It forcibly loads all AWS classes up-front, which can be slow, but it is a one-time cost. That said, this is not an issue in Ruby 2.0 and you can drop the call to eager_autoload! if you are able to upgrade.

trevorrowe avatar Mar 21 '13 16:03 trevorrowe

Closing this issue as it should work properly if all classes have been eagerly auto-loaded. Please re-open if you still run into issues.

trevorrowe avatar Apr 16 '13 16:04 trevorrowe

Hi, Thanks, I just tested out sending mails out with the latest version of the sdk and so far have not experienced any issues yet even without the eager_autoload. AWS.eager_autoload! raises an exception though, created a separate issue for it.

desheikh avatar Apr 24 '13 21:04 desheikh