>From 04d18663972cf07bf283ac55b10760bbb34841b5 Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Sat, 27 Sep 2014 20:52:56 +0200
Subject: [PATCH 2/4] Make backends use a nonblocking socket to communicate
 with the frontend, block with latches.

Likely doesn't work correctly without the next patch - but it's easier
to review this way.

Author: Andres Freund
---
 src/backend/libpq/be-secure-openssl.c | 24 +++++++---------
 src/backend/libpq/be-secure.c         | 53 ++++++++++++++++++++++++++++++++++-
 src/backend/libpq/pqcomm.c            | 27 +-----------------
 src/backend/tcop/postgres.c           | 21 ++++++++++++++
 4 files changed, 84 insertions(+), 41 deletions(-)

diff --git a/src/backend/libpq/be-secure-openssl.c b/src/backend/libpq/be-secure-openssl.c
index 8d8f129..31fd004 100644
--- a/src/backend/libpq/be-secure-openssl.c
+++ b/src/backend/libpq/be-secure-openssl.c
@@ -48,6 +48,8 @@
 
 #include "postgres.h"
 
+#include "miscadmin.h"
+
 #include <sys/stat.h>
 #include <signal.h>
 #include <fcntl.h>
@@ -72,6 +74,7 @@
 
 #include "libpq/libpq.h"
 #include "tcop/tcopprot.h"
+#include "storage/proc.h"
 #include "utils/memutils.h"
 
 
@@ -371,12 +374,11 @@ aloop:
 		{
 			case SSL_ERROR_WANT_READ:
 			case SSL_ERROR_WANT_WRITE:
-#ifdef WIN32
-				pgwin32_waitforsinglesocket(SSL_get_fd(port->ssl),
-											(err == SSL_ERROR_WANT_READ) ?
-						FD_READ | FD_CLOSE | FD_ACCEPT : FD_WRITE | FD_CLOSE,
-											INFINITE);
-#endif
+				if (MyProc != NULL)
+					WaitLatchOrSocket(&MyProc->procLatch,
+									  err == SSL_ERROR_WANT_READ ?
+									  WL_SOCKET_READABLE :  WL_SOCKET_WRITEABLE,
+									  port->sock, 0);
 				goto aloop;
 			case SSL_ERROR_SYSCALL:
 				if (r < 0)
@@ -522,12 +524,6 @@ rloop:
 				n = -1;
 				break;
 			}
-#ifdef WIN32
-			pgwin32_waitforsinglesocket(SSL_get_fd(port->ssl),
-										(err == SSL_ERROR_WANT_READ) ?
-									FD_READ | FD_CLOSE : FD_WRITE | FD_CLOSE,
-										INFINITE);
-#endif
 			goto rloop;
 		case SSL_ERROR_SYSCALL:
 			/* leave it to caller to ereport the value of errno */
@@ -722,7 +718,7 @@ my_sock_read(BIO *h, char *buf, int size)
 		if (res <= 0)
 		{
 			/* If we were interrupted, tell caller to retry */
-			if (errno == EINTR)
+			if (errno == EINTR || errno == EWOULDBLOCK || errno == EAGAIN)
 			{
 				BIO_set_retry_read(h);
 			}
@@ -741,7 +737,7 @@ my_sock_write(BIO *h, const char *buf, int size)
 	BIO_clear_retry_flags(h);
 	if (res <= 0)
 	{
-		if (errno == EINTR)
+		if (errno == EINTR || errno == EWOULDBLOCK || errno == EAGAIN)
 		{
 			BIO_set_retry_write(h);
 		}
diff --git a/src/backend/libpq/be-secure.c b/src/backend/libpq/be-secure.c
index 41ec1ad..605c2be 100644
--- a/src/backend/libpq/be-secure.c
+++ b/src/backend/libpq/be-secure.c
@@ -18,6 +18,8 @@
 
 #include "postgres.h"
 
+#include "miscadmin.h"
+
 #include <sys/stat.h>
 #include <signal.h>
 #include <fcntl.h>
@@ -34,6 +36,7 @@
 #include "libpq/libpq.h"
 #include "tcop/tcopprot.h"
 #include "utils/memutils.h"
+#include "storage/proc.h"
 
 
 char	   *ssl_cert_file;
@@ -147,8 +150,27 @@ secure_raw_read(Port *port, void *ptr, size_t len)
 
 	prepare_for_client_read();
 
+	/*
+	 * Try to read from the socket without blocking. If it suceeds we're
+	 * done, otherwise we'll wait for the socket using the latch mechanism.
+	 */
+rloop:
 	n = recv(port->sock, ptr, len, 0);
 
+	if (!port->noblock && n <= 0 && (errno == EWOULDBLOCK || errno == EAGAIN))
+	{
+		int w;
+
+		Assert(MyProc);
+
+		w = WaitLatchOrSocket(&MyProc->procLatch,
+							  WL_SOCKET_READABLE,
+							  port->sock, 0);
+
+		if (w & WL_SOCKET_READABLE)
+			goto rloop;
+	}
+
 	client_read_ended();
 
 	return n;
@@ -170,7 +192,9 @@ secure_write(Port *port, void *ptr, size_t len)
 	}
 	else
 #endif
+	{
 		n = secure_raw_write(port, ptr, len);
+	}
 
 	return n;
 }
@@ -178,5 +202,32 @@ secure_write(Port *port, void *ptr, size_t len)
 ssize_t
 secure_raw_write(Port *port, const void *ptr, size_t len)
 {
-	return send(port->sock, ptr, len, 0);
+	ssize_t n;
+
+wloop:
+	n = send(port->sock, ptr, len, 0);
+
+	if (!port->noblock && n < 0 && (errno == EWOULDBLOCK || errno == EAGAIN))
+	{
+		int w;
+
+		Assert(MyProc);
+
+		/*
+		 * We probably want to check for latches being set at some point
+		 * here. That'd allow us to handle interrupts while blocked on
+		 * writes. If set we'd not retry directly, but return. That way we
+		 * don't do anything while (possibly) inside a ssl library.
+		 */
+		w = WaitLatchOrSocket(&MyProc->procLatch,
+							  WL_SOCKET_WRITEABLE,
+							  port->sock, 0);
+
+		if (w & WL_SOCKET_WRITEABLE)
+		{
+			goto wloop;
+		}
+	}
+
+	return n;
 }
diff --git a/src/backend/libpq/pqcomm.c b/src/backend/libpq/pqcomm.c
index 605d891..443a243 100644
--- a/src/backend/libpq/pqcomm.c
+++ b/src/backend/libpq/pqcomm.c
@@ -792,31 +792,6 @@ TouchSocketFiles(void)
 static void
 pq_set_nonblocking(bool nonblocking)
 {
-	if (MyProcPort->noblock == nonblocking)
-		return;
-
-#ifdef WIN32
-	pgwin32_noblock = nonblocking ? 1 : 0;
-#else
-
-	/*
-	 * Use COMMERROR on failure, because ERROR would try to send the error to
-	 * the client, which might require changing the mode again, leading to
-	 * infinite recursion.
-	 */
-	if (nonblocking)
-	{
-		if (!pg_set_noblock(MyProcPort->sock))
-			ereport(COMMERROR,
-					(errmsg("could not set socket to nonblocking mode: %m")));
-	}
-	else
-	{
-		if (!pg_set_block(MyProcPort->sock))
-			ereport(COMMERROR,
-					(errmsg("could not set socket to blocking mode: %m")));
-	}
-#endif
 	MyProcPort->noblock = nonblocking;
 }
 
@@ -945,7 +920,7 @@ pq_getbyte_if_available(unsigned char *c)
 		 * EINTR really shouldn't happen with a non-blocking socket). Report
 		 * other errors.
 		 */
-		if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
+		if (errno == EAGAIN || errno == EWOULDBLOCK)
 			r = 0;
 		else
 		{
diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c
index 61f17bf..b3a332e 100644
--- a/src/backend/tcop/postgres.c
+++ b/src/backend/tcop/postgres.c
@@ -3645,6 +3645,27 @@ PostgresMain(int argc, char *argv[],
 										 * platforms */
 	}
 
+	/*
+	 * We now always operate the underlying socket in nonblocking mode and use
+	 * WaitLatchOrSocket to implement blocking semantics if needed. We can't
+	 * put the socket into nonblocking mode earlier because latches need some
+	 * setup. We also can't ever do so while bootstrapping.
+	 *
+	 * Use COMMERROR on failure, because ERROR would try to send the error to
+	 * the client, which might require changing the mode again, leading to
+	 * infinite recursion.
+	 */
+	if (MyProcPort != NULL)
+	{
+#ifdef WIN32
+		pgwin32_noblock = true;
+#else
+		if (!pg_set_noblock(MyProcPort->sock))
+			ereport(COMMERROR,
+					(errmsg("could not set socket to nonblocking mode: %m")));
+	}
+#endif
+
 	pqinitmask();
 
 	if (IsUnderPostmaster)
-- 
1.8.3.251.g1462b67

