Project

General

Profile

Actions

Bug #8067

closed

Checking a network connection in a loop never succeeds even if the connection is available.

Added by l0x (Markus Lux) over 11 years ago. Updated over 5 years ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 2.0.0p0 (2013-02-24 revision 39474) [x86_64-linux]
Backport:
[ruby-core:53279]

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.

[1] http://stackoverflow.com/questions/15321260/checking-a-network-connection-in-a-loop-never-succeeds-even-if-the-connection-is

=end

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0