Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1ZC9zB-0006zM-3n for pgsql-bugs@arkaria.postgresql.org; Mon, 06 Jul 2015 17:15:17 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1ZC9z9-0005iW-LF for pgsql-bugs@arkaria.postgresql.org; Mon, 06 Jul 2015 17:15:15 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1ZC9xu-0004La-PD for pgsql-bugs@postgresql.org; Mon, 06 Jul 2015 17:13:58 +0000 Received: from mail-wi0-x22c.google.com ([2a00:1450:400c:c05::22c]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84) (envelope-from ) id 1ZC9xm-0003z1-UP for pgsql-bugs@postgresql.org; Mon, 06 Jul 2015 17:13:58 +0000 Received: by widjy10 with SMTP id jy10so166884435wid.1 for ; Mon, 06 Jul 2015 10:13:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:message-id:date:from:reply-to:user-agent:mime-version:to:cc :subject:references:in-reply-to:content-type; bh=llsQ0nT+MiPStb6OwfeNBaN4+AWC2nn9jFFaf6eElh4=; b=aXv7BqcQw8RZxq5Kn3SteHzU6cyd/C38kZ5LZRWn5g9YTfgGpMcOYfGBun9qSucs6p 2xPB0YUhYmeowFzsciyrqNkTAzuKrFO0/I/s23bMh0WVFf7Mz4c8ZlQlJ77pWog6wv+m LZ7TMECO3ziMTG+I8ujKflTvP8TUOU811fUVqWSB7kbqXgzyyInJ5Mgz/VTpwMM2wFHy 9wfNV6Tc2L6TGI/cts4rxZCg8Y4PDB9q9OhPEqlHkO4Zohwbv1N45oXqwMMx8/zp5j1A n/4kmd9qrZAfaevgcIBS7Uj+oIKJsPeowRhxNenk8nAt/O5sYePYhD8WWk6yeSBO4oCz Yahg== X-Received: by 10.195.17.199 with SMTP id gg7mr103195285wjd.121.1436202829714; Mon, 06 Jul 2015 10:13:49 -0700 (PDT) Received: from [192.168.1.110] (dsl-hkibrasgw1-58c38f-82.dhcp.inet.fi. [88.195.143.82]) by smtp.gmail.com with ESMTPSA id k5sm21150914wij.1.2015.07.06.10.13.47 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 06 Jul 2015 10:13:48 -0700 (PDT) Message-ID: <559AB74A.6070602@iki.fi> Date: Mon, 06 Jul 2015 20:13:46 +0300 From: Heikki Linnakangas Reply-To: hlinnaka@iki.fi User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Icedove/31.7.0 MIME-Version: 1.0 To: Michael Paquier CC: Oleksandr Shulgin , Tom Lane , Pg Bugs Subject: Re: PQexec() hangs on OOM References: <547480DE.4040408@vmware.com> <25150.1428328002@sss.pgh.pa.us> <552427FC.6060302@iki.fi> <5526BA0A.4090600@iki.fi> <87wq03rz1t.fsf@ashulgin01.corp.ad.zalando.net> <5596B924.6020307@iki.fi> In-Reply-To: Content-Type: multipart/mixed; boundary="------------010102020909080204090308" X-Pg-Spam-Score: -2.4 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org This is a multi-part message in MIME format. --------------010102020909080204090308 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit On 07/04/2015 03:40 PM, Michael Paquier wrote: > On Sat, Jul 4, 2015 at 1:32 AM, Heikki Linnakangas wrote: >> In short, the idea of returning a status code from parseInput(), instead of >> just dealing with the error, was a bad one. Sorry I didn't have this >> epiphany earlier... >> >> I came up with the attached. It fixes the few cases where we were currently >> returning "need more data" when OOM happened, to deal with the OOM error >> instead, by setting conn->errorMessage. How does this look to you? > > So this relies on the fact that when errorMessage is set subsequent > messages are ignored, right? This looks neat. > > At the bottom of getAnotherTuple() in fe-protocol2.c if > PQmakeEmptyPGresult returns NULL, shouldn't the error message be > enforced to "out of memory"? It is an error code path, so an error > will be set, but perhaps the message is incorrect. > > - if (!res->errMsg) > - goto failure; > + if (res) > + { > + res->resultStatus = isError ? PGRES_FATAL_ERROR : > PGRES_NONFATAL_ERROR; > + res->errMsg = pqResultStrdup(res, workBuf.data); > + } > If res->errMsg is NULL, we may have a crash later on when calling > appendPQExpBufferStr on this field. I think that we should add an > additional check on it. Yeah, added. I tested the various error paths this patch modifies with a debugger, and found out that the getCopyStart changes were not doing much good. The caller still waits for the COPY-IN result, so it still gets stuck. So I left out that change. The getParamDescriptions() changes were slightly broken. It didn't read the whole input message with pqGetInt() etc., so pqParseInput3() threw the "message contents do not agree with length in message type" error. I started fixing that, by changing the error handling in that function to be more like that in getRowDescriptions(), but then I realized that all the EOF return cases in the pqParseInput3() subroutines are actually dead code. pqParseInput3() always reads the whole message, before passing it on to the right subroutine. That was documented for getRowDescriptions() and getAnotherTuple(), but the rest of the functions were more like the protocol version 2 code, prepared to deal with incomplete messages. I think it would be good to refactor that, removing the EOF return cases altogether. So I left out that change for now as well. That left me with the attached patch. It doesn't handle the getParamDescription() case, nor the getCopyStart() case, but it's a start. We don't necessarily need to fix everything in one go. Does this look correct to you, as far as it goes? - Heikki --------------010102020909080204090308 Content-Type: text/x-diff; name="improve-OOM-handling-in-libpq-3.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="improve-OOM-handling-in-libpq-3.patch" diff --git a/src/interfaces/libpq/fe-protocol2.c b/src/interfaces/libpq/fe-protocol2.c index eeba7f3..9ff73db 100644 --- a/src/interfaces/libpq/fe-protocol2.c +++ b/src/interfaces/libpq/fe-protocol2.c @@ -498,10 +498,17 @@ pqParseInput2(PGconn *conn) conn->result = PQmakeEmptyPGresult(conn, PGRES_COMMAND_OK); if (!conn->result) - return; + { + printfPQExpBuffer(&conn->errorMessage, + libpq_gettext("out of memory")); + pqSaveErrorResult(conn); + } + } + if (conn->result) + { + strlcpy(conn->result->cmdStatus, conn->workBuffer.data, + CMDSTATUS_LEN); } - strlcpy(conn->result->cmdStatus, conn->workBuffer.data, - CMDSTATUS_LEN); checkXactStatus(conn, conn->workBuffer.data); conn->asyncStatus = PGASYNC_READY; break; @@ -522,8 +529,16 @@ pqParseInput2(PGconn *conn) "unexpected character %c following empty query response (\"I\" message)", id); if (conn->result == NULL) + { conn->result = PQmakeEmptyPGresult(conn, PGRES_EMPTY_QUERY); + if (!conn->result) + { + printfPQExpBuffer(&conn->errorMessage, + libpq_gettext("out of memory")); + pqSaveErrorResult(conn); + } + } conn->asyncStatus = PGASYNC_READY; break; case 'K': /* secret key data from the backend */ @@ -965,14 +980,17 @@ pqGetErrorNotice2(PGconn *conn, bool isError) * Make a PGresult to hold the message. We temporarily lie about the * result status, so that PQmakeEmptyPGresult doesn't uselessly copy * conn->errorMessage. + * + * NB: This allocation can fail, if you run out of memory. The rest of the + * function handles that gracefully, and we still try to set the error + * message as the connection's error message. */ res = PQmakeEmptyPGresult(conn, PGRES_EMPTY_QUERY); - if (!res) - goto failure; - res->resultStatus = isError ? PGRES_FATAL_ERROR : PGRES_NONFATAL_ERROR; - res->errMsg = pqResultStrdup(res, workBuf.data); - if (!res->errMsg) - goto failure; + if (res) + { + res->resultStatus = isError ? PGRES_FATAL_ERROR : PGRES_NONFATAL_ERROR; + res->errMsg = pqResultStrdup(res, workBuf.data); + } /* * Break the message into fields. We can't do very much here, but we can @@ -1024,15 +1042,22 @@ pqGetErrorNotice2(PGconn *conn, bool isError) pqClearAsyncResult(conn); conn->result = res; resetPQExpBuffer(&conn->errorMessage); - appendPQExpBufferStr(&conn->errorMessage, res->errMsg); + if (res && !PQExpBufferDataBroken(workBuf) && res->errMsg) + appendPQExpBufferStr(&conn->errorMessage, res->errMsg); + else + printfPQExpBuffer(&conn->errorMessage, + libpq_gettext("out of memory")); if (conn->xactStatus == PQTRANS_INTRANS) conn->xactStatus = PQTRANS_INERROR; } else { - if (res->noticeHooks.noticeRec != NULL) - (*res->noticeHooks.noticeRec) (res->noticeHooks.noticeRecArg, res); - PQclear(res); + if (res) + { + if (res->noticeHooks.noticeRec != NULL) + (*res->noticeHooks.noticeRec) (res->noticeHooks.noticeRecArg, res); + PQclear(res); + } } termPQExpBuffer(&workBuf); diff --git a/src/interfaces/libpq/fe-protocol3.c b/src/interfaces/libpq/fe-protocol3.c index a847f08..dbc0d89 100644 --- a/src/interfaces/libpq/fe-protocol3.c +++ b/src/interfaces/libpq/fe-protocol3.c @@ -204,10 +204,15 @@ pqParseInput3(PGconn *conn) conn->result = PQmakeEmptyPGresult(conn, PGRES_COMMAND_OK); if (!conn->result) - return; + { + printfPQExpBuffer(&conn->errorMessage, + libpq_gettext("out of memory")); + pqSaveErrorResult(conn); + } } - strlcpy(conn->result->cmdStatus, conn->workBuffer.data, - CMDSTATUS_LEN); + if (conn->result) + strlcpy(conn->result->cmdStatus, conn->workBuffer.data, + CMDSTATUS_LEN); conn->asyncStatus = PGASYNC_READY; break; case 'E': /* error return */ @@ -226,7 +231,11 @@ pqParseInput3(PGconn *conn) conn->result = PQmakeEmptyPGresult(conn, PGRES_EMPTY_QUERY); if (!conn->result) - return; + { + printfPQExpBuffer(&conn->errorMessage, + libpq_gettext("out of memory")); + pqSaveErrorResult(conn); + } } conn->asyncStatus = PGASYNC_READY; break; @@ -239,7 +248,11 @@ pqParseInput3(PGconn *conn) conn->result = PQmakeEmptyPGresult(conn, PGRES_COMMAND_OK); if (!conn->result) - return; + { + printfPQExpBuffer(&conn->errorMessage, + libpq_gettext("out of memory")); + pqSaveErrorResult(conn); + } } conn->asyncStatus = PGASYNC_READY; } @@ -306,7 +319,11 @@ pqParseInput3(PGconn *conn) conn->result = PQmakeEmptyPGresult(conn, PGRES_COMMAND_OK); if (!conn->result) - return; + { + printfPQExpBuffer(&conn->errorMessage, + libpq_gettext("out of memory")); + pqSaveErrorResult(conn); + } } conn->asyncStatus = PGASYNC_READY; } @@ -822,11 +839,14 @@ pqGetErrorNotice3(PGconn *conn, bool isError) * Make a PGresult to hold the accumulated fields. We temporarily lie * about the result status, so that PQmakeEmptyPGresult doesn't uselessly * copy conn->errorMessage. + * + * NB: This allocation can fail, if you run out of memory. The rest of the + * function handles that gracefully, and we still try to set the error + * message as the connection's error message. */ res = PQmakeEmptyPGresult(conn, PGRES_EMPTY_QUERY); - if (!res) - goto fail; - res->resultStatus = isError ? PGRES_FATAL_ERROR : PGRES_NONFATAL_ERROR; + if (res) + res->resultStatus = isError ? PGRES_FATAL_ERROR : PGRES_NONFATAL_ERROR; /* * Read the fields and save into res. @@ -966,20 +986,27 @@ pqGetErrorNotice3(PGconn *conn, bool isError) */ if (isError) { - res->errMsg = pqResultStrdup(res, workBuf.data); - if (!res->errMsg) - goto fail; + if (res) + res->errMsg = pqResultStrdup(res, workBuf.data); pqClearAsyncResult(conn); conn->result = res; - appendPQExpBufferStr(&conn->errorMessage, workBuf.data); + if (PQExpBufferDataBroken(workBuf)) + printfPQExpBuffer(&conn->errorMessage, + libpq_gettext("out of memory")); + else + appendPQExpBufferStr(&conn->errorMessage, workBuf.data); } else { - /* We can cheat a little here and not copy the message. */ - res->errMsg = workBuf.data; - if (res->noticeHooks.noticeRec != NULL) - (*res->noticeHooks.noticeRec) (res->noticeHooks.noticeRecArg, res); - PQclear(res); + /* if we couldn't allocate the result set, just discard the NOTICE */ + if (res) + { + /* We can cheat a little here and not copy the message. */ + res->errMsg = workBuf.data; + if (res->noticeHooks.noticeRec != NULL) + (*res->noticeHooks.noticeRec) (res->noticeHooks.noticeRecArg, res); + PQclear(res); + } } termPQExpBuffer(&workBuf); --------------010102020909080204090308 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs --------------010102020909080204090308--