Thread: Socket (fd) to posters server getting closed. (libpq)

Socket (fd) to posters server getting closed. (libpq)

From
Sudheer H R
Date:
Hi,

I am writing a piece of software to connect to the Postgres database in the asynchronous manner and have used PQconnectStartParams, PQsendPrepare, PQFlush , etc… in conjunction with libev.


The established connection is used for a transaction, and then added to a pool of connections, and then subsequently retrieved from the pool for reuse.

I notice that the retrieved connection has the socket fd closed, which leads to an error o the nature:  could not receive data from server: Bad file descriptor


While the client library (libpq) does not detect the closed socket (unix domain socket) and tries to communicate with the server and throws an error.



Saw this communication trail on the subject https://www.postgresql.org/message-id/12708.1411394055@sss.pgh.pa.us where someone else has faced the same error while coding in python (I am using Lua for this purpose)


I guess it becomes necessary to check for readiness of read and write of the fd returned by PQsocket before continuing to use it again.

My concern is, this is happening unexpectedly and quite soon (after the connection is added to the pool in 1 or 2 seconds)

I would like to know the cause of closure of the connection, if there is any and like to arrest it rather than having to reopen the connection again.

Any feedback regarding this would be appreciated.

Regards,
Sudheer

Re: Socket (fd) to posters server getting closed. (libpq)

From
Tom Lane
Date:
Sudheer H R <sudheer.hr@tekenlight.com> writes:
> I am writing a piece of software to connect to the Postgres database in the asynchronous manner and have used
PQconnectStartParams,PQsendPrepare, PQFlush , etc… in conjunction with libev. 

> The established connection is used for a transaction, and then added to a pool of connections, and then subsequently
retrievedfrom the pool for reuse. 

> I notice that the retrieved connection has the socket fd closed, which leads to an error o the nature:  could not
receivedata from server: Bad file descriptor 

libpq itself will assuredly never close a socket that's associated with
an open connection.  You need to look elsewhere for the cause.

            regards, tom lane