Sometimes PQfinish() does not return forever in the following program.

conn = PQsetdbLogin();
if(PQstatus(conn) == CONNECTION_BAD)
{
  PQfinish(conn);	/* blocks here */
}

PQfinish calls closePGconn that call pqPuts. pqPuts calls select(2)
and it never returns if a connection associated with the socket is not
established.

This could happend if the connection is not established, but the
socket is still opened by PQsetdbLogin. Possible fixes are:

1) close the socket in PQsetdbLogin if it is in a situation being
   returns CONNECTION_BAD

		case PGRES_POLLING_WRITING:
			if (pqWait(0, 1, conn))
			{
				conn->status = CONNECTION_BAD;
				close(conn->sock); <-- add this
				conn->sock == -1;  <-- add this
				return 0;
			}
			break;


2) check if the status of handle returned PQsetdbLogin is
   CONNECTION_BAD closePGconn. if so, do not call pqPuts (and
   pqFlush)

   change this:
	if (conn->sock >= 0)

   to:
	if (conn->status != CONNECTION_BAD && conn->sock >= 0)

any thoughts?
--
Tatsuo Ishii



Fri, 25 May 2001 22:12:39 +0900
Tatsuo Ishii <t-ishii@sra.co.jp>
PQsetdbLogin bug?
Tatsuo Ishii <t-ishii@sra.co.jp> writes:
> 2) check if the status of handle returned PQsetdbLogin is
>    CONNECTION_BAD closePGconn. if so, do not call pqPuts (and
>    pqFlush)

I like this approach.  The other way, you'd have to be sure that all
failure paths close the socket before returning; even if you get it
right today, somebody will break it again in future.  The sending of
the final 'X' is really quite optional anyhow, so I'd say it's fine
not to do it when there's any question about whether it's safe.

			regards, tom lane

Fri, 25 May 2001 10:26:11 -0400
Tom Lane <tgl@sss.pgh.pa.us>
Re: PQsetdbLogin bug? 
> Tatsuo Ishii <t-ishii@sra.co.jp> writes:
> > 2) check if the status of handle returned PQsetdbLogin is
> >    CONNECTION_BAD closePGconn. if so, do not call pqPuts (and
> >    pqFlush)
> 
> I like this approach.  The other way, you'd have to be sure that all
> failure paths close the socket before returning; even if you get it
> right today, somebody will break it again in future.  The sending of
> the final 'X' is really quite optional anyhow, so I'd say it's fine
> not to do it when there's any question about whether it's safe.

done.
--
Tatsuo Ishii

Mon, 28 May 2001 13:36:23 +0900
Tatsuo Ishii <t-ishii@sra.co.jp>
Re: PQsetdbLogin bug? 