Timeout error?

Discussion in 'Ruby' started by Mark Probert, Oct 4, 2004.

  1. Mark Probert

    Mark Probert Guest

    Hi ..

    I have the following code:


    def alive?(host,port)
    ok = false
    begin
    Timeout::timeout(30) {
    begin
    t = TCPSocket.new(host,port)
    t.close
    ok = true
    rescue Exception => e
    @exception = e
    end
    }
    rescue Timeout::Error => e
    @exception = e
    end
    return ok
    end

    In a known failure case, TCPSocket will fail (no host connectivity).
    This will cause timeout() to trigger an exception that will get caught by
    the Timeout::Error handler. Right?

    However,

    13:56 (hobbes)$ ruby foo.rb
    Exception `Errno::ETIMEDOUT' at ./bsn.rb:150 - Connection timed out -

    13:57 (hobbes)$ ruby -v
    ruby 1.8.1 (2003-12-25) [i386-cygwin]

    What is the right way of handling this exception?

    --
    -mark. (probertm @ acm dot org)
    Mark Probert, Oct 4, 2004
    #1
    1. Advertising

  2. Mark Probert

    Bill Atkins Guest

    Add a rescue clause for the exception you're getting:

    begin
    ...
    rescue Timeout::Error => e
    ...
    rescue Errno::ETIMEDOUT => e
    ...
    end

    Is that what you're looking for?

    Bill Atkins

    On Tue, 5 Oct 2004 06:09:53 +0900, Mark Probert <> wrote:
    >
    > Hi ..
    >
    > I have the following code:
    >
    > def alive?(host,port)
    > ok = false
    > begin
    > Timeout::timeout(30) {
    > begin
    > t = TCPSocket.new(host,port)
    > t.close
    > ok = true
    > rescue Exception => e
    > @exception = e
    > end
    > }
    > rescue Timeout::Error => e
    > @exception = e
    > end
    > return ok
    > end
    >
    > In a known failure case, TCPSocket will fail (no host connectivity).
    > This will cause timeout() to trigger an exception that will get caught by
    > the Timeout::Error handler. Right?
    >
    > However,
    >
    > 13:56 (hobbes)$ ruby foo.rb
    > Exception `Errno::ETIMEDOUT' at ./bsn.rb:150 - Connection timed out -
    >
    > 13:57 (hobbes)$ ruby -v
    > ruby 1.8.1 (2003-12-25) [i386-cygwin]
    >
    > What is the right way of handling this exception?
    >
    > --
    > -mark. (probertm @ acm dot org)
    >
    >
    Bill Atkins, Oct 5, 2004
    #2
    1. Advertising

  3. Mark Probert

    Mark Probert Guest

    Hi ..

    Bill Atkins <> wrote:
    >
    > Add a rescue clause for the exception you're getting: ...
    > Is that what you're looking for?
    >


    Not quite.

    I guess the real qustion is why Timeout::Error doesn't trap
    Errno::ETIMEDOUT. The socket times out before the timeout() and raises a
    different timeout error. Perhaps it would be better if, when a block is
    wrapped in a Timeout::timeout, that all subservient timeouts should be
    caught by the single rescue.

    It does raise the question why the generic rescue isn't triggered.


    > Bill Atkins
    >
    > On Tue, 5 Oct 2004 06:09:53 +0900, Mark Probert
    > <> wrote:
    >>
    >> Hi ..
    >>
    >> I have the following code:
    >>
    >> def alive?(host,port)
    >> ok = false
    >> begin
    >> Timeout::timeout(30) {
    >> begin
    >> t = TCPSocket.new(host,port) # <--- line 150
    >> t.close
    >> ok = true
    >> rescue Exception => e # why not caught here?
    >> @exception = e
    >> end
    >> }
    >> rescue Timeout::Error => e # or here?
    >> @exception = e
    >> end
    >> return ok
    >> end
    >>
    >> In a known failure case, TCPSocket will fail (no host connectivity).
    >>
    >> However,
    >>
    >> 13:56 (hobbes)$ ruby foo.rb
    >> Exception `Errno::ETIMEDOUT' at ./bsn.rb:150 - Connection timed out -
    >>
    >> 13:57 (hobbes)$ ruby -v
    >> ruby 1.8.1 (2003-12-25) [i386-cygwin]
    >>
    >> What is the right way of handling this exception?
    >>
    >> --
    >> -mark. (probertm @ acm dot org)
    >>
    >>

    >
    >
    >




    --
    -mark. (probertm @ acm dot org)
    Mark Probert, Oct 5, 2004
    #3
  4. Mark Probert

    Bill Atkins Guest

    Hmm. Well, your rescue clause is looking specifically for exceptions
    of type Timeout::Error or of a subclass of that class. Since
    Errno::ETIMEDOUT isn't descended from Timeout::Error, the rescue
    clause never gets called. If you want a generic rescue to catch
    everything, try 'rescue Exception => e'

    Bill

    On Tue, 5 Oct 2004 14:04:53 +0900, Mark Probert <> wrote:
    > Hi ..
    >
    > Bill Atkins <> wrote:
    > >
    > > Add a rescue clause for the exception you're getting: ...
    > > Is that what you're looking for?
    > >

    >
    > Not quite.
    >
    > I guess the real qustion is why Timeout::Error doesn't trap
    > Errno::ETIMEDOUT. The socket times out before the timeout() and raises a
    > different timeout error. Perhaps it would be better if, when a block is
    > wrapped in a Timeout::timeout, that all subservient timeouts should be
    > caught by the single rescue.
    >
    > It does raise the question why the generic rescue isn't triggered.
    >
    >
    > > Bill Atkins
    > >
    > > On Tue, 5 Oct 2004 06:09:53 +0900, Mark Probert
    > > <> wrote:
    > >>
    > >> Hi ..
    > >>
    > >> I have the following code:
    > >>
    > >> def alive?(host,port)
    > >> ok = false
    > >> begin
    > >> Timeout::timeout(30) {
    > >> begin
    > >> t = TCPSocket.new(host,port) # <--- line 150
    > >> t.close
    > >> ok = true
    > >> rescue Exception => e # why not caught here?
    > >> @exception = e
    > >> end
    > >> }
    > >> rescue Timeout::Error => e # or here?
    > >> @exception = e
    > >> end
    > >> return ok
    > >> end
    > >>
    > >> In a known failure case, TCPSocket will fail (no host connectivity).
    > >>
    > >> However,
    > >>
    > >> 13:56 (hobbes)$ ruby foo.rb
    > >> Exception `Errno::ETIMEDOUT' at ./bsn.rb:150 - Connection timed out -
    > >>
    > >> 13:57 (hobbes)$ ruby -v
    > >> ruby 1.8.1 (2003-12-25) [i386-cygwin]
    > >>
    > >> What is the right way of handling this exception?
    > >>
    > >> --
    > >> -mark. (probertm @ acm dot org)
    > >>
    > >>

    > >
    > >
    > >

    >
    >
    >
    >
    > --
    > -mark. (probertm @ acm dot org)
    >
    >
    Bill Atkins, Oct 5, 2004
    #4
  5. On Tue, Oct 05, 2004 at 02:04:53PM +0900, Mark Probert wrote:
    > I guess the real qustion is why Timeout::Error doesn't trap
    > Errno::ETIMEDOUT. The socket times out before the timeout() and raises a
    > different timeout error. Perhaps it would be better if, when a block is
    > wrapped in a Timeout::timeout, that all subservient timeouts should be
    > caught by the single rescue.
    >
    > It does raise the question why the generic rescue isn't triggered.


    It is for me (ruby-1.8.2p2, FreeBSD), so perhaps this is either a Windows
    platform issue, or something from 1.8.1 which has been fixed.

    $ cat foo.rb
    require 'socket'
    require 'timeout'

    def alive?(host,port)
    ok = false
    begin
    Timeout::timeout(30) {
    begin
    t = TCPSocket.new(host,port) # <--- line 150
    t.close
    ok = true
    rescue Exception => e # why not caught here?
    @exception = e
    end
    }
    rescue Timeout::Error => e # or here?
    @exception = e
    end
    return ok
    end

    p alive?('192.0.2.1',1234)
    $ time ruby foo.rb
    false

    real 0m30.299s
    user 0m0.076s
    sys 0m0.024s

    << change timeout to 120 >>

    $ time ruby foo.rb
    false

    real 1m15.155s
    user 0m0.074s
    sys 0m0.025s

    (75 secs is the default TCP SYN timeout in FreeBSD)

    Regards,

    Brian.
    Brian Candler, Oct 6, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bob Johnson
    Replies:
    0
    Views:
    3,724
    Bob Johnson
    Aug 7, 2003
  2. Do
    Replies:
    2
    Views:
    6,327
  3. Mark Probert

    Timeout::timeout and Socket timeout

    Mark Probert, Oct 6, 2004, in forum: Ruby
    Replies:
    1
    Views:
    1,251
    Brian Candler
    Oct 6, 2004
  4. vishnu
    Replies:
    0
    Views:
    334
    vishnu
    May 31, 2006
  5. vishnu
    Replies:
    0
    Views:
    187
    vishnu
    May 31, 2006
Loading...

Share This Page