Forum: Ruby-core [ruby-trunk - Bug #8067][Open] Checking a network connection in a loop never succeeds even if the co

Cb603807b64204c992bbfb0deed0aa64?d=identicon&s=25 l0x (Markus Lux) (Guest)
on 2013-03-10 13:10
(Received via mailing list)
Issue #8067 has been reported by l0x (Markus Lux).

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

Author: l0x (Markus Lux)
Status: Open
Priority: Normal
Assignee:
Category:
Target version:
ruby -v: ruby 2.0.0p0 (2013-02-24 revision 39474) [x86_64-linux]


=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/checki...

=end
C3f1f969025de9c8661055c2110d6c55?d=identicon&s=25 steakknife (Barry Allard) (Guest)
on 2013-04-07 22:23
(Received via mailing list)
Issue #8067 has been updated by steakknife (Barry Allard).


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!
    It works!
    It works!
    It works!
    It works!
    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
    It works!
    It works!
    It works!
    It works!
    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
    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 <main>'
            from ./net_test.rb:4:in `loop'
            from ./net_test.rb:4:in `<main>'

Haven't tried Linux or FreeBSD yet.
----------------------------------------
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-38339

Author: l0x (Markus Lux)
Status: Open
Priority: Normal
Assignee:
Category:
Target version:
ruby -v: ruby 2.0.0p0 (2013-02-24 revision 39474) [x86_64-linux]


=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/checki...

=end
This topic is locked and can not be replied to.