# A bug? Socket broken due to resource leak in failed connect?

**URL:** <https://www.ruby-forum.com/t/a-bug-socket-broken-due-to-resource-leak-in-failed-connect/60617>\
**Category:** Ruby\
**Created:** [May 16, 2006, 6:06am UTC](https://www.ruby-forum.com/t/a-bug-socket-broken-due-to-resource-leak-in-failed-connect/60617 "2006-05-16T06:06:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![uncutstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/7993a0/32.png) [@uncutstone](https://www.ruby-forum.com/u/uncutstone)\
**Post date:** [May 16, 2006, 6:06am UTC](https://www.ruby-forum.com/t/a-bug-socket-broken-due-to-resource-leak-in-failed-connect/60617/1 "2006-05-16T06:06:09Z")

</div>

It is really a triky problem.

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 connects and sends a request. Tcpclient.rb first makes a  
connect to the local tcp server, 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 wierd thing is , after certain number of tries, the established  
connection between the tcp client and the local tcp server is broken.  
So the line “from tcpclient.rb:19:in goodConn.puts” also raises a  
exception . Even more,  
what exception “goodConn.puts” 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 `write': Bad file descriptor (Errno::EBADF) from tcpclient.rb:19:in`puts’  
from tcpclient.rb:19:in `testsocket’  
from tcpclient.rb:25

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

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 substantial times  
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, the run the tcpclient in another console. Be sure to  
be offline when you try the code. Being offline will make it run fast,  
otherwise it will take long time to run

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  
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

goodConn.puts(“hello”)  
s = goodConn.gets  
puts s  
goodConn.close  
end

testsocket

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

---

<div class="post-metadata">

**Author:** ![uncutstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/7993a0/32.png) [@uncutstone](https://www.ruby-forum.com/u/uncutstone)\
**Post date:** [May 16, 2006, 5:03pm UTC](https://www.ruby-forum.com/t/a-bug-socket-broken-due-to-resource-leak-in-failed-connect/60617/2 "2006-05-16T17:03:05Z")

</div>

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 misunderstood something. Any opinion is  
highly appreciated.

---

<div class="post-metadata">

**Author:** ![uncutstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/7993a0/32.png) [@uncutstone](https://www.ruby-forum.com/u/uncutstone)\
**Post date:** [May 17, 2006, 1:39am UTC](https://www.ruby-forum.com/t/a-bug-socket-broken-due-to-resource-leak-in-failed-connect/60617/3 "2006-05-17T01:39:39Z")

</div>

Hi,

In message “Re: A bug? Socket broken due to resource leak in failed  
conn”  
on Wed, 17 May 2006 00:03:29 +0900, uncutstone wu  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

|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.

I’m afraid that it’s a bug in Winsock library on Win98, since it  
should never raise Unknown Error on any case.

```
						matz.

```

---

<div class="post-metadata">

**Author:** ![uncutstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/7993a0/32.png) [@uncutstone](https://www.ruby-forum.com/u/uncutstone)\
**Post date:** [May 17, 2006, 5:53am UTC](https://www.ruby-forum.com/t/a-bug-socket-broken-due-to-resource-leak-in-failed-connect/60617/4 "2006-05-17T05:53:11Z")

</div>

Yukihiro M. wrote:

> Hi,
> 
> In message “Re: A bug? Socket broken due to resource leak in failed  
> conn”  
> on Wed, 17 May 2006 00:03:29 +0900, uncutstone wu  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:
> 
> |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.
> 
> I’m afraid that it’s a bug in Winsock library on Win98, since it  
> should never raise Unknown Error on any case.
> 
> ```
> matz.
> 
> ```

It’s my pleasure to get reply from you, the matz. 🙂

Yes, I agree, it is very likely a win98 socket bug. I did think so  
before I post this message.

I am writing a program which brute force scans a large group of IP  
addresses to find ftp servers. Most of the ip addresses cannot be  
connected. After cetain number of connection tries, the socket gets  
broken.

How can I work around to this since it maybe a bug in the platform?

FYI, this program is part of a ftp search engine which I am developing  
using ruby.

---

<div class="post-metadata">

**Author:** ![uncutstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/7993a0/32.png) [@uncutstone](https://www.ruby-forum.com/u/uncutstone)\
**Post date:** [May 17, 2006, 7:57am UTC](https://www.ruby-forum.com/t/a-bug-socket-broken-due-to-resource-leak-in-failed-connect/60617/5 "2006-05-17T07:57:48Z")

</div>

uncutstone wu wrote:

> I am writing a program which brute force scans a large group of IP  
> addresses to find ftp servers. Most of the ip addresses cannot be  
> connected. After cetain number of connection tries, the socket gets  
> broken.
> 
> How can I work around to this since it maybe a bug in the platform?

Maybe you could talk to the TCP socket at a lower protocol level than  
connect, using raw IP packets. The source of nmap might be helpful.  
(I’ve no idea whether win98 supports raw sockets, though…)
