Thread: async problems?

async problems?

From
Mark Harrison
Date:
Here is a test program which dies in the postgres runtime.  I've simplified
the code as much as I can, and I cannot see where I'm doing anything wrong.

Has anybody had success with async mode?  Am I doing something wrong here?
Or are there some problems with async operation that I don't know about?

(the larger program does this in an event loop... this non-event loop
program exhibits the same behavior.)

Many TIA,
Mark

#include <stdio.h>
#include <stdlib.h>

#include "libpq-fe.h"

char fetchstr[] = "FETCH 100 in cur";

int main()
{
     PGconn* conn;
     PGresult   *res;
     int rc;
     int     tot = 0;
     int ntuples;

     conn = PQconnectdb("host=planb dbname=planb");
     printf("PQconnectdb %d\n", PQstatus(conn));

     rc = PQsetnonblocking(conn, 1);
     printf("PQsetnonblocking %d\n", rc);

     res = PQexec(conn, "BEGIN");
     printf("PQexec begin %s\n", PQresStatus(PQresultStatus(res)));
     PQclear(res);

     res = PQexec(conn,"DECLARE cur CURSOR FOR select * from assets limit 8888");    printf("PQexec declare %s\n",
PQresStatus(PQresultStatus(res)));
     PQclear(res);

     rc = PQsendQuery(conn, fetchstr);
     printf("PQsendQuery %d\n", rc);
     PQclear(res);
     PQflush(conn);

     while (1) {
         res = PQgetResult(conn);
         printf("tot=%d res=%p\n", tot, res);

         if (res == NULL) {
             rc = PQsendQuery(conn, fetchstr);
             printf("PQsendQuery %d\n", rc);
             PQflush(conn);
         }
         else {
             ntuples = PQntuples(res);
             tot += ntuples;

             if (ntuples == 0) {
                 printf("done: tot=%d\n", tot);
                 res = PQexec(conn, "CLOSE cur");
                 printf("PQexec close %s\n", PQresStatus(PQresultStatus(res)));
                 PQclear(res);
                 res = PQexec(conn, "END");
                 printf("PQexec end %s\n", PQresStatus(PQresultStatus(res)));
                 PQclear(res);
                 PQfinish(conn);
                 exit(0);
             }
             PQclear(res);

         }
     }
}


#0  chunk_alloc (ar_ptr=0x40178a00, nb=2056) at malloc.c:2983
#1  0x400c4108 in __libc_malloc (bytes=2048) at malloc.c:2811
#2  0x40021063 in pqResultAlloc (res=0x8053928, nBytes=6, isBinary=0 '\0')
     at fe-exec.c:271
#3  0x4002b1fb in getAnotherTuple (conn=0x8049e50, msgLength=21)
     at fe-protocol3.c:530
#4  0x4002a9e9 in pqParseInput3 (conn=0x8049e50) at fe-protocol3.c:271
#5  0x400221fe in parseInput (conn=0x8049e50) at fe-exec.c:998
#6  0x4002231c in PQgetResult (conn=0x8049e50) at fe-exec.c:1072
#7  0x080489bb in main () at n6.c:37
#8  0x40060627 in __libc_start_main (main=0x8048850 <main>, argc=1,
     ubp_av=0xbfffdee4, init=0x8048618 <_init>, fini=0x8048b70 <_fini>,
     rtld_fini=0x4000dcd4 <_dl_fini>, stack_end=0xbfffdedc)
     at ../sysdeps/generic/libc-start.c:129

Re: async problems?

From
Tom Lane
Date:
Mark Harrison <mh@pixar.com> writes:
>      res = PQexec(conn,"DECLARE cur CURSOR FOR select * from assets limit 8888");    printf("PQexec declare %s\n",
PQresStatus(PQresultStatus(res)));
>      PQclear(res);

>      rc = PQsendQuery(conn, fetchstr);
>      printf("PQsendQuery %d\n", rc);
>      PQclear(res);

You just re-cleared an already cleared result.  This probably caused a
double free, corrupting malloc's datastructures.  The later crash within
malloc is not surprising...

            regards, tom lane

Re: async problems?

From
Mark Harrison
Date:
Tom Lane wrote:
> Mark Harrison <mh@pixar.com> writes:
>
>>     res = PQexec(conn,"DECLARE cur CURSOR FOR select * from assets limit 8888");    printf("PQexec declare %s\n",
PQresStatus(PQresultStatus(res)));
>>     PQclear(res);
>
>
>>     rc = PQsendQuery(conn, fetchstr);
>>     printf("PQsendQuery %d\n", rc);
>>     PQclear(res);
>
>
> You just re-cleared an already cleared result.  This probably caused a
> double free, corrupting malloc's datastructures.  The later crash within
> malloc is not surprising...

Ah, everything's better now... Thanks very much!

Mark