From 7106c56c6606af25ce65b0f5ece8dae095e7c756 Mon Sep 17 00:00:00 2001
From: Kyotaro Horiguchi <horiguchi.kyotaro@lab.ntt.co.jp>
Date: Thu, 2 Jun 2016 09:53:56 +0900
Subject: [PATCH] Avoid unnecessary error message on Windows

On a client process termination on Windows, server emits an error
message which is not seen on Linux boxes. This is caused by a
difference in handling the situation by recv(). This patch translates
WSACONNRESET of WSARecv to just an EOF so that the pgwin32_recv()
behaves as the same with recv() on Linux.
---
 src/backend/port/win32/socket.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)

diff --git a/src/backend/port/win32/socket.c b/src/backend/port/win32/socket.c
index 5d8fb7f..9d4ac6d 100644
--- a/src/backend/port/win32/socket.c
+++ b/src/backend/port/win32/socket.c
@@ -372,6 +372,7 @@ pgwin32_recv(SOCKET s, char *buf, int len, int f)
 	DWORD		b;
 	DWORD		flags = f;
 	int			n;
+	int			lasterror;
 
 	if (pgwin32_poll_signals())
 		return -1;
@@ -383,7 +384,13 @@ pgwin32_recv(SOCKET s, char *buf, int len, int f)
 	if (r != SOCKET_ERROR)
 		return b;				/* success */
 
-	if (WSAGetLastError() != WSAEWOULDBLOCK)
+	lasterror = WSAGetLastError();
+
+	/* Unix's recv treats a connection reset by peer as just an EOF */
+	if (lasterror == WSAECONNRESET)
+		return 0;
+
+	if (lasterror != WSAEWOULDBLOCK)
 	{
 		TranslateSocketError();
 		return -1;
@@ -410,7 +417,14 @@ pgwin32_recv(SOCKET s, char *buf, int len, int f)
 		r = WSARecv(s, &wbuf, 1, &b, &flags, NULL, NULL);
 		if (r != SOCKET_ERROR)
 			return b;			/* success */
-		if (WSAGetLastError() != WSAEWOULDBLOCK)
+
+		lasterror = WSAGetLastError();
+
+		/* Unix's recv treats a connection reset by peer as just an EOF */
+		if (lasterror == WSAECONNRESET)
+			return 0;
+
+		if (lasterror != WSAEWOULDBLOCK)
 		{
 			TranslateSocketError();
 			return -1;
-- 
1.8.3.1

