"Mark Probert" <probertm / acm.org> schrieb im Newsbeitrag
news:200502110912.31423.probertm / acm.org...
> Hi ..
>
>  On Friday 11 February 2005 00:20, Robert Klemme wrote:
> >
> > Which means ensure is never called if the acquire fails.  So I would
do it
> > like this:
> >
> >   <excellent version removed>
> >
> Thank you.
>
> Ara also mention, off-list, that I am catching the exception, otherwise
the
> app would be failing.  He suggested that the issue could also be with
how I
> am using @exception.
>
> I am not so sure that is the case.  The basic driver that I use looks
like:
>
>     require 'bsn_a.rb'
>     def run
>         threads = []
>         @addr.each do |ip_addr|
>             threads << Thread.new(ip_addr) do |node|
>               name, ip, usr, pwd = node.split(/:/)
>
>               bsn = BSN.new(node)  #### deliberately fail for now
>               bsn.user = usr
>               bsn.pwd  = pwd
>
>               puts "  .. trying to go to #{nn} (#{bsn.host})"
>        f = bsn.login    #### this is line 144
>               puts "  --> #{(f ? " got there" : " failed ")}
#{bsn.host}"
>             end
>         end
>         threads.each { |thr| thr.join }
>     end
>
> The program creates a BSN object then calls login().
>
>   def login
>       f = alive?
>       if not f
>           _pr "login --> host not alive (#{@exception})"  ### log to
file
>           return false
>       end
>       ### go on with login stuff
>   end
>
> So, it is calling alive? to find out if we can connect, before we try
and
> connect for real.
>
> My problem is that when I do this from my test harness, line 144 doesn't
throw
> an exception:
>
>
> 8:48 (kant)$ ruby test.rb
>   .. trying to go to Foo (Foo:10.10.10.5:foo:bar)
>   -->  failed  Foo:10.10.10.5:foo:bar
>   .. trying to go to Foo (Foo:10.10.10.5:foo:bar)
>   -->  failed  Foo:10.10.10.5:foo:bar
>
> However, from the 'real' program it does throw one at line 144 ????
>
> 8:49 (kant)$ ruby bsncoll.rb -g -n eeua.txt -c flow.txt -d data
>   .. trying to go to Foo (Foo:10.10.10.5:foo:bar)
>   Exception `SocketError' at ./bsn_a.rb:144 - getaddrinfo: hostname
>     nor servname provided, or not known
>   -->  failed  Foo:10.10.10.5:foo:bar
>   .. trying to go to Bar (Bar:10.10.10.6:foo:bar)
>   Exception `SocketError' at ./bsn_a.rb:144 - getaddrinfo: hostname
>     nor servname provided, or not known
>   -->  failed  Bar:10.10.10.6:foo:bar
>   - post-processing data ... please wait
>
> Ara is right that the program doesn't die.  However, it is, in the
second case
> throwing a message to stderr that the first program doesn't do.  I am
not
> certain, but I think that this may be part of my memory leak problem
that I
> mentioned in another thread.  And I don't seem to be able to make it go
away!

Did you try with "Thread.abort_on_exception = true"?  Maybe you just don't
see the exception in one of the cases.

> This behaviour seems abnormal.  If the exception is thrown at 144, then
I
> should be able to rescue the way that Robert points out.  Except, it
doesn't
> in this case.

Currently I have no explanation for your problem.  Is it really the same
ruby etc.?

Personally I would not do a test beforehand to see whether I can connect.
I'd just connect, catch exceptions and handle them.  Because even if
alive? succeeded your connect can still fail (for example if something
happened on the network between your test and the real connect).

Btw, you could as well implement something like BSN.open.

class BSN
  def self.open(node,user,pass)
    bsn = self.new
    bsn.user = user
    bsn.pass = pass

    bsn.connect # socket connect
    begin
      yield bsn
    ensure
      bsn.close # logout and disconnect
    end
  end
end

Then you can do

BSN.open(node,user,pass) do |bsn|
  bsn.login
  ...
end

and be sure that the bsn is always properly closed.

Kind regards

    robert