Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitfa4440f

Browse files
committed
Improve libpq's error recovery for connection loss during COPY.
In pqSendSome, if the connection is already closed at entry, discard anyqueued output data before returning. There is no possibility of eversending the data, and anyway this corresponds to what we'd do if we'ddetected a hard error while trying to send(). This avoids possibleindefinite bloat of the output buffer if the application keeps tryingto send data (or even just keeps trying to do PQputCopyEnd, as psqlindeed will).Because PQputCopyEnd won't transition out of PGASYNC_COPY_IN stateuntil it's successfully queued the COPY END message, and pqPutMsgEnddoesn't distinguish a queuing failure from a pqSendSome failure,this omission allowed an infinite loop in psql if the connection closureoccurred when we had at least 8K queued to send. It might be worthrefactoring so that we can make that distinction, but for the momentthe other changes made here seem to offer adequate defenses.To guard against other variants of this scenario, do not allowPQgetResult to return a PGRES_COPY_XXX result if the connection isalready known dead. Make sure it returns PGRES_FATAL_ERROR instead.Per report from Stephen Frost. Back-patch to all active branches.
1 parent993c396 commitfa4440f

File tree

2 files changed

+36
-12
lines changed

2 files changed

+36
-12
lines changed

‎src/interfaces/libpq/fe-exec.c

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ static int PQsendQueryGuts(PGconn *conn,
6363
constint*paramFormats,
6464
intresultFormat);
6565
staticvoidparseInput(PGconn*conn);
66+
staticPGresult*getCopyResult(PGconn*conn,ExecStatusTypecopytype);
6667
staticboolPQexecStart(PGconn*conn);
6768
staticPGresult*PQexecFinish(PGconn*conn);
6869
staticintPQsendDescribe(PGconn*conn,chardesc_type,
@@ -1734,22 +1735,13 @@ PQgetResult(PGconn *conn)
17341735
conn->asyncStatus=PGASYNC_BUSY;
17351736
break;
17361737
casePGASYNC_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
casePGASYNC_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
casePGASYNC_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
returnres;
17871779
}
17881780

1781+
/*
1782+
* getCopyResult
1783+
* Helper for PQgetResult: generate result for COPY-in-progress cases
1784+
*/
1785+
staticPGresult*
1786+
getCopyResult(PGconn*conn,ExecStatusTypecopytype)
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+
returnpqPrepareAsyncResult(conn);
1801+
}
1802+
1803+
/* If we have an async result for the COPY, return that */
1804+
if (conn->result&&conn->result->resultStatus==copytype)
1805+
returnpqPrepareAsyncResult(conn);
1806+
1807+
/* Otherwise, invent a suitable PGresult */
1808+
returnPQmakeEmptyPGresult(conn,copytype);
1809+
}
1810+
17891811

17901812
/*
17911813
* PQexec

‎src/interfaces/libpq/fe-misc.c

Lines changed: 2 additions & 0 deletions
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)

[8]ページ先頭

©2009-2025 Movatter.jp