Bug #8067
closedChecking a network connection in a loop never succeeds even if the connection is available.
Description
=begin
I want to check for a connection to a specific server with Ruby. So I just wrap the HTTP request into a begin/rescue construct and try again in a few seconds if the connection is not available.
((Problem)): If I start the program with no connection available (link down) it will never succeed in finding a connection even if at some time the connection is available (e.g. via ping, browser request).
((Test case:))
require 'net/http'
loop do
begin
uri = URI.parse 'http://www.example.com'
resp = Net::HTTP.get uri
rescue SocketError => se
puts se
sleep 3
next
rescue Exception => e
puts e
break
end
puts 'It works!'
break
end
((Steps to reproduce:))
(1) Take network link down
(2) Start program
(3) -> getaddrinfo: Name or service not known
(4) -> ...
(5) Turn network link on again
(6) Still no connection, same error
((Expected behavior:)) Don't throw SocketException as soon as a connection is available.
((Ruby versions tested:))
ruby 1.9.3p392 (2013-02-22 revision 39386) [x86_64-linux]
ruby 2.0.0p0 (2013-02-24 revision 39474) [x86_64-linux]
This may only affect Linux since there exist reports on [1] where people commented that the program works as expected on Windows and Mac.
=end
Updated by steakknife (Barry Allard) over 11 years ago
Modified a bit for interactive looping:
#!/usr/bin/env ruby
require 'net/http'
loop do
sleep 1
begin
uri = URI.parse 'http://www.example.com'
resp = Net::HTTP.get uri
puts 'It works!'
rescue SocketError => se
puts se
next
rescue Exception => e
puts e
next
end
end
Mac 10.7.5 + 1.9.3-p392 output:
It works! # network on here
It works!
It works!
It works!
It works!
No route to host - connect(2) # network off here
getaddrinfo: nodename nor servname provided, or not known
getaddrinfo: nodename nor servname provided, or not known
getaddrinfo: nodename nor servname provided, or not known
getaddrinfo: nodename nor servname provided, or not known
getaddrinfo: nodename nor servname provided, or not known
getaddrinfo: nodename nor servname provided, or not known
getaddrinfo: nodename nor servname provided, or not known # network on here
It works!
It works!
It works!
It works! # network off here
No route to host - connect(2)
getaddrinfo: nodename nor servname provided, or not known
getaddrinfo: nodename nor servname provided, or not known
getaddrinfo: nodename nor servname provided, or not known
getaddrinfo: nodename nor servname provided, or not known
getaddrinfo: nodename nor servname provided, or not known
getaddrinfo: nodename nor servname provided, or not known
getaddrinfo: nodename nor servname provided, or not known
getaddrinfo: nodename nor servname provided, or not known
getaddrinfo: nodename nor servname provided, or not known # network on here
It works!
It works!
It works!
^C
It works!
It works!
^C./net_test.rb:5:in sleep': Interrupt from ./net_test.rb:5:in
block in '
from ./net_test.rb:4:in loop' from ./net_test.rb:4:in
'
Haven't tried Linux or FreeBSD yet.
Updated by jeremyevans0 (Jeremy Evans) over 5 years ago
- Status changed from Open to Feedback
The script runs as expected on OpenBSD with ruby 2.6. I expect the issue you are experiencing is probably specific to your environment. Can you recreate this issue with a supported version of Ruby, and if so provide more details regarding your environment?
Updated by jeremyevans0 (Jeremy Evans) over 5 years ago
- Status changed from Feedback to Closed