Rilson Nascimento | 10 Aug 2007 05:24
Picon

connection in TIME_WAIT with pqxx

Hi there,

I am using pqxx in a multi-threaded server that connects to a PostgreSQL database. I'm experiencing a problem in which my server leaves a bunch of TIME_WAIT socket connection with the PostgreSQL server when it is running.

When I run 'netstat -tcp' I see literally hundreds of connections from my server to PgSQL in TIME_WAIT state (see below), even after a short period of server activity:
tcp     0     0  localhost:38727       localhost:5432         TIME_WAIT
...

I'm using pqxx's lazyconnection. Actually there is only ONE client running in a loop sending transactions to the server (via tcp socket), which in turn connects to the PgSQL database via a lazyconnection (I tried with the usual connection object too).

I guess this is an effect of poor networking programming (socket programming) and/or poor pqxx programming.
Whatever, What should I do to realize this is not a problem related with misusing of pqxx? I mean, I want to be sure I am using pqxx in the right fashion to ensure this problem is not caused by pqxx.

Thanks for your help,

-Ron


_______________________________________________
Libpqxx-general mailing list
Libpqxx-general@...
http://gborg.postgresql.org/mailman/listinfo/libpqxx-general
Jeroen T. Vermeulen | 10 Aug 2007 07:31
Picon
Picon
Favicon

Re: connection in TIME_WAIT with pqxx

On Fri, August 10, 2007 10:24, Rilson Nascimento wrote:

> I am using pqxx in a multi-threaded server that connects to a PostgreSQL
> database. I'm experiencing a problem in which my server leaves a bunch of
> TIME_WAIT socket connection with the PostgreSQL server when it is running.

If I remember correctly, those are closed sockets that the OS keeps around
for a while just in case more packets arrive for them.  So they should go
away eventually, completely independently of the coming and going of your
program.  That part is normal.

The suspicious part is that so many sockets were opened in the first
place.  You're not knowingly creating and closing lots of short-lived
connections?  Or even just a lot of long-lived connections?

The sockets could be by-products of retries (attempt to connect, fail,
close socket, try a new one) but on a localhost connection you generally
either fail or succeed consistently.  If a localhost connection fails, the
next attempt won't work either and the program just won't be able to
access the database.

> I'm using pqxx's lazyconnection. Actually there is only ONE client running
> in a loop sending transactions to the server (via tcp socket), which in
> turn connects to the PgSQL database via a lazyconnection (I tried with the
> usual connection object too).

The plain "connection" class provides the more useful information: if you
see the same behaviour there, that tells us this is not an obscure problem
in the lazy-connection logic, and that comes as a relief.

> I guess this is an effect of poor networking programming (socket
> programming) and/or poor pqxx programming.
> Whatever, What should I do to realize this is not a problem related with
> misusing of pqxx? I mean, I want to be sure I am using pqxx in the right
> fashion to ensure this problem is not caused by pqxx.

The safest thing as far as threading is concerned is to make sure that no
two threads access the same connection, or other objects belonging to the
same connection, simultaneously.  That's more strict than is really
necessary, but it's also relatively easy to maintain.

Jeroen
Rilson Nascimento | 10 Aug 2007 08:33
Picon

Re: connection in TIME_WAIT with pqxx

Hi Jeroen,

Many thanks for your fast and valuable reply. As I guessed in the beginning, it was poor network programming on my end. Fortunately, I found the problem just now after a long week of debugging. It ended up being what you said in the email: creating and closing lots of short-lived socket connections.

There is nothing wrong with pqxx, it is working smoothly. I can say now that I understand sockets way better ;-)

Thanks again, congratulations for the pqxx project, and keep up the good work!

kind regards,

-Rilson

On 8/10/07, Jeroen T. Vermeulen <jtv-qWit8jRvyhVmR6Xm/wNWPw@public.gmane.org> wrote:
On Fri, August 10, 2007 10:24, Rilson Nascimento wrote:

> I am using pqxx in a multi-threaded server that connects to a PostgreSQL
> database. I'm experiencing a problem in which my server leaves a bunch of
> TIME_WAIT socket connection with the PostgreSQL server when it is running.

If I remember correctly, those are closed sockets that the OS keeps around
for a while just in case more packets arrive for them.  So they should go
away eventually, completely independently of the coming and going of your
program.  That part is normal.

The suspicious part is that so many sockets were opened in the first
place.  You're not knowingly creating and closing lots of short-lived
connections?  Or even just a lot of long-lived connections?

The sockets could be by-products of retries (attempt to connect, fail,
close socket, try a new one) but on a localhost connection you generally
either fail or succeed consistently.  If a localhost connection fails, the
next attempt won't work either and the program just won't be able to
access the database.


> I'm using pqxx's lazyconnection. Actually there is only ONE client running
> in a loop sending transactions to the server (via tcp socket), which in
> turn connects to the PgSQL database via a lazyconnection (I tried with the
> usual connection object too).

The plain "connection" class provides the more useful information: if you
see the same behaviour there, that tells us this is not an obscure problem
in the lazy-connection logic, and that comes as a relief.


> I guess this is an effect of poor networking programming (socket
> programming) and/or poor pqxx programming.
> Whatever, What should I do to realize this is not a problem related with
> misusing of pqxx? I mean, I want to be sure I am using pqxx in the right
> fashion to ensure this problem is not caused by pqxx.

The safest thing as far as threading is concerned is to make sure that no
two threads access the same connection, or other objects belonging to the
same connection, simultaneously.  That's more strict than is really
necessary, but it's also relatively easy to maintain.


Jeroen



_______________________________________________
Libpqxx-general mailing list
Libpqxx-general@...
http://gborg.postgresql.org/mailman/listinfo/libpqxx-general
Rilson Nascimento | 10 Aug 2007 12:17
Picon

Re: connection in TIME_WAIT with pqxx

Hi Jeroen, list

My problem with sockets is definitely closed. Now my transactions are behaving strangely. I managed to open just one db connection per client, but now I cannot run multiple transactions per connection (it seems). Errors indicating that there is already a transaction open start to arise. Otherwise, if I change the code to create one db connection per transaction I start to see those TIME_WAIT (a lot of them!) connections with the database again. That's a perfect example of a dilemma.

Any thoughts or comment on this?

Thank you,

-Rilson



On 8/10/07, Rilson Nascimento < rilson.nascimento-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
Hi Jeroen,

Many thanks for your fast and valuable reply. As I guessed in the beginning, it was poor network programming on my end. Fortunately, I found the problem just now after a long week of debugging. It ended up being what you said in the email: creating and closing lots of short-lived socket connections.

There is nothing wrong with pqxx, it is working smoothly. I can say now that I understand sockets way better ;-)

Thanks again, congratulations for the pqxx project, and keep up the good work!

kind regards,

-Rilson


On 8/10/07, Jeroen T. Vermeulen < jtv-qWit8jRvyhVmR6Xm/wNWPw@public.gmane.org> wrote:
On Fri, August 10, 2007 10:24, Rilson Nascimento wrote:

> I am using pqxx in a multi-threaded server that connects to a PostgreSQL
> database. I'm experiencing a problem in which my server leaves a bunch of
> TIME_WAIT socket connection with the PostgreSQL server when it is running.

If I remember correctly, those are closed sockets that the OS keeps around
for a while just in case more packets arrive for them.  So they should go
away eventually, completely independently of the coming and going of your
program.  That part is normal.

The suspicious part is that so many sockets were opened in the first
place.  You're not knowingly creating and closing lots of short-lived
connections?  Or even just a lot of long-lived connections?

The sockets could be by-products of retries (attempt to connect, fail,
close socket, try a new one) but on a localhost connection you generally
either fail or succeed consistently.  If a localhost connection fails, the
next attempt won't work either and the program just won't be able to
access the database.


> I'm using pqxx's lazyconnection. Actually there is only ONE client running
> in a loop sending transactions to the server (via tcp socket), which in
> turn connects to the PgSQL database via a lazyconnection (I tried with the
> usual connection object too).

The plain "connection" class provides the more useful information: if you
see the same behaviour there, that tells us this is not an obscure problem
in the lazy-connection logic, and that comes as a relief.


> I guess this is an effect of poor networking programming (socket
> programming) and/or poor pqxx programming.
> Whatever, What should I do to realize this is not a problem related with
> misusing of pqxx? I mean, I want to be sure I am using pqxx in the right
> fashion to ensure this problem is not caused by pqxx.

The safest thing as far as threading is concerned is to make sure that no
two threads access the same connection, or other objects belonging to the
same connection, simultaneously.  That's more strict than is really
necessary, but it's also relatively easy to maintain.


Jeroen




_______________________________________________
Libpqxx-general mailing list
Libpqxx-general@...
http://gborg.postgresql.org/mailman/listinfo/libpqxx-general
Jeroen T. Vermeulen | 10 Aug 2007 12:22
Picon
Picon
Favicon

Re: connection in TIME_WAIT with pqxx

On Fri, August 10, 2007 17:17, Rilson Nascimento wrote:

> My problem with sockets is definitely closed. Now my transactions are
> behaving strangely. I managed to open just one db connection per client,
> but
> now I cannot run multiple transactions per connection (it seems). Errors
> indicating that there is already a transaction open start to arise.
> Otherwise, if I change the code to create one db connection per
> transaction
> I start to see those TIME_WAIT (a lot of them!) connections with the
> database again. That's a perfect example of a dilemma.
>
> Any thoughts or comment on this?

As always, a connection can have only one transaction open at a time.  So
always make sure you destroy a transaction before you open a new one on
the same connection!

(And if you want your work to stay in the database, of course, you'll want
to commit the transaction first :-)

Jeroen

Gmane