[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Socket broken due to socket resource leak???

uncutstone

5/15/2006 3:37:00 PM

It is really a triky problem. My OS is windows 98, ruby version is ruby
1.8.2 (2004-12-25) [i386-mswin32].

Following code is used to illustrate the problem I encountered. Before
you execute it, please ensure to be offline, so it can be run very fast
.. Please notice the line in tcpclient.rb "145.times do", it is the
key issue.

Let me explain it.

Tcpserver.rb is a local tcp server simply sends a hello message
whenever a client is connected. Tcpclient.rb first makes a connect to
the local tcp server( this connection will be read later), then tries
many times to connect to a bad ip addresses which indeed can never be
connected. So every try will raise a exception " Unknown Error -
connect(2)". This exception is simply ignored and the client
continues trying.

The wiered thing is , after certain number of tries, the established
connection between the tcp client and the local tcp server is broken.
So the line "goodConn.gets" also raises a exception . Even more,
what exception "goodConn.gets" raised is realted to the number of
tries to connect to the bad ip.

a. if times is less than 140 , it acts normally, nothing wrong, a hello
message is received from the local tcp server. I got this:

Exception raised: Unknown Error - connect(2)
Hello from local tcp server

b. If 140< times <145, I got this :

Exception raised: Unknown Error - connect(2)
tcpclient.rb:19:in `gets': Bad file descriptor (Errno::EBADF)
from tcpclient.rb:19:in `testsocket'
from tcpclient.rb:24

c. if times > 145, I got this:
Exception raised: Unknown Error - connect(2)
tcpclient.rb:19:in `gets': Invalid argument (Errno::EINVAL)
from tcpclient.rb:19:in `testsocket'
from tcpclient.rb:24

General speaking, it seems a resource leak happens every time a
"Unknown Error-connect(2)" exception raised when the tcpclient
failed to connnect to a bad ip address. So after a substantial number
of tries, the leak accumulates and the socket is crushed. Is it a bug
of socket library??

Code is listed below, if you are insterested, please try it, run the
ftpserver.rb first.
1. tcpserver.rb:

require 'socket'
server = TCPServer.new('localhost',21)
while(session = server.accept)
session.print "Hello from local tcp server"
session.close
end


2. tcpclient.rb:

require 'socket'
def testsocket
goodAddr = "localhost"
goodConn = TCPSocket.new(goodAddr,21)

badAddr = "137.144.70.12" #no connection could established on this
address
145.times do
badconn = nil
begin
badconn = TCPSocket.new(badAddr,21)
rescue Exception => aException
puts "Exception raised: #{aException.to_s}"
ensure
badconn.close if @badconn and not @badconn.closed?
end
end

s = goodConn.gets
puts s
goodConn.close
end

testsocket

65 Answers

Robert Klemme

5/15/2006 5:12:00 PM

0

uncutstone wrote:
> It is really a triky problem. My OS is windows 98, ruby version is ruby
> 1.8.2 (2004-12-25) [i386-mswin32].
>
> Following code is used to illustrate the problem I encountered. Before
> you execute it, please ensure to be offline, so it can be run very fast
> . Please notice the line in tcpclient.rb "145.times do", it is the
> key issue.
>
> Let me explain it.
>
> Tcpserver.rb is a local tcp server simply sends a hello message
> whenever a client is connected. Tcpclient.rb first makes a connect to
> the local tcp server( this connection will be read later), then tries
> many times to connect to a bad ip addresses which indeed can never be
> connected. So every try will raise a exception " Unknown Error -
> connect(2)". This exception is simply ignored and the client
> continues trying.
>
> The wiered thing is , after certain number of tries, the established
> connection between the tcp client and the local tcp server is broken.
> So the line "goodConn.gets" also raises a exception . Even more,
> what exception "goodConn.gets" raised is realted to the number of
> tries to connect to the bad ip.

This comes as no surprise as the server closes the connection after it
has sent his hello message. Since you don't immediately retrieve the
message it may well be that the connection close overrides the buffered
data and you never see it.

Kind regards

robert

uncutstone

5/15/2006 5:58:00 PM

0


Robert Klemme wrote:
> This comes as no surprise as the server closes the connection after it
> has sent his hello message. Since you don't immediately retrieve the
> message it may well be that the connection close overrides the buffered
> data and you never see it.

It is not so simple. Please try anotherclient.rb
require 'socket'

def testsocket
goodAddr = "localhost"
goodConn = TCPSocket.new(goodAddr,21)

sleep 30

s = goodConn.gets
puts s
goodConn.close
end

testsocket
I got:
Hello from local tcp server

After 30 seconds of sleep, tcp server is sure has closed the session,
client can still get the result .

You can also add a line "session.gets" in tcpserver.rb before
&#8220;session.print &#8216;Hello from local tcp server&#8217;&#8221;
and a goodConn.puts("hello") before "goodConn.gets", so the tcpserver
will wait for tcpclient before it sends hello message.
But what I have described remains , socket broken due to substantial
times of failed connection attempt.

uncutstone

5/15/2006 6:05:00 PM

0

I rewrite the code, socket broken still happens:

tcpserver.rb:
require 'socket'

server = TCPServer.new('localhost',21)
while(session = server.accept)
puts "Client Request: #{session.gets}"
session.print "Hello from local tcp server"
session.close
end

tcpclient.rb:

require 'socket'

def testsocket
goodAddr = "localhost"
goodConn = TCPSocket.new(goodAddr,21)

badAddr = "137.144.70.12" #no connection could established on this
address
160.times do
badconn = nil
begin
badconn = TCPSocket.new(badAddr,21)
rescue Exception => aException
puts "Exception raised: #{aException.to_s}"
ensure
badconn.close if @badconn and not @badconn.closed?
end
end

goodConn.puts("hello")
s = goodConn.gets
puts s
goodConn.close
end

testsocket

uncutstone

5/15/2006 6:14:00 PM

0

Be sure to be offline when you try the code above. Being offline will
make it run fast, otherwise it will take long time to run.

uncutstone

5/16/2006 2:21:00 PM

0

Maybe this post is too long to read. Let me make it simple.

General speaking, I think it is likely a bug of ruby's socket library.
It seems that a resource leak happens every time TCPSocket.new failed.

Do you agree it is a bug? Or I missunderstood something. Any opinion is
highly appreciated.

BR549

2/24/2013 5:23:00 PM

0

On Feb 24, 11:15 am, Mitchell Holman <nomailverizon.net> wrote:
> David Hartung <david@h0tm*il.com> wrote innews:jtqdnSHhtsK6BLTMnZ2dnUVZ_gOdnZ2d@giganews.com:
>
>
>
>
>
> > On 02/23/2013 09:21 PM, Mitchell Holman wrote:
> >> David Hartung <david@h0tm*il.com> wrote in news:J6OdnYdJqe2_
> >> 4rTMnZ2dnUVZ_jWdn...@giganews.com:
>
> >>> On 02/23/2013 06:35 PM, darnell wrote:
> >>>> On 2/23/2013 3:14 PM, Mitchell Holman wrote:
> >>>>>> And YOU spent how many years as a POW in Hanoi, having your
> >> shoulders
> >>>>>>> ripped out of socket and your bones broken ??
>
> >>>>>       Where was never-spent-a-day-in uniform
> >>>>> Cruz when Hegel was fighting in Vietnam?
> >>>>> For that matter where were you?
>
> >>>> Or, with that big flapping yap of yours, where were YOU, twinky?
>
> >>> Yes Mitchell, where were you?
>
> >>      I was actively protesting the war alongside
> >> the VVAW, who were kind enough to let me march
> >> with them in the mass DC demonstration in May of
> >> 1971.
>
> >>      Where were you in 1971, and what were you
> >> doing to stop that godawful war?
>
> > Had you and other protesters been responsible citizens, the war could
> > have ended in 1968:
>
>      My "other protesters" were Vietnam Vets. If you
> want to make a case they are not "responsible citizens"
> go right ahead.- Hide quoted text -
>
> - Show quoted text -

And how many returning Vietnam vets did you spit on and call a baby
killer? Did you ever serve in the military?

darnell

2/24/2013 5:54:00 PM

0

On 2/24/2013 10:15 AM, Mitchell Holman wrote:
> My "other protesters" were Vietnam Vets. If you
> want to make a case they are not "responsible citizens"
> go right ahead.
>
Were they engaging in criminal acts?


Were you?

Mitchell Holman

2/24/2013 6:22:00 PM

0

"Robert Westergrom,1900 Harvey rd.,Wilmington,D.E"
<burtonurny@gmail.com> wrote in
news:89cd80a7-e532-4cdc-bce4-fd1f3c688e56@u7g2000yqg.googlegroups.com:

> On Feb 24, 11:15?am, Mitchell Holman <nomailverizon.net> wrote:
>> David Hartung <david@h0tm*il.com> wrote
>> innews:jtqdnSHhtsK6BLTMnZ2dnUVZ_g
> OdnZ2d@giganews.com:
>>
>>
>>
>>
>>
>> > On 02/23/2013 09:21 PM, Mitchell Holman wrote:
>> >> David Hartung <david@h0tm*il.com> wrote in news:J6OdnYdJqe2_
>> >> 4rTMnZ2dnUVZ_jWdn...@giganews.com:
>>
>> >>> On 02/23/2013 06:35 PM, darnell wrote:
>> >>>> On 2/23/2013 3:14 PM, Mitchell Holman wrote:
>> >>>>>> And YOU spent how many years as a POW in Hanoi, having your
>> >> shoulders
>> >>>>>>> ripped out of socket and your bones broken ??
>>
>> >>>>> ? ? ? Where was never-spent-a-day-in uniform
>> >>>>> Cruz when Hegel was fighting in Vietnam?
>> >>>>> For that matter where were you?
>>
>> >>>> Or, with that big flapping yap of yours, where were YOU, twinky?
>>
>> >>> Yes Mitchell, where were you?
>>
>> >> ? ? ?I was actively protesting the war alongside
>> >> the VVAW, who were kind enough to let me march
>> >> with them in the mass DC demonstration in May of
>> >> 1971.
>>
>> >> ? ? ?Where were you in 1971, and what were you
>> >> doing to stop that godawful war?
>>
>> > Had you and other protesters been responsible citizens, the war
>> > could have ended in 1968:
>>
>> ? ? ?My "other protesters" were Vietnam Vets. If you
>> want to make a case they are not "responsible citizens"
>> go right ahead.- Hide quoted text -
>>
>> - Show quoted text -
>
> And how many returning Vietnam vets did you spit on and call a baby
> killer?


None.

Did you fight in the war you supported?



darnell

2/24/2013 6:31:00 PM

0

On 2/24/2013 11:22 AM, Mitchell Holman wrote:
>> And how many returning Vietnam vets did you spit on and call a baby
>> >killer?
>
> None.

Prove that now, Twinky.

dzweibach

2/24/2013 8:09:00 PM

0

"Robert Westergrom,1900 Harvey rd.,Wilmington,D.E" <burtonurny@gmail.com>
wrote:
> On Feb 24, 11:15=A0am, Mitchell Holman <nomailverizon.net> wrote:
> > David Hartung <david@h0tm*il.com> wrote
> > innews:jtqdnSHhtsK6BLTMnZ2dnUVZ_g=
> OdnZ2d@giganews.com:
> >
> >
> >
> >
> >
> > > On 02/23/2013 09:21 PM, Mitchell Holman wrote:
> > >> David Hartung <david@h0tm*il.com> wrote in news:J6OdnYdJqe2_
> > >> 4rTMnZ2dnUVZ_jWdn...@giganews.com:
> >
> > >>> On 02/23/2013 06:35 PM, darnell wrote:
> > >>>> On 2/23/2013 3:14 PM, Mitchell Holman wrote:
> > >>>>>> And YOU spent how many years as a POW in Hanoi, having your
> > >> shoulders
> > >>>>>>> ripped out of socket and your bones broken ??
> >
> > >>>>> =A0 =A0 =A0 Where was never-spent-a-day-in uniform
> > >>>>> Cruz when Hegel was fighting in Vietnam?
> > >>>>> For that matter where were you?
> >
> > >>>> Or, with that big flapping yap of yours, where were YOU, twinky?
> >
> > >>> Yes Mitchell, where were you?
> >
> > >> =A0 =A0 =A0I was actively protesting the war alongside
> > >> the VVAW, who were kind enough to let me march
> > >> with them in the mass DC demonstration in May of
> > >> 1971.
> >
> > >> =A0 =A0 =A0Where were you in 1971, and what were you
> > >> doing to stop that godawful war?
> >
> > > Had you and other protesters been responsible citizens, the war could
> > > have ended in 1968:
> >
> > =A0 =A0 =A0My "other protesters" were Vietnam Vets. If you
> > want to make a case they are not "responsible citizens"
> > go right ahead.- Hide quoted text -
> >
> > - Show quoted text -
>
> And how many returning Vietnam vets did you spit on and call a baby
> killer? Did you ever serve in the military?

That never happened. Urban myth. Some years back a guy wrote a book called
"Spitting Image" couldn't fine a single verified incident. Of course being
untrue won't keep you from believing it.