Skip to content

Commit a88a371

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 7fedd79 commit a88a371

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

src/interfaces/libpq/fe-exec.c

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ static int PQsendQueryGuts(PGconn *conn,
6060
const int *paramFormats,
6161
int resultFormat);
6262
static void parseInput(PGconn *conn);
63+
static PGresult *getCopyResult(PGconn *conn, ExecStatusType copytype);
6364
static bool PQexecStart(PGconn *conn);
6465
static PGresult *PQexecFinish(PGconn *conn);
6566
static int PQsendDescribe(PGconn *conn, char desc_type,
@@ -1561,16 +1562,10 @@ PQgetResult(PGconn *conn)
15611562
conn->asyncStatus = PGASYNC_BUSY;
15621563
break;
15631564
case PGASYNC_COPY_IN:
1564-
if (conn->result && conn->result->resultStatus == PGRES_COPY_IN)
1565-
res = pqPrepareAsyncResult(conn);
1566-
else
1567-
res = PQmakeEmptyPGresult(conn, PGRES_COPY_IN);
1565+
res = getCopyResult(conn, PGRES_COPY_IN);
15681566
break;
15691567
case PGASYNC_COPY_OUT:
1570-
if (conn->result && conn->result->resultStatus == PGRES_COPY_OUT)
1571-
res = pqPrepareAsyncResult(conn);
1572-
else
1573-
res = PQmakeEmptyPGresult(conn, PGRES_COPY_OUT);
1568+
res = getCopyResult(conn, PGRES_COPY_OUT);
15741569
break;
15751570
default:
15761571
printfPQExpBuffer(&conn->errorMessage,
@@ -1607,6 +1602,36 @@ PQgetResult(PGconn *conn)
16071602
return res;
16081603
}
16091604

1605+
/*
1606+
* getCopyResult
1607+
* Helper for PQgetResult: generate result for COPY-in-progress cases
1608+
*/
1609+
static PGresult *
1610+
getCopyResult(PGconn *conn, ExecStatusType copytype)
1611+
{
1612+
/*
1613+
* If the server connection has been lost, don't pretend everything is
1614+
* hunky-dory; instead return a PGRES_FATAL_ERROR result, and reset the
1615+
* asyncStatus to idle (corresponding to what we'd do if we'd detected I/O
1616+
* error in the earlier steps in PQgetResult). The text returned in the
1617+
* result is whatever is in conn->errorMessage; we hope that was filled
1618+
* with something relevant when the lost connection was detected.
1619+
*/
1620+
if (conn->status != CONNECTION_OK)
1621+
{
1622+
pqSaveErrorResult(conn);
1623+
conn->asyncStatus = PGASYNC_IDLE;
1624+
return pqPrepareAsyncResult(conn);
1625+
}
1626+
1627+
/* If we have an async result for the COPY, return that */
1628+
if (conn->result && conn->result->resultStatus == copytype)
1629+
return pqPrepareAsyncResult(conn);
1630+
1631+
/* Otherwise, invent a suitable PGresult */
1632+
return PQmakeEmptyPGresult(conn, copytype);
1633+
}
1634+
16101635

16111636
/*
16121637
* PQexec

src/interfaces/libpq/fe-misc.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,8 @@ pqSendSome(PGconn *conn, int len)
774774
{
775775
printfPQExpBuffer(&conn->errorMessage,
776776
libpq_gettext("connection not open\n"));
777+
/* Discard queued data; no chance it'll ever be sent */
778+
conn->outCount = 0;
777779
return -1;
778780
}
779781

0 commit comments

Comments
 (0)