pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Heikki Linnakangas <hlinnaka@iki.fi>
To: Michael Paquier <michael.paquier@gmail.com>
Cc: Oleksandr Shulgin <oleksandr.shulgin@zalando.de>
Cc: Tom Lane <tgl@sss.pgh.pa.us>
Cc: Pg Bugs <pgsql-bugs@postgresql.org>
Subject: Re: PQexec() hangs on OOM
Date: Mon, 06 Jul 2015 20:13:46 +0300
Message-ID: <559AB74A.6070602@iki.fi> (raw)
In-Reply-To: <CAB7nPqSm57dvLyQsCf7+owgiNxK_sGfyFp3OdVj=YS3kRbTMqA@mail.gmail.com>
References: <547480DE.4040408@vmware.com>
	<CAB7nPqR8UtXpTOkKOuxhhXM8PzcPa4dQDKE6rYx+tZhPrQQNWg@mail.gmail.com>
	<CAB7nPqSb3RF=s+BDyq1Un2+rng20EQQyxajdTRtpRG7XsyuLnw@mail.gmail.com>
	<25150.1428328002@sss.pgh.pa.us>
	<CAB7nPqQPaDb+=GCkdZb1L-VbpJTiigPx9_UNTA1a4H2MLGi9XA@mail.gmail.com>
	<552427FC.6060302@iki.fi>
	<CAB7nPqTqJmUL_jmATPvXzfET9u6DFQxtvF2J74D7XO6QebyAEw@mail.gmail.com>
	<5526BA0A.4090600@iki.fi>
	<CAB7nPqQBPSwz+g2BN5HQyUiMEUY-Scr9jm7RyBSOeo00HFo3gw@mail.gmail.com>
	<87wq03rz1t.fsf@ashulgin01.corp.ad.zalando.net>
	<CAB7nPqT1TVnaKgfoXE7fgKhvL6-ibDWTj1BQT3TjEgp60_cH0A@mail.gmail.com>
	<5596B924.6020307@iki.fi>
	<CAB7nPqSm57dvLyQsCf7+owgiNxK_sGfyFp3OdVj=YS3kRbTMqA@mail.gmail.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

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

-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs

Attachments:

improve-OOM-handling-in-libpq-3.patch (text/x-diff, 6.9K) download 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);

view thread (48+ messages)  latest in thread

Message-ID: <559AB74A.6070602@iki.fi>
Permalink:  ../559AB74A.6070602@iki.fi/
Also on:    postgresql.org/message-id/559AB74A.6070602@iki.fi

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: hlinnaka@iki.fi, michael.paquier@gmail.com, oleksandr.shulgin@zalando.de, tgl@sss.pgh.pa.us
  Subject: Re: PQexec() hangs on OOM
  In-Reply-To: <559AB74A.6070602@iki.fi>
  References: <547480DE.4040408@vmware.com> <CAB7nPqR8UtXpTOkKOuxhhXM8PzcPa4dQDKE6rYx+tZhPrQQNWg@mail.gmail.com> <CAB7nPqSb3RF=s+BDyq1Un2+rng20EQQyxajdTRtpRG7XsyuLnw@mail.gmail.com> <25150.1428328002@sss.pgh.pa.us> <CAB7nPqQPaDb+=GCkdZb1L-VbpJTiigPx9_UNTA1a4H2MLGi9XA@mail.gmail.com> <552427FC.6060302@iki.fi> <CAB7nPqTqJmUL_jmATPvXzfET9u6DFQxtvF2J74D7XO6QebyAEw@mail.gmail.com> <5526BA0A.4090600@iki.fi> <CAB7nPqQBPSwz+g2BN5HQyUiMEUY-Scr9jm7RyBSOeo00HFo3gw@mail.gmail.com> <87wq03rz1t.fsf@ashulgin01.corp.ad.zalando.net> <CAB7nPqT1TVnaKgfoXE7fgKhvL6-ibDWTj1BQT3TjEgp60_cH0A@mail.gmail.com> <5596B924.6020307@iki.fi> <CAB7nPqSm57dvLyQsCf7+owgiNxK_sGfyFp3OdVj=YS3kRbTMqA@mail.gmail.com> <559AB74A.6070602@iki.fi>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox