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

Commita88a371

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 parent7fedd79 commita88a371

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
constint*paramFormats,
6161
intresultFormat);
6262
staticvoidparseInput(PGconn*conn);
63+
staticPGresult*getCopyResult(PGconn*conn,ExecStatusTypecopytype);
6364
staticboolPQexecStart(PGconn*conn);
6465
staticPGresult*PQexecFinish(PGconn*conn);
6566
staticintPQsendDescribe(PGconn*conn,chardesc_type,
@@ -1561,16 +1562,10 @@ PQgetResult(PGconn *conn)
15611562
conn->asyncStatus=PGASYNC_BUSY;
15621563
break;
15631564
casePGASYNC_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
casePGASYNC_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
returnres;
16081603
}
16091604

1605+
/*
1606+
* getCopyResult
1607+
* Helper for PQgetResult: generate result for COPY-in-progress cases
1608+
*/
1609+
staticPGresult*
1610+
getCopyResult(PGconn*conn,ExecStatusTypecopytype)
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+
returnpqPrepareAsyncResult(conn);
1625+
}
1626+
1627+
/* If we have an async result for the COPY, return that */
1628+
if (conn->result&&conn->result->resultStatus==copytype)
1629+
returnpqPrepareAsyncResult(conn);
1630+
1631+
/* Otherwise, invent a suitable PGresult */
1632+
returnPQmakeEmptyPGresult(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)

[8]ページ先頭

©2009-2025 Movatter.jp