Rails Mailer "Net::OpenTimeout: execution expired" Exception on production server only
Asked Answered
M

8

39

I am using Ruby MRI 2.0.0 and Rails 3.2.12 on a Ubuntu 12.04 TLS VPS and attempting to setup email notifications in my app. It was working fine a few days ago, but not anymore. My web host is OVH.

My SMTP settings:

config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true

ActionMailer::Base.smtp_settings = {
  :address              => "smtp.gmail.com",
  :port                 => 587,
  :user_name            => '[email protected]',
  :password             => 'secret',
  :authentication       => 'plain',
  :enable_starttls_auto => true
}

Using RAILS_ENV=production rails console:

class MyMailer < ActionMailer::Base
  def test_email
    sender     = "[email protected]"
    receiver   = "[email protected]"
    mail from: sender, to: receiver, subject: "Hello!", body: "World!!"
  end
end
 => nil

MyMailer.test_email.deliver

The output:

Net::OpenTimeout: execution expired
    from ~/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/net/smtp.rb:540:in `initialize'
    from ~/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/net/smtp.rb:540:in `open'
    from ~/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/net/smtp.rb:540:in `tcp_socket'
    from ~/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/net/smtp.rb:550:in `block in do_start'
    from ~/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/net/smtp.rb:549:in `do_start'
    from ~/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/net/smtp.rb:519:in `start'
    from ~/.rvm/gems/ruby-2.0.0-p0@mygemset/gems/mail-2.4.4/lib/mail/network/delivery_methods/smtp.rb:144:in `deliver!'
    from ~/.rvm/gems/ruby-2.0.0-p0@mygemset/gems/mail-2.4.4/lib/mail/message.rb:2034:in `do_delivery'
    from ~/.rvm/gems/ruby-2.0.0-p0@mygemset/gems/mail-2.4.4/lib/mail/message.rb:229:in `block in deliver'
    from ~/.rvm/gems/ruby-2.0.0-p0@mygemset/gems/actionmailer-3.2.12/lib/action_mailer/base.rb:415:in `block in deliver_mail'
    from ~/.rvm/gems/ruby-2.0.0-p0@mygemset/gems/activesupport-3.2.12/lib/active_support/notifications.rb:123:in `block in instrument'
    from ~/.rvm/gems/ruby-2.0.0-p0@mygemset/gems/activesupport-3.2.12/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
    from ~/.rvm/gems/ruby-2.0.0-p0@mygemset/gems/activesupport-3.2.12/lib/active_support/notifications.rb:123:in `instrument'
    from ~/.rvm/gems/ruby-2.0.0-p0@mygemset/gems/actionmailer-3.2.12/lib/action_mailer/base.rb:413:in `deliver_mail'
    from ~/.rvm/gems/ruby-2.0.0-p0@mygemset/gems/mail-2.4.4/lib/mail/message.rb:229:in `deliver'
    from (irb):28
    from ~/.rvm/gems/ruby-2.0.0-p0@mygemset/gems/railties-3.2.12/lib/rails/commands/console.rb:47:in `start'
    from ~/.rvm/gems/ruby-2.0.0-p0@mygemset/gems/railties-3.2.12/lib/rails/commands/console.rb:8:in `start'
    from ~/.rvm/gems/ruby-2.0.0-p0@mygemset/gems/railties-3.2.12/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'2.0.0p0 :029 >

I tried the following:

  • The exception_notification gem was added to the setup a few days ago. I tried to comment its line in Gemfile as well as its matching configuration, and run bundle install. After restarting the server, the issue is still present, even if I delete and recreate the gemset.
  • Test it out on a virtual machine (exact same setup as the VPS including iptables rules): works
  • Disable iptables rules: does not work
  • Manually connect to Gmail from the VPS using openssl: works (so this is not a firewall issue - see here: Connecting to smtp.gmail.com via command line);
  • Enable IMAP in Gmail account options (it was disabled): does not work
  • Use a different Gmail account: does not works
  • Replace Ruby 2.0.0 by Ruby 1.9.3
  • Upgrade to Rails 3.2.13

Does someone have a possible clue as to how to resolve this issue?

Thanks!

Mukden answered 16/4, 2013 at 14:47 Comment(1)
Here's another manifestation: execution expired /ree-187/lib/ruby/1.8/timeout.rb:64:in open' .../vendor/plugins/action_mailer_optional_tls/lib/smtp_tls.rb:40:in do_tls_start' /.../vendor/plugins/action_mailer_optional_tls/lib/smtp_tls.rb:40:in `do_tls_start'...Hugely
M
5

The issue was due to an IPv6 misconfiguration on the production server and has now been fixed.

Mukden answered 22/4, 2013 at 8:34 Comment(7)
Same problem here, do you know how to spot that misconfiguration? Is there some log file to lookup or something?Osugi
Something I noticed: when I try to connect to smpt.gmail.co via command line it works, but first it tries with the IPv6 address and waits for SEVERAL seconds, only after this long period of time it tries with the simple IP and connects! Maybe is during the first time that my app goes timeout... now the question is: can this be a symptom to spot this kind of misconfiguration?Osugi
I'm not sure if this is the issue, but I'm getting the same symptoms... how'd you fix it?Willaims
@darmen Is there any progress on this issue? I'm experience the same problemSt
@Willaims Is there any progress on this issue?St
Nope, if the issue is actually the same as mine, it is something only your hosting provider can fix. As mine did after a few days (and a rather long issue ticket).Osugi
No wait, I remembered there was a temporary fix I found, read here: serverfault.com/questions/512744/… and good luck!Osugi
F
20

First, do a direct connection with Telnet:

telnet smtp-relay.sendinblue.com 587
Trying 94.143.17.4...

This is the basic connection troubleshooting, and works with any provider or port. Replace SendBlue and the 587 port with your actual hostname/port.

If you get this error:

telnet: Unable to connect to remote host: Connection timed out

then, the problem isn't in Rails.

In the above example, the problem is in the port number. Services like sendinblue or mandrill (I believe gmail too) don't support the 587 port anymore. "2525" is the new "587".


If you get a timeout on telnet, check this:

  1. hostname: it's usual to people use "smtp.sendinblue.com" instead of "stmp-relay.sendinblue.com", "smtp.mandrill.com" instead of "smtp.mandrillapp.com", and so on.
  2. port: 587 is obsolete. Major providers are now using 2525 instead. Major cloud services like DigitalOcean, block outgoing connections to 587 as well. That's why it will work on your pc, but not on your server. I won't even mention the "25" port, which is even more obsolete than 587. Also, some providers use specific non-default ones instead or imap.
  3. ipv6 vs ipv4: check if the hostname is being translated as a IPv4. If not, try to disable IPv6 (see others answers).
  4. hostname resolution: run the same telnet command on a machine that you know the email sending is working. Check if the translated ip (the xxx part of "Trying xxx...") is the same. If not, go back to your server and replace the hostname with this ip. If works, change your /etc/hosts and force the hostname to use this ip.
Foetid answered 27/4, 2016 at 5:1 Comment(2)
how do you do you the same connection when using ruby on rails?Headcloth
then what to do if the telnet is connected?Dunt
T
17

I probably had the same issue, my production application didn't send mails although everything in development was working fine. I also got the "Net::OpenTimeout" error.

My problem was that i was using a Google server in production, and it blocks ports 25, 465 and 587 on outbound connections.

Since I was using Mandrill for sending mails, I was able to switch the connecting port from 587 to 2525 and everything is okay now.

Tisdale answered 24/6, 2014 at 8:2 Comment(4)
I'm also using google cloud and changed my smtp port from 587 to 2525 and works fine! Thanks!Madid
I had an iptables ruleset that didn't allow output connections to port 465 (self managed server)Dorolice
@lucianosousa, thanks, I had to use 2587, described here docs.aws.amazon.com/ses/latest/DeveloperGuide/smtp-connect.htmlSheave
Also using GCP and you saved me a massive headache tracking this down. Thank you!Boni
O
14

Here is also a temporary fix that may come in handy while waiting for your hosting provider to fix the issue:

Add the following lines to /etc/sysctl.conf:

#disable ipv6
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.ipv6.conf.lo.disable_ipv6 = 1

Now reload the sysctl service using the following command:

sudo sysctl -p

Now the apps are able to send emails again.

You can always know if IPv6 is enabled calling

cat /proc/sys/net/ipv6/conf/all/disable_ipv6

from terminal. Two possible answers: 0 => IPv6 is enabled; 1 => IPv6 disabled.

From: https://serverfault.com/questions/512744/timeout-error-in-all-my-apps-for-every-call-to-smtp-servers

Osugi answered 15/10, 2013 at 9:18 Comment(3)
Note that link-only answers are discouraged, SO answers should be the end-point of a search for a solution (vs. yet another stopover of references, which tend to get stale over time). Please consider adding a stand-alone synopsis here, keeping the link as a reference.Baseburner
This one did not work for me even after restarting the sysctl.conf to reloadFlori
I would like to add something in your solution @DuArme , we need to reload the sysctl configs after adding these lines as sudo sysctl -p. Only after reloading sysctl , it will workShedd
M
5

The issue was due to an IPv6 misconfiguration on the production server and has now been fixed.

Mukden answered 22/4, 2013 at 8:34 Comment(7)
Same problem here, do you know how to spot that misconfiguration? Is there some log file to lookup or something?Osugi
Something I noticed: when I try to connect to smpt.gmail.co via command line it works, but first it tries with the IPv6 address and waits for SEVERAL seconds, only after this long period of time it tries with the simple IP and connects! Maybe is during the first time that my app goes timeout... now the question is: can this be a symptom to spot this kind of misconfiguration?Osugi
I'm not sure if this is the issue, but I'm getting the same symptoms... how'd you fix it?Willaims
@darmen Is there any progress on this issue? I'm experience the same problemSt
@Willaims Is there any progress on this issue?St
Nope, if the issue is actually the same as mine, it is something only your hosting provider can fix. As mine did after a few days (and a rather long issue ticket).Osugi
No wait, I remembered there was a temporary fix I found, read here: serverfault.com/questions/512744/… and good luck!Osugi
E
5

You can configure Ubuntu to prefer IPv4 over IPv6. This way you will be able to send emails and access IPv6-only sites. Edit /etc/gai.conf and uncomment the following line:

precedence ::ffff:0:0/96 100
Endothelioma answered 29/4, 2015 at 9:34 Comment(0)
T
3

Try this, if all above fails

I got it solved by adding this in application.rb under config

 require 'net/http'

 require 'openssl'

 require 'resolv-replace'
Tarn answered 6/9, 2017 at 8:19 Comment(0)
S
0

If you (or the internet in this case as this question is the first result for this problem) are testing Mailgun, you may get this error if you're using port 25. Change the port to 587 worked, even though their docs/quick links says 25 is ok to use.

Sideband answered 18/6, 2016 at 10:51 Comment(1)
Funny enough, port 587 wasn't working for me, and port 25 fixed it.Hide
K
0

I added these to /etc/gai.conf in CentOS7 and it worked.

label       ::1/128        0
label       ::/0           1
label       2002::/16      2
label       ::/96          3
label       ::ffff:0:0/96  4
precedence  ::1/128        50
precedence  ::/0           40
precedence  2002::/16      30
precedence  ::/96          20
precedence  ::ffff:0:0/96  100

http://blog.asiantuntijakaveri.fi/2014/12/prefer-ipv4-over-ipv6-on-centos-6.html:title

Klemperer answered 1/10, 2016 at 9:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.