Infinite loop on error

Marko Kreen <[email protected]> Wed, 28 Jul 2010 18:54:59 +0300
Newsgroups gmane.comp.python.db.psycopg.devel
Message-ID <[email protected]>
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