[ruby-core:93799] [Ruby master Bug#8067] Checking a network connection in a loop never succeeds even if the connection is available.

From: merch-redmine@...
Date: 2019-07-15 22:48:04 UTC
List: ruby-core #93799
Issue #8067 has been updated by jeremyevans0 (Jeremy Evans).

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?

----------------------------------------
Bug #8067: Checking a network connection in a loop never succeeds even if the connection is available.
https://bugs.ruby-lang.org/issues/8067#change-79654

* Author: l0x (Markus Lux)
* Status: Feedback
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: ruby 2.0.0p0 (2013-02-24 revision 39474) [x86_64-linux]
* Backport: 
----------------------------------------
=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



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>

In This Thread

Prev Next