pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Bug (and fix): leaks of TCP connections when connected to a <7.4 server
10+ messages / 6 participants
[nested] [flat]

* Bug (and fix): leaks of TCP connections when connected to a <7.4 server
@ 2004-06-21 13:00  Laurent Sylvain <sylvain.laurent@elca.ch>
  0 siblings, 3 replies; 10+ messages in thread

From: Laurent Sylvain @ 2004-06-21 13:00 UTC (permalink / raw)
  To: pgsql-jdbc

Hello,

I experienced some TCP connection leaks when using PGSQL JDBC driver 7.4
(build 214) to connect to a 7.3.4 server.
The symptoms are that when performing a netstat on the client machine, many
connections were in the CLOSE_WAIT state.

The problem is that the driver tries to connect using v3 protocol and when
it sees that the server doesn't understand it, it opens a new connection
(PGStream) to the server without closing the previous one:

AbstractJdbc1Connection.java line 304 and on:
	if (l_elen > 30000) {
		//if the error length is > than 30000 we assume this is
really a v2 protocol 
		//server so try again with a v2 connection
		//need to create a new connection and try again
		try
		{
			pgStream = new PGStream(p_host, p_port);
		}
		catch (ConnectException cex)

A quick fix is to do a pgStream.close(); before instantiating a new
PGStream, but I don't know very well the sources and it might have other
side effects.

To circumvent the problem while waiting for an eventual new version of the
driver, I added "?compatible=7.3" to the connection URL...

I hope it'll improve the driver ;-)

Sylvain Laurent



^ permalink  raw  reply  [nested|flat] 10+ messages in thread

* Re: Bug (and fix): leaks of TCP connections when connected
@ 2004-06-21 19:34  Kris Jurka <books@ejurka.com>
  parent: Laurent Sylvain <sylvain.laurent@elca.ch>
  2 siblings, 1 reply; 10+ messages in thread

From: Kris Jurka @ 2004-06-21 19:34 UTC (permalink / raw)
  To: Laurent Sylvain <sylvain.laurent@elca.ch>; +Cc: pgsql-jdbc



On Mon, 21 Jun 2004, Laurent Sylvain wrote:

> I experienced some TCP connection leaks when using PGSQL JDBC driver 7.4
> (build 214) to connect to a 7.3.4 server.
> The symptoms are that when performing a netstat on the client machine, many
> connections were in the CLOSE_WAIT state.

I'm not much of an expert on TCP, so could you give me a little more
background on this?  I've duplicated the behavior you describe and I
believe your fix is correct, but I'd like to understand this better.  
First why is this a problem, the CLOSE_WAIT entries disappear rather
quickly.  Second the number of CLOSE_WAIT items is nothing compared to the 
number of TIME_WAIT entries, why aren't they a problem?

Kris Jurka



^ permalink  raw  reply  [nested|flat] 10+ messages in thread

* Re: Bug (and fix): leaks of TCP connections when connected
@ 2004-06-22 01:42  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Kris Jurka <books@ejurka.com>
  0 siblings, 0 replies; 10+ messages in thread

From: Tom Lane @ 2004-06-22 01:42 UTC (permalink / raw)
  To: Kris Jurka <books@ejurka.com>; +Cc: Laurent Sylvain <sylvain.laurent@elca.ch>; pgsql-jdbc

Kris Jurka <books@ejurka.com> writes:
> I'm not much of an expert on TCP, so could you give me a little more
> background on this?  I've duplicated the behavior you describe and I
> believe your fix is correct, but I'd like to understand this better.  
> First why is this a problem, the CLOSE_WAIT entries disappear rather
> quickly.  Second the number of CLOSE_WAIT items is nothing compared to the 
> number of TIME_WAIT entries, why aren't they a problem?

I think CLOSE_WAIT means that the kernel knows the far end has closed
the connection, but it's not yet been able to tell the local client so
(the vice-versa case may be called this too, not sure).  This state can
persist indefinitely if the client is uncooperative.  TIME_WAIT is a
short-lived state; the connection will be forgotten completely after the
timeout, which I think is order-of-a-minute.  (TIME_WAIT exists only so
that the kernel remembers what to do with any delayed packets that may
arrive from the far end.)

Bottom line is that CLOSE_WAIT means that a client bug is keeping the
kernel from freeing resources.  You don't want to see that.

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 10+ messages in thread

* Re: Bug (and fix): leaks of TCP connections when connected
@ 2004-06-22 02:03  Oliver Jowett <oliver@opencloud.com>
  parent: Laurent Sylvain <sylvain.laurent@elca.ch>
  2 siblings, 1 reply; 10+ messages in thread

From: Oliver Jowett @ 2004-06-22 02:03 UTC (permalink / raw)
  To: Laurent Sylvain <sylvain.laurent@elca.ch>; +Cc: pgsql-jdbc

Laurent Sylvain wrote:
> Hello,
> 
> I experienced some TCP connection leaks when using PGSQL JDBC driver 7.4
> (build 214) to connect to a 7.3.4 server.
> The symptoms are that when performing a netstat on the client machine, many
> connections were in the CLOSE_WAIT state.
> 
> The problem is that the driver tries to connect using v3 protocol and when
> it sees that the server doesn't understand it, it opens a new connection
> (PGStream) to the server without closing the previous one:

In theory the discarded connections should eventually be garbage 
collected and closed, right? So at least the leak is bounded.

(I'll check that this is fixed in my patches; I restructured that area 
quite a bit)

-O



^ permalink  raw  reply  [nested|flat] 10+ messages in thread

* Re: Bug (and fix): leaks of TCP connections when connected
@ 2004-06-22 02:57  Marcus Andree S. Magalhaes <marcus.magalhaes@vlinfo.com.br>
  parent: Oliver Jowett <oliver@opencloud.com>
  0 siblings, 1 reply; 10+ messages in thread

From: Marcus Andree S. Magalhaes @ 2004-06-22 02:57 UTC (permalink / raw)
  To: oliver@opencloud.com; +Cc: sylvain.laurent@elca.ch; pgsql-jdbc

<snip>

> In theory the discarded connections should eventually be garbage
> collected and closed, right? So at least the leak is bounded.
>

I don't think so. We had a similar problem here.

After many hours of debugging, we came to the fact that either
java garbage collector does _not_ close open connections or do it
after a long time (much longer than one would expect).
This feature/bug caused a severe connection leak in our code and
we had to issue a explicit close() to our socket connections.






^ permalink  raw  reply  [nested|flat] 10+ messages in thread

* Re: Bug (and fix): leaks of TCP connections when connected
@ 2004-06-22 03:42  Oliver Jowett <oliver@opencloud.com>
  parent: Marcus Andree S. Magalhaes <marcus.magalhaes@vlinfo.com.br>
  0 siblings, 0 replies; 10+ messages in thread

From: Oliver Jowett @ 2004-06-22 03:42 UTC (permalink / raw)
  To: Marcus Andree S. Magalhaes <marcus.magalhaes@vlinfo.com.br>; +Cc: sylvain.laurent@elca.ch; pgsql-jdbc

Marcus Andree S. Magalhaes wrote:
> <snip>
> 
>>In theory the discarded connections should eventually be garbage
>>collected and closed, right? So at least the leak is bounded.
> 
> I don't think so. We had a similar problem here.
> 
> After many hours of debugging, we came to the fact that either
> java garbage collector does _not_ close open connections

It's not the GC that directly closes the open connections; it just 
causes unreachable objects to be collected. It's the responsibility of 
object that holds out-of-heap resources to do whatever cleanup (OS-level 
close() calls etc) is needed when it becomes unreachable, traditionally 
via finalize() (although you can also use ReferenceQueue). I'd expect 
the socket implementation to do this.

> or do it
> after a long time (much longer than one would expect).

Typically finalization and Reference clearing happens only on full 
collections (it's not guaranteed to happen this way, though), so I'd 
expect the connections to be collected then. It can take some time for a 
full GC to happen, depending on your heap settings. We see problems in 
NIO buffer allocation when System.gc() is disabled (see below) on a 
quiescent system that can have intervals of over a day between full GCs.

If you leak sockets or some other resource rapidly enough that you run 
out of the resources at the OS level (or indirectly hit a resource 
limitation on the remote side e.g. server connection limit) before the 
owning objects are collected, you will have a problem.

NIO direct buffer allocation hits exactly this issue, and ends up 
issuing a System.gc() when it thinks it's out of direct buffer space to 
try to avoid the problem. There are some bugs in Sun's implementation 
though which make it hard to use (it forces a stop-the-world GC which is 
bad if you are using the CMS collector, and there is a race between the 
allocating thread returning from System.gc() and buffer references being 
cleared in the reference-handler thread). Assuming those problems can be 
fixed, there's an argument for doing the same thing in the socket 
allocation code (and anywhere else that allocates out-of-heap resources) 
too. It becomes a question of how far to take it: if the server refuses 
a connection with "too many connections", should you GC and try again if 
that collected any connections to the server in question?

See http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5025281

(apologies for rambling :)

-O



^ permalink  raw  reply  [nested|flat] 10+ messages in thread

* Re: Bug (and fix): leaks of TCP connections when connected
@ 2004-06-22 07:15  Laurent Sylvain <sylvain.laurent@elca.ch>
  0 siblings, 1 reply; 10+ messages in thread

From: Laurent Sylvain @ 2004-06-22 07:15 UTC (permalink / raw)
  To: Oliver Jowett <oliver@opencloud.com>; +Cc: pgsql-jdbc

There's no finalizer on the Socket class that closes it so that even if it
is garbage collected, the socket is not closed at the OS level. I think I
saw somewhere this was done by design and actually I believe it's much
cleaner to properly close the socket before losing any reference to it so
that OS resources are freed as early as possible. Much better for
scalability ;-)
My java program ran for days so that I'm pretty sure GC occurred. Anyway
when I performed a netstat I had hundreds of connections waiting to be
closed...

Sylvain

-----Original Message-----
From: Oliver Jowett [mailto:oliver@opencloud.com]
Sent: mardi, 22. juin 2004 04:04
To: Laurent Sylvain
Cc: 'pgsql-jdbc@postgresql.org'
Subject: Re: [JDBC] Bug (and fix): leaks of TCP connections when
connected to a <7.4 server


Laurent Sylvain wrote:
> Hello,
> 
> I experienced some TCP connection leaks when using PGSQL JDBC driver 7.4
> (build 214) to connect to a 7.3.4 server.
> The symptoms are that when performing a netstat on the client machine,
many
> connections were in the CLOSE_WAIT state.
> 
> The problem is that the driver tries to connect using v3 protocol and when
> it sees that the server doesn't understand it, it opens a new connection
> (PGStream) to the server without closing the previous one:

In theory the discarded connections should eventually be garbage 
collected and closed, right? So at least the leak is bounded.

(I'll check that this is fixed in my patches; I restructured that area 
quite a bit)

-O



^ permalink  raw  reply  [nested|flat] 10+ messages in thread

* Re: Bug (and fix): leaks of TCP connections when connected
@ 2004-06-22 09:37  Kris Jurka <books@ejurka.com>
  parent: Laurent Sylvain <sylvain.laurent@elca.ch>
  2 siblings, 0 replies; 10+ messages in thread

From: Kris Jurka @ 2004-06-22 09:37 UTC (permalink / raw)
  To: Laurent Sylvain <sylvain.laurent@elca.ch>; +Cc: pgsql-jdbc



On Mon, 21 Jun 2004, Laurent Sylvain wrote:

> I experienced some TCP connection leaks when using PGSQL JDBC driver 7.4
> (build 214) to connect to a 7.3.4 server.
> The symptoms are that when performing a netstat on the client machine, many
> connections were in the CLOSE_WAIT state.
> 
> The problem is that the driver tries to connect using v3 protocol and when
> it sees that the server doesn't understand it, it opens a new connection
> (PGStream) to the server without closing the previous one:
> 

I've applied the attached patch to the 7.4 and 7.5 cvs trees.  It adds the 
pgStream.close() you suggested as well as closing the stream for other 
connection failures such as authentication failures or lack of ssl 
support.

Kris Jurka

Attachments:

tcpclose.patch (text/plain, 1.9K) download ? built ? jdbcbuild.sh Index: org/postgresql/jdbc1/AbstractJdbc1Connection.java =================================================================== RCS file: /cvsroot/pgsql-server/src/interfaces/jdbc/org/postgresql/jdbc1/Attic/AbstractJdbc1Connection.java,v retrieving revision 1.27.2.2 diff -c -r1.27.2.2 AbstractJdbc1Connection.java *** org/postgresql/jdbc1/AbstractJdbc1Connection.java 10 Feb 2004 01:58:48 -0000 1.27.2.2 --- org/postgresql/jdbc1/AbstractJdbc1Connection.java 22 Jun 2004 09:23:37 -0000 *************** *** 208,218 **** throw new PSQLException ("postgresql.con.failed", PSQLState.CONNECTION_UNABLE_TO_CONNECT, e); } ! //Now do the protocol work ! if (haveMinimumCompatibleVersion("7.4")) { ! openConnectionV3(host,port,info,database,url,d,password); ! } else { ! openConnectionV2(host,port,info,database,url,d,password); } } --- 208,228 ---- throw new PSQLException ("postgresql.con.failed", PSQLState.CONNECTION_UNABLE_TO_CONNECT, e); } ! try { ! //Now do the protocol work ! if (haveMinimumCompatibleVersion("7.4")) { ! openConnectionV3(host,port,info,database,url,d,password); ! } else { ! openConnectionV2(host,port,info,database,url,d,password); ! } ! } catch (SQLException sqle) { ! // if we fail to completely establish a connection, ! // close down the socket to not leak resources. ! try { ! pgStream.close(); ! } catch (IOException ioe) { } ! ! throw sqle; } } *************** *** 305,310 **** --- 315,321 ---- //if the error length is > than 30000 we assume this is really a v2 protocol //server so try again with a v2 connection //need to create a new connection and try again + pgStream.close(); try { pgStream = new PGStream(p_host, p_port);
^ permalink raw reply [nested|flat] 10+ messages in thread

* Re: Bug (and fix): leaks of TCP connections when connected
@ 2004-06-22 14:52  David Wall <d.wall@computer.org>
  parent: Laurent Sylvain <sylvain.laurent@elca.ch>
  0 siblings, 1 reply; 10+ messages in thread

From: David Wall @ 2004-06-22 14:52 UTC (permalink / raw)
  To: pgsql-jdbc

> There's no finalizer on the Socket class that closes it so that even if it
> is garbage collected, the socket is not closed at the OS level. I think I
> saw somewhere this was done by design and actually I believe it's much
> cleaner to properly close the socket before losing any reference to it so
> that OS resources are freed as early as possible. Much better for
> scalability ;-)

"By design," huh?  We call such thinking: bugs on purpose (also known to the
anti-MSFT crowd as windows programming). <smile>

Yes, it's best to clean up.  But no, a finalizer should discard any open
resources as that what it was designed for.  After all, if the object is
GCed, then nobody is using it, so nobody ever will close it.  At any rate,
sounds like the PG team has already fixed the leak in its code.  Thanks
guys.

David




^ permalink  raw  reply  [nested|flat] 10+ messages in thread

* Re: Bug (and fix): leaks of TCP connections when connected
@ 2004-06-22 16:36  Marcus Andree S. Magalhaes <marcus.magalhaes@vlinfo.com.br>
  parent: David Wall <d.wall@computer.org>
  0 siblings, 0 replies; 10+ messages in thread

From: Marcus Andree S. Magalhaes @ 2004-06-22 16:36 UTC (permalink / raw)
  To: d.wall@computer.org; +Cc: pgsql-jdbc

>> There's no finalizer on the Socket class that closes it so that even
>> if it is garbage collected, the socket is not closed at the OS level.
>> I think I saw somewhere this was done by design and actually I believe
>> it's much cleaner to properly close the socket before losing any
>> reference to it so that OS resources are freed as early as possible.
>> Much better for scalability ;-)
>

Our experience here seems to agree with this fact. Either tcp conns
aren't close by the GC or it takes too long to do just that. In both
cases, we ended with connection leaks.

> "By design," huh?  We call such thinking: bugs on purpose (also known to
> the anti-MSFT crowd as windows programming). <smile>
>

LOL. I tend to define it as "lazy programming". Like those days in
college, when we had to write code that needed to survive only a
few minutes, while being screened and tested by our teachers...

And that's why I prefer to write code in C. There isn't any
Wizard of Oz to do things for you, so, you'd better free your
memory by yourself [and close your connections, too].

> Yes, it's best to clean up.  But no, a finalizer should discard any open
> resources as that what it was designed for.  After all, if the object is
> GCed, then nobody is using it, so nobody ever will close it.  At any
> rate, sounds like the PG team has already fixed the leak in its code.
> Thanks guys.
>

Yes, a finalizer should take care (or better care, we just don't know)
of this issue. But we're living in a real world, where apps have bugs
or design glitches.... Maybe the connection is still referenced by
internal (or not so visible) structures so they're never closed/freed
automagically.







^ permalink  raw  reply  [nested|flat] 10+ messages in thread


end of thread, other threads:[~2004-06-22 16:36 UTC | newest]

Thread overview: 10+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2004-06-21 13:00 Bug (and fix): leaks of TCP connections when connected to a <7.4 server Laurent Sylvain <sylvain.laurent@elca.ch>
2004-06-21 19:34 ` Re: Bug (and fix): leaks of TCP connections when connected Kris Jurka <books@ejurka.com>
2004-06-22 01:42   ` Re: Bug (and fix): leaks of TCP connections when connected Tom Lane <tgl@sss.pgh.pa.us>
2004-06-22 02:03 ` Re: Bug (and fix): leaks of TCP connections when connected Oliver Jowett <oliver@opencloud.com>
2004-06-22 02:57   ` Re: Bug (and fix): leaks of TCP connections when connected Marcus Andree S. Magalhaes <marcus.magalhaes@vlinfo.com.br>
2004-06-22 03:42     ` Re: Bug (and fix): leaks of TCP connections when connected Oliver Jowett <oliver@opencloud.com>
2004-06-22 09:37 ` Re: Bug (and fix): leaks of TCP connections when connected Kris Jurka <books@ejurka.com>
2004-06-22 07:15 Re: Bug (and fix): leaks of TCP connections when connected Laurent Sylvain <sylvain.laurent@elca.ch>
2004-06-22 14:52 ` Re: Bug (and fix): leaks of TCP connections when connected David Wall <d.wall@computer.org>
2004-06-22 16:36   ` Re: Bug (and fix): leaks of TCP connections when connected Marcus Andree S. Magalhaes <marcus.magalhaes@vlinfo.com.br>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox