Marko Kreen | 28 Jul 2010 17:54
Picon

Infinite loop on error

Psycopg has several loops in form:

while ((curs->pgres = PQgetResult()) != NULL)
{
}

or

do {
  pgres = PQgetResult();
} while (pgres != NULL);

The problem is that if libpq gets so fatal error it decides
to close the connection, PQgetResult() will never return NULL
from that connection, thus infinite loop.

I suspect only good fix is to check connection status in all places:

  if (PQstatus(curs->conn) == CONNECTION_BAD) { }

Also as the construct is complex enough, it seems to call for a single
function...

--

-- 
marko
Daniele Varrazzo | 2 Aug 2010 02:56
Picon
Gravatar

Re: Infinite loop on error

On Wed, Jul 28, 2010 at 4:54 PM, Marko Kreen <markokr@...> wrote:
> Psycopg has several loops in form:
>
> while ((curs->pgres = PQgetResult()) != NULL)
> {
> }
>
> or
>
> do {
>  pgres = PQgetResult();
> } while (pgres != NULL);
>
>
> The problem is that if libpq gets so fatal error it decides
> to close the connection, PQgetResult() will never return NULL
> from that connection, thus infinite loop.

Are you sure about that? I don't have the libpq source code handy, I
will surely check that. But, if it doesn't return a NULL with the
connection in broken state... what does it return?

PQgetResult() also returns null when there is no result to fetch. I
may have extrapolated excessively assuming that it returns null when
the connection is closed too, but it doesn't seem an excessively
stretched interpretation.

I'm curious to do some test in the next days.

-- Daniele
(Continue reading)


Gmane