Marko Kreen | 2 Aug 2010 08:56
Picon

Re: Infinite loop on error

On 8/2/10, Daniele Varrazzo <daniele.varrazzo@...> wrote:
> 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.

Yeah, found out via strace/gdb...  strace was showing infinite
close(-1) - thus libpq was trying to close already closed connection.

> But, if it doesn't return a NULL with the
>  connection in broken state... what does it return?

Probably PQmakeEmptyPGresult(conn, PGRES_FATAL_ERROR)
(Continue reading)

Daniele Varrazzo | 5 Aug 2010 03:31
Picon
Gravatar

Re: Infinite loop on error

On Mon, Aug 2, 2010 at 7:56 AM, Marko Kreen <markokr@...> wrote:
> On 8/2/10, Daniele Varrazzo <daniele.varrazzo@...> wrote:
>> On Wed, Jul 28, 2010 at 4:54 PM, Marko Kreen <markokr@...> wrote:
>>  > Psycopg has several loops in form:
>> [...]
>>  > 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.
>
> Yeah, found out via strace/gdb...  strace was showing infinite
> close(-1) - thus libpq was trying to close already closed connection.
>
>> But, if it doesn't return a NULL with the
>>  connection in broken state... what does it return?
>
> Probably PQmakeEmptyPGresult(conn, PGRES_FATAL_ERROR)

Yup, seen in the libpq source.

>>  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.
>
> Yeah, seems NULL means "query is finished _and_ everything is fine".

I shall propose a clarification to the pg docs ml.
(Continue reading)

Daniele Varrazzo | 9 Aug 2010 12:00
Picon
Gravatar

Re: Infinite loop on error

On Thu, Aug 5, 2010 at 2:31 AM, Daniele Varrazzo
<daniele.varrazzo@...> wrote:
>
> I did, but I've not been able to reproduce a loop (I've produced
> segfaults, crashes... but not an infinite loop). Do you have a recipe
> to reproduce it? I have a patch adding the proposed check to the loops
> (http://tinyurl.com/3463bql). If you can test it (or propose how to
> test it) it would be great.

An update on this issue: PostgreSQL developers are not convinced that
the issue was caused by the PQgetResult loop and that the pattern we
use in psycopg is sane - at least as sane as many other programs using
libpq and for which no bug has been reported about such loop
<http://archives.postgresql.org/pgsql-docs/2010-08/msg00007.php>.

Marko, I think we should look for the cause of the infinite close()
loop elsewhere.

-- Daniele

Gmane