Skip to content

Commit fa4440f

Browse files
committed
Improve libpq's error recovery for connection loss during COPY.
In pqSendSome, if the connection is already closed at entry, discard any queued output data before returning. There is no possibility of ever sending the data, and anyway this corresponds to what we'd do if we'd detected a hard error while trying to send(). This avoids possible indefinite bloat of the output buffer if the application keeps trying to send data (or even just keeps trying to do PQputCopyEnd, as psql indeed will). Because PQputCopyEnd won't transition out of PGASYNC_COPY_IN state until it's successfully queued the COPY END message, and pqPutMsgEnd doesn't distinguish a queuing failure from a pqSendSome failure, this omission allowed an infinite loop in psql if the connection closure occurred when we had at least 8K queued to send. It might be worth refactoring so that we can make that distinction, but for the moment the other changes made here seem to offer adequate defenses. To guard against other variants of this scenario, do not allow PQgetResult to return a PGRES_COPY_XXX result if the connection is already known dead. Make sure it returns PGRES_FATAL_ERROR instead. Per report from Stephen Frost. Back-patch to all active branches.
1 parent 993c396 commit fa4440f

File tree

2 files changed

+36
-12
lines changed

2 files changed

+36
-12
lines changed

src/interfaces/libpq/fe-exec.c

+34-12
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ static int PQsendQueryGuts(PGconn *conn,
6363
const int *paramFormats,
6464
int resultFormat);
6565
static void parseInput(PGconn *conn);
66+
static PGresult *getCopyResult(PGconn *conn, ExecStatusType copytype);
6667
static bool PQexecStart(PGconn *conn);
6768
static PGresult *PQexecFinish(PGconn *conn);
6869
static int PQsendDescribe(PGconn *conn, char desc_type,
@@ -1734,22 +1735,13 @@ PQgetResult(PGconn *conn)
17341735
conn->asyncStatus = PGASYNC_BUSY;
17351736
break;
17361737
case PGASYNC_COPY_IN:
1737-
if (conn->result && conn->result->resultStatus == PGRES_COPY_IN)
1738-
res = pqPrepareAsyncResult(conn);
1739-
else
1740-
res = PQmakeEmptyPGresult(conn, PGRES_COPY_IN);
1738+
res = getCopyResult(conn, PGRES_COPY_IN);
17411739
break;
17421740
case PGASYNC_COPY_OUT:
1743-
if (conn->result && conn->result->resultStatus == PGRES_COPY_OUT)
1744-
res = pqPrepareAsyncResult(conn);
1745-
else
1746-
res = PQmakeEmptyPGresult(conn, PGRES_COPY_OUT);
1741+
res = getCopyResult(conn, PGRES_COPY_OUT);
17471742
break;
17481743
case PGASYNC_COPY_BOTH:
1749-
if (conn->result && conn->result->resultStatus == PGRES_COPY_BOTH)
1750-
res = pqPrepareAsyncResult(conn);
1751-
else
1752-
res = PQmakeEmptyPGresult(conn, PGRES_COPY_BOTH);
1744+
res = getCopyResult(conn, PGRES_COPY_BOTH);
17531745
break;
17541746
default:
17551747
printfPQExpBuffer(&conn->errorMessage,
@@ -1786,6 +1778,36 @@ PQgetResult(PGconn *conn)
17861778
return res;
17871779
}
17881780

1781+
/*
1782+
* getCopyResult
1783+
* Helper for PQgetResult: generate result for COPY-in-progress cases
1784+
*/
1785+
static PGresult *
1786+
getCopyResult(PGconn *conn, ExecStatusType copytype)
1787+
{
1788+
/*
1789+
* If the server connection has been lost, don't pretend everything is
1790+
* hunky-dory; instead return a PGRES_FATAL_ERROR result, and reset the
1791+
* asyncStatus to idle (corresponding to what we'd do if we'd detected I/O
1792+
* error in the earlier steps in PQgetResult). The text returned in the
1793+
* result is whatever is in conn->errorMessage; we hope that was filled
1794+
* with something relevant when the lost connection was detected.
1795+
*/
1796+
if (conn->status != CONNECTION_OK)
1797+
{
1798+
pqSaveErrorResult(conn);
1799+
conn->asyncStatus = PGASYNC_IDLE;
1800+
return pqPrepareAsyncResult(conn);
1801+
}
1802+
1803+
/* If we have an async result for the COPY, return that */
1804+
if (conn->result && conn->result->resultStatus == copytype)
1805+
return pqPrepareAsyncResult(conn);
1806+
1807+
/* Otherwise, invent a suitable PGresult */
1808+
return PQmakeEmptyPGresult(conn, copytype);
1809+
}
1810+
17891811

17901812
/*
17911813
* PQexec

src/interfaces/libpq/fe-misc.c

+2
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,8 @@ pqSendSome(PGconn *conn, int len)
804804
{
805805
printfPQExpBuffer(&conn->errorMessage,
806806
libpq_gettext("connection not open\n"));
807+
/* Discard queued data; no chance it'll ever be sent */
808+
conn->outCount = 0;
807809
return -1;
808810
}
809811

0 commit comments

Comments
 (0)