f



Socket broken due to socket resource leak???

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

0
uncutstone (51)
5/15/2006 3:37:24 PM
comp.lang.ruby 48885 articles. 0 followers. Post Follow

5 Replies
664 Views

Similar Articles

[PageSpeed] 39

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
0
bob.news (3807)
5/15/2006 5:11:45 PM
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.

0
uncutstone (51)
5/15/2006 5:58:25 PM
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

0
uncutstone (51)
5/15/2006 6:04:49 PM
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.

0
uncutstone (51)
5/15/2006 6:13:30 PM
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.

0
uncutstone (51)
5/16/2006 2:21:03 PM
Reply:

Similar Artilces:

Ruby Socket to Java Socket
Hello ! I have some problem trying to establish dialog between a Ruby socket(client) and a Java server socket (java.net.ServerSocket). Everytime I try to write on the server socket I get an "Invalid stream header" error from the server... s = TCPSocket.open("host", 9061) s.write("test \n"); => Error Can someone give a little hint on how to start inthe right way on this ? Thx a lot ! Seurdge -- Posted via http://www.ruby-forum.com/. Ok, I have experiment a lot and found that : When the Java Server use : in = requestSocket.getInputStream(); It wo...

C socket to Ruby socket
Hello What is the correct way to convert a C socket (int) to a Ruby Socket object in a C extension? Something like int s = socket(...); VALUE sock = rb_some_func(s); In my extension there's a callback function that receives a socket as an argument, and I'd like to pass that socket as an argument to a ruby block, so I need to convert it to a Socket object first. Thanks in advance, Andre On 9/5/06, Andre Nathan <andre@digirati.com.br> wrote: > Hello > > What is the correct way to convert a C socket (int) to a Ruby Socket > object in a C extension? Something l...

socket.socket.makefile
I'm writing an alternative socket module, and have come across the code for the makefile call, which mentions the following: (XXX refactor to share code?) http://hg.python.org/cpython/file/27adb952813b/Lib/socket.py#l149 Has this been refactored elsewhere? Is there something I can use to wrap the SocketIO inheriting from RawIOBase in all the variants the io.open/socket.makefile functions return for me? ...

Help translating C# socket code equivalent Ruby socket code
Possible to help in translating the following C# socket code to Ruby socket code please. What specific socket code lines needs to do following task. using System.Net.Sockets; Socket clientSock; clientSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); receivedBytesLen = clientSock.Receive(Recievbuf, bufoffset, packetsize, 0); int fileNameLen = BitConverter.ToChar(Recievbuf, 0); string fileName = "image1.text";//Encoding.ASCII.GetString(Recievbuf, 4, fileNameLen); // MessageBox.Show("Client:{0} connected & File {1} started received."...

socket.sendall(), non-blocking sockets, and multi-threaded socket sending
My application requires sending a large piece (~2MB) of data to several devices on a network via TCP sockets. I have experimented with different methods for doing this and this has raised some questions about the implementation of Python sockets. (both methods use blocking sockets) Method 1: Calls socket.sendall(data) for each device in sequence, all in a single thread. Method 2: Each socket has its own thread that calls socket.send(datachunk) iteratively until all data is sent. So Method 1 sends all data to device1, then send all data to device2, etc... until all data is sent to all devic...

socket/sendmsg seems to drop packets socket/sendmsg seems to drop packets socket/sendmsg seems to drop packets socket/sendmsg seems to drop packets socket/sendmsg seems to drop packets... how to
Hi, I am using the function sendmsg to send for example 1000 packets over a socket from one computer to another. The function call always succeedes, i.e., -1 is never returned. However, when using tcpdump on the sending computer, I see that only 984 out of 1000 packets are actually sent out from the computer (I see the same 984 packets with tcpdump on the receiving computer). How should I debug this to see where the 16 packets are dropped? It seems as if they are dropped somewhere in the Linux kernel. Brs, Markus dspfun <dspfun@hotmail.com> writes: > I am using the function sendm...

SOCKET:SOCKET-STREAM missing?
I'm using clisp on a Windows2000 machine, playing with the SOCKET features, but I can't get anything to stream out. I get the feeling I should be doing something with SOCKET:SOCKET-STREAM somewhere, because... (defun x (socket:socket-server 5552)) (print "Hello" x) ....give me... *** - PRINT: argument #<SOCKET-SERVER 0.0.0.0:5552> should be a stream ....as an error. I can connect to 5552 with telnet and then use SOCKET:SOCKET-SERVER-CLOSE to close the connections. I just can't send data to it. I would use SOCKET:SOCKET-STREAM as I think I should but it gives...

socket.socket.settimeout implementation
When using socket.socket.settimeout we normally only guard against "socket.timeout" exception.Now the implementation of "settimeout" in "Python-2.4.3/Modules/socketmodule.c" sets the socket fd to nonblocking and uses "select()" to timeout as seen below in line 1487 and 1386 : static PyObject * 1470 sock_settimeout(PySocketSockObject *s, PyObject *arg) 1471 { 1472 double timeout; .............................. 1485 1486 s->sock_timeout = timeout; 1487 internal_setblocking(s, timeout < 0.0); 1488 1489 ...

Socket() failed for UDP socket
Hello, I have recently started getting an error message, whenever i boot up my laptop. The error is : " Could not start DB server: Socket() failed for UDP socket: (10106) the requested service provider could not be loaded or initialized" The only odd thing, i have noticed after i have started getting this error is... I am not able to connect to my neighbor's wireless network (says it has limited or no connectivity). Looking forward to any help in this regard.. thanks, KR "Kishore_R" <kishore.racherla@gmail.com> wrote in news:1128986695.202568.119580@g43g200...

Ruby and socket.so
Hi, When I "require socket" in my application I get following error. /usr/local/lib/ruby/1.8/x86_64-linux/socket.so: cannot open shared object file: No such file or directory - /usr/local/lib/ruby/1.8/x86_64-linux/socket.so The socket.so is of course located in above directory. Ruby test file. require "socket" if $0 == __FILE__ print "Hello World" end My ruby version is: ruby 1.8.2 (2004-12-25) [x86_64-linux] Any help would be appriciated. -- Peter Lerche ------------------------------------- Linux/BSD Dedicated Server Hosting Advanced Server Tools ...

Socket#gets and Socket#puts
We really ought to think about documenting the Socket class tree. :) Okay, I'm learning with a little "Trial by Fire" over here. I'm building a threaded server. I'm using Socket#gets for reading and that's going fine. I've learned that it returns nil, when the connection is closed. Does it throw exceptions under any conditions and if so, which ones? What about Socket#puts? If it's waiting to send something, what happens if the connection is closed? Should I expect any exceptions on this end of the equation? Thanks. James Edward Gray II On ...

Socket operation on non-socket
Sometimes I get an error message when I try to check my mail that says "Socket operation on non-socket." Can anyone tell me what could be causing it? Thanks, Cliff Lewis Your email server was too busy to respond within the alloted time out. It happens sometimes when the server is too busy. If you try again it probably works? On Tue, 08 Mar 2005 20:27:15 -0500, Cliff Lewis <clewis@gwis.com> wrote: >Sometimes I get an error message when I try to check my mail that says >"Socket operation on non-socket." Can anyone tell me what could be >causing it? It does usually work, although I often get another error message saying "Error reading from network. Cause: connection aborted due to timeout or other failure. (10053)" I get that message a lot, and I gather that it can be caused by my antivirus program (Computer Associates eTrust EZ Antivirus), although sometimes I still have the problem when I disable the virus checking. I guess I am trying to figure out if this frequent problem is caused by something on my computer or something on the server. On Tue, 08 Mar 2005 21:00:49 -0500, Paul Blais <PBlais@ODStrategies.Org> wrote: >Your email server was too busy to respond within the alloted time out. >It happens sometimes when the server is too busy. If you try again it >probably works? > >On Tue, 08 Mar 2005 20:27:15 -0500, Cliff Lewis <clewis@gwis.com> >wrote: > >&...

socket
Hi, Is it possible to open a TCP socket as read_only from a process, so that I can open another socket in another process with the same ip\port as write_only? Thanks, Hakim On Thu, 16 Jun 2005 09:21:50 -0700, ddrv2002 wrote: > Hi, > > Is it possible to open a TCP socket as read_only from a process, so > that I can open another socket in another process with the same ip\port > as write_only? No. You'll probably want to read unix network programming btw. M4 -- Ah, the beauty of OSS. Hundreds of volunteers worldwide volunteering their time inventing and implementing n...

socket
Hi. I need to send an HTTP post specifying password and username to a web server through a socket in order to login. I'm writing this program in c under a linux system. How does the request has to be formatted? I send the request using the following code: write( socket_fd, buffer, strlen( buffer )); where socket_fd is the socket descriptor, buffer is the string containing the post request and strlen(buffer) its length. thank you Teo81 schrieb: > Hi. I need to send an HTTP post specifying password and username to a > web server through a socket in order to login. I'm writin...

socket
Someone knows how to send a custom object through a socket between two machines? Thank to everyone who'll answer to me. Linus Linus wrote: > > Someone knows how to send a custom object through a socket between two machines? Serialization. -- Lee Fesperman, FFE Software, Inc. (http://www.firstsql.com) ============================================================== * The Ultimate DBMS is here! * FirstSQL/J Object/Relational DBMS (http://www.firstsql.com) On 9 Mar 2005 01:29:22 -0800, Linus wrote: > Someone knows how to send a custom object through a socket between > two m...

Socket
We are using VA Smalltalk 8.5 and TobSockets in the normal client/server sense. However, I need to communicate with a vendor software using the same port for client and server TCP packets. Don't ask me to change the design it is not my decision. Can you Smalltalkers provide some advice if there is a good way to do this in TobSockets? I don't really want to play around in SciScoket. Thanks! Bernard Hi Bernard, I use Totally Objects SocketSet a lot, so maybe I can help. Can you tell more about what you are trying to do. Is your code connecting to the vendor software or i...

socket.unbind or socket.unlisten?
I have a program that uses socket.bind() and socket.listen() frequently. After that program stops, it is not able to bind() again for a while: File "/home/gandalf/Python/Lib/orb/accesspoints/srvtcp.py", line 27, in __init__ self.serversocket.bind((self.listen_address,self.port)) File "<string>", line 1, in bind socket.error: (48, 'Address already in use') The problem with this, is that this server program SOMETIMES need to be restarted very quickly. I tried to find the solution in the socket module. But there is no "socket.unbind" or &q...

Uncatchable socket.error in socket.py (?)
Hi, I've written a very simple 'kill-server' to help me shut down processes through Telnet or HTTP. The kill-server is a function and is launched as a thread. I use the module socket.py on Python v2.3 (Windows) I can use telnet host:port and enter the secret killword or use a broser with http://host:port/secret_killword The 'kill-server' validates the secret_killword and writes a file to /tmp. The main process regularly checks for the existence of that file, and ends the loop if it does exist. It works well. But most for the fun (because this will probably never be ex...

Reconnect socket without close/socket?
I am writing a program that needs to read some data from a server via a TCP socket every few seconds. (The server is the hddtemp program, running in daemon mode.) Documentation is somewhat sparse, but AFAICT the server simply accepts connections, writes its output to the socket, and immediately closes it. So every time I want to poll the server, I need to reconnect. Is there any way to accomplish this without doing a close()/socket() cycle every time? I've tried using shutdown(fd, SHUT_RDWR), but I get a "Transport endpoint is already connected" error. Thanks! ...

problems with sockets while implemting posix sockets
In the function msg_receive, fd is a socket connected to a remote Internet client. What is wrong with msg_receive? Suggest a fix. #define MAXVALS 65536 typedef struct _msg_t { unsigned int num; unsigned int values[MAXVALS]; } msg_t; int msg_receive (int fd, msg_t *m) { size_t sz = sizeof (unsigned int); assert (m); if (read (fd, (void *)&m->num, sz) < sz) return -1; return read (fd, (void *)&m->values, sz * m->num); } On 21 Feb 2005 12:50:09 -0800, puzzlecracker <ironsel2000@gmail.com> wrote...

Socket Error 10038 on Multicast Socket
Hi, I get WSAENOTSOCK (10038) error on a Multicast socket when a packet is multicast to a multicast address that the socket has joined. The strange thing is, I get the error only on one particular system, installed with Windows XP Pro (SP2). I have other XP Pro (SP2) systems, but they dont give this error. Anyone else with the same problem? I tried changing the network card to use a PCI card that I know works fine in other systems, but still the same problem. Oh the said system has Microsoft Virtual PC installed as well. Could be causing the problem? TIA Hari *** Posted via a free Usenet account from http://www.teranews.com *** ...

Socket 478 to Socket 775 adapter?
Hi all, The P4 on my desktop is a Skt.478. With Intel now not producing the 478 anymore, if I wanted to upgrade the chip to a higher speed such that there was not any 478's produced at that speed, I am looking at buying a new MoBo which is a definate no at the moment with my system just having a new board in it (ASUS P4S800). Does anyone know if there is a Socket 478 to Socket 775 adapter available? I have checked powerleap.com, and they only have a Socket 423 to Socket 478 and no higher. Thanks, Neil -- Desktop = P4 Northwood @ 2.6GHz, 512MB PC2100, 120GB HD, 256MB XFX GeFo...

[socket] test if socket descriptor is valid
Hi Is it possible to check that a socket descriptor is valid before receiveing data ( recv) ? maybe fstat ? "mbm" <mbmessaoud@yahoo.com> wrote in message news:1190890077.826077.15620@57g2000hsv.googlegroups.com... > Hi > > Is it possible to check that a socket descriptor is valid before > receiveing data ( recv) ? > > maybe fstat ? Why bother? recv will return EBADF if the descriptor is invalid anyway. However you are correct - using fstat you can check the st_mode field for the file type. Note this is only valid if fstat returns correctly... you may ...

Socket programming: Command and Data socket
Hi all, I am learning about socket programming from the books from Richard Stevens. I just started, but browsing thru the books I didn't find an example of how one could implement a program that uses two sockets for communication (like for example the FTP command, which uses a data and a command socket). I am trying to write such a program myself, but I can't really get around the design of the program. My questions is relatively simple (I think). How would I go about setting up communication with two sockets? I just need ideas on how to do this. I've been trying to get this work...

Web resources about - Socket broken due to socket resource leak??? - comp.lang.ruby

Resource Management Act 1991 - Wikipedia, the free encyclopedia
The Resource Management Act ( RMA ) passed in 1991 in New Zealand is a significant, and at times, controversial Act of Parliament . The RMA promotes ...


Qld mines in battle for survival: Queensland Resources Council
... government must help prop up the state's ailing coalmines or risk losing thousands of jobs, a lobby group says. The Queensland Resources Council ...

Principals say Victorian state schools lacking resources to ensure students get quality education - HeraldSun ...
MOST of Victoria’s state school principals say they don’t have enough resources to ensure their students have a quality education.

Principals: ‘School resources falling short’
MOST of Victoria’s state school principals say they don’t have enough resources to ensure their students have a quality education.

Miners marching home to the UK and Ireland from WA resources downturn
Many of the British workers drawn to a lucrative industry in Australia have been forced by job cuts to return, writes Jon Yeomans

Refugee students adapt to Regina schools amidst resource shortage
Regina Public Schools says it does not have the resources to meet the needs of the 49 new refugee students in the school division.

Iluka Resources on counter-cyclical investment prowl
Mineral sands major Iluka Resources is adamant that now is the best time for it to pursue growth.

An excellent resource
Anatomy Tutorial. This is the same as a medical school gross anatomy course, minus the cadaver dissection. You can see one of their Youtube ...

Tencent Gains Exclusive Sports Content Resources From ESPN
Tencent Gains Exclusive Sports Content Resources From ESPN ChinaTechNews.com Chinese Internet giant Tencent announced that the company has ...

Resources last updated: 3/2/2016 7:06:54 PM