Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1duRlR-0003CH-2z for pgsql-committers@arkaria.postgresql.org; Tue, 19 Sep 2017 23:17:13 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1duRlQ-0006N8-II for pgsql-committers@arkaria.postgresql.org; Tue, 19 Sep 2017 23:17:12 +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_2) (envelope-from ) id 1duRlA-0005VO-Et for pgsql-committers@postgresql.org; Tue, 19 Sep 2017 23:16:56 +0000 Received: from out1-smtp.messagingengine.com ([66.111.4.25]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1duRl7-0002p8-8Y for pgsql-committers@postgresql.org; Tue, 19 Sep 2017 23:16:55 +0000 Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.nyi.internal (Postfix) with ESMTP id AC99020D4D; Tue, 19 Sep 2017 19:16:51 -0400 (EDT) Received: from frontend1 ([10.202.2.160]) by compute5.internal (MEProxy); Tue, 19 Sep 2017 19:16:51 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-sender :x-me-sender:x-sasl-enc:x-sasl-enc; s=fm1; bh=3E0IKfsNvyjZPxNBN4 LzZwqDb4rdmL1K8htqQYedSWI=; b=egZDb7zJUgaURxIxnJi9cUJ6nYqQ1q4rmx OzgwANc18B3/lqZkd3OayfzC8iMtsgp1pN1ubPNyHY2eExt0YG/hU98OWtcLiRn5 4xFK74qcVfWc48PfJUK2Bqoxn6Ltx/QQ4W8jzqcZb9vCOi0zH5keIfh9Y+z6NTrq BkKr1IltiRgARW2kZGx1fv3eU4kLX3XD+QrAfRWUW2q/h7gY5AMmMUZ5JKCO4kfH e8bqo63OYXGlDmsFIMSahBIFVYJvl0N9s01eWtVGRTqtmsDYn0R9p7AMgBV72+hZ psdtTnZTm1T2fcLfWBKWFv1xJLfyVfr+Hj3xTm/l6QszyHrHbP2g== X-ME-Sender: X-Sasl-enc: 9X3tQYjnPD8RJr+cP9fQC9eztv9wB8fd+4KhOLkJdjRH 1505863011 Received: from intern.anarazel.de (unknown [136.24.76.72]) by mail.messagingengine.com (Postfix) with ESMTPA id 5A7BD7F9BA; Tue, 19 Sep 2017 19:16:51 -0400 (EDT) Date: Tue, 19 Sep 2017 16:16:50 -0700 From: Andres Freund To: Tom Lane Cc: pgsql-committers@postgresql.org Subject: Re: pgsql: Make new crash restart test a bit more robust. Message-ID: <20170919231650.mjb6awkmkrfop2rs@alap3.anarazel.de> References: <20170919192032.ctybexs5whww2rw6@alap3.anarazel.de> <28340.1505849089@sss.pgh.pa.us> <20170919202518.au4ngniiwdsyewbp@alap3.anarazel.de> <1136.1505854018@sss.pgh.pa.us> <20170919205318.sm3vcmsngd633kpd@alap3.anarazel.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20170919205318.sm3vcmsngd633kpd@alap3.anarazel.de> List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-committers Precedence: bulk Sender: pgsql-committers-owner@postgresql.org On 2017-09-19 13:53:18 -0700, Andres Freund wrote: > On 2017-09-19 16:46:58 -0400, Tom Lane wrote: > > Have we forgotten an fflush() or something? > > > > Also, maybe problem is on client side. I vaguely recall a libpq bug > > wherein it would complain about socket EOF even though data remained > > to be processed. Maybe we reintroduced something like that? > > That seems quite possible. Preface: This is largely a as of yet unverified theory. But seems worthwhile to investigate whether it's the cause of this issue or not. By changing the error message I know that the "server closed the connection unexpectedly" ERROR is coming from pqsecure_raw_read(). The caller here has to be pqReadData(). Where have the following block: if (nread > 0) { conn->inEnd += nread; /* * Hack to deal with the fact that some kernels will only give us back * 1 packet per recv() call, even if we asked for more and there is * more available. If it looks like we are reading a long message, * loop back to recv() again immediately, until we run out of data or * buffer space. Without this, the block-and-restart behavior of * libpq's higher levels leads to O(N^2) performance on long messages. * * Since we left-justified the data above, conn->inEnd gives the * amount of data already read in the current message. We consider * the message "long" once we have acquired 32k ...B */ if (conn->inEnd > 32768 && (conn->inBufSize - conn->inEnd) >= 8192) { someread = 1; goto retry3; } return 1; } So imagine we've just read one block containing the error message from the server. That's going to be less than 32kb. So we go into the retry3 path. /* OK, try to read some data */ retry3: nread = pqsecure_read(conn, conn->inBuffer + conn->inEnd, conn->inBufSize - conn->inEnd); if (nread < 0) { if (SOCK_ERRNO == EINTR) goto retry3; /* Some systems return EAGAIN/EWOULDBLOCK for no data */ #ifdef EAGAIN if (SOCK_ERRNO == EAGAIN) return someread; #endif #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN)) if (SOCK_ERRNO == EWOULDBLOCK) return someread; #endif /* We might get ECONNRESET here if using TCP and backend died */ #ifdef ECONNRESET if (SOCK_ERRNO == ECONNRESET) goto definitelyFailed; #endif /* pqsecure_read set the error message for us */ return -1; } So if the connection actually was closed we: definitelyFailed: /* Do *not* drop any already-read data; caller still wants it */ pqDropConnection(conn, false); conn->status = CONNECTION_BAD; /* No more connection to backend */ return -1; and PQgetResult() will happily signal that upwards with an error: /* Wait for some more data, and load it. */ if (flushResult || pqWait(TRUE, FALSE, conn) || pqReadData(conn) < 0) { /* * conn->errorMessage has been set by pqWait or pqReadData. We * want to append it to any already-received error message. */ pqSaveErrorResult(conn); conn->asyncStatus = PGASYNC_IDLE; return pqPrepareAsyncResult(conn); ISTM, we need to react differently if we've already partially read data successfully? Am I missing something? Greetings, Andres Freund -- Sent via pgsql-committers mailing list (pgsql-committers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-committers