Re: [GENERAL] \copy: unexpected response (4) - Mailing list pgsql-hackers

From Tom Lane
Subject Re: [GENERAL] \copy: unexpected response (4)
Date
Msg-id 24444.1249666439@sss.pgh.pa.us
Whole thread Raw
Responses Re: [GENERAL] \copy: unexpected response (4)  (Neil Best <nbest@ci.uchicago.edu>)
List pgsql-hackers
I wrote:
> Hmm, so it looks like the connection dropped and libpq failed to
> recognize that, or maybe libpq was okay but psql needs to check a bit
> more carefully here.  I'll take a look.

I could not reproduce this problem in testing, but after eyeballing
the code awhile I have a theory.  It looks like it is possible for
PQputCopyEnd to fail and leave the PGconn in COPY_IN state, but this
only happens (1) if the output buffer contained at least 8K already,
causing pqSendSome to be invoked from pqPutMsgEnd, and (2) pqSendSome
returned failure.  In that situation the loop in copy.c becomes
infinite, since there's no mechanism for getting out of COPY_IN state.
This case would be relatively difficult to trigger, but it seems to fit
all the facts, if we assume that the connection had failed for some
reason just at that point.  BTW, the "SSL renegotiation failure" bit
suggests that it could have been an OpenSSL bug not a real network
lossage, so you might want to see how up-to-date your openssl libraries
are.

Anyway, it seems to me that the most appropriate fix is to add some
code to that loop, along the lines of

    /*
     * Make sure we have pumped libpq dry of results; else it may still be in
     * ASYNC_BUSY state, leading to false readings in, eg, get_prompt().
     */
    while ((result = PQgetResult(pset.db)) != NULL)
    {
        success = false;
        psql_error("\\copy: unexpected response (%d)\n",
                   PQresultStatus(result));
+        /* if still in COPY IN state, try to get out of it */
+        if (PQresultStatus(result) == PGRES_COPY_IN)
+            PQputCopyEnd(conn, _("trying to exit copy mode"));
        PQclear(result);
    }

This would cover this particular case and perhaps others as well.

            regards, tom lane

pgsql-hackers by date:

Previous
From: Robert Haas
Date:
Subject: Re: mixed, named notation support
Next
From: Sam Mason
Date:
Subject: Re: Fixing geometic calculation