From db6e8e1624a8f0357373450136c850f2b6e7fc8a Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Thu, 12 Nov 2015 13:03:53 -0500 Subject: Fix unwanted flushing of libpq's input buffer when socket EOF is seen. In commit 210eb9b743c0645d I centralized libpq's logic for closing down the backend communication socket, and made the new pqDropConnection routine always reset the I/O buffers to empty. Many of the call sites previously had not had such code, and while that amounted to an oversight in some cases, there was one place where it was intentional and necessary *not* to flush the input buffer: pqReadData should never cause that to happen, since we probably still want to process whatever data we read. This is the true cause of the problem Robert was attempting to fix in c3e7c24a1d60dc6a, namely that libpq no longer reported the backend's final ERROR message before reporting "server closed the connection unexpectedly". But that only accidentally fixed it, by invoking parseInput before the input buffer got flushed; and very likely there are timing scenarios where we'd still lose the message before processing it. To fix, pass a flag to pqDropConnection to tell it whether to flush the input buffer or not. On review I think flushing is actually correct for every other call site. Back-patch to 9.3 where the problem was introduced. In HEAD, also improve the comments added by c3e7c24a1d60dc6a. --- src/interfaces/libpq/fe-protocol3.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/interfaces/libpq/fe-protocol3.c') diff --git a/src/interfaces/libpq/fe-protocol3.c b/src/interfaces/libpq/fe-protocol3.c index 290f93c6ab1..4bfdc0f4710 100644 --- a/src/interfaces/libpq/fe-protocol3.c +++ b/src/interfaces/libpq/fe-protocol3.c @@ -446,8 +446,8 @@ handleSyncLoss(PGconn *conn, char id, int msgLength) /* build an error result holding the error message */ pqSaveErrorResult(conn); conn->asyncStatus = PGASYNC_READY; /* drop out of GetResult wait loop */ - - pqDropConnection(conn); + /* flush input data since we're giving up on processing it */ + pqDropConnection(conn, true); conn->status = CONNECTION_BAD; /* No more connection to backend */ } -- cgit v1.2.3