aboutsummaryrefslogtreecommitdiff
path: root/src/backend/libpq/be-secure-openssl.c
diff options
context:
space:
mode:
authorAndres Freund <andres@anarazel.de>2015-02-03 22:25:20 +0100
committerAndres Freund <andres@anarazel.de>2015-02-03 22:25:20 +0100
commit4f85fde8eb860f263384fffdca660e16e77c7f76 (patch)
tree9d9d18a368f4e7db987140d6dfd384b9e2e51b0a /src/backend/libpq/be-secure-openssl.c
parent387da18874afa17156ee3af63766f17efb53c4b9 (diff)
downloadpostgresql-4f85fde8eb860f263384fffdca660e16e77c7f76.tar.gz
postgresql-4f85fde8eb860f263384fffdca660e16e77c7f76.zip
Introduce and use infrastructure for interrupt processing during client reads.
Up to now large swathes of backend code ran inside signal handlers while reading commands from the client, to allow for speedy reaction to asynchronous events. Most prominently shared invalidation and NOTIFY handling. That means that complex code like the starting/stopping of transactions is run in signal handlers... The required code was fragile and verbose, and is likely to contain bugs. That approach also severely limited what could be done while communicating with the client. As the read might be from within openssl it wasn't safely possible to trigger an error, e.g. to cancel a backend in idle-in-transaction state. We did that in some cases, namely fatal errors, nonetheless. Now that FE/BE communication in the backend employs non-blocking sockets and latches to block, we can quite simply interrupt reads from signal handlers by setting the latch. That allows us to signal an interrupted read, which is supposed to be retried after returning from within the ssl library. As signal handlers now only need to set the latch to guarantee timely interrupt processing, remove a fair amount of complicated & fragile code from async.c and sinval.c. We could now actually start to process some kinds of interrupts, like sinval ones, more often that before, but that seems better done separately. This work will hopefully allow to handle cases like being blocked by sending data, interrupting idle transactions and similar to be implemented without too much effort. In addition to allowing getting rid of ImmediateInterruptOK, that is. Author: Andres Freund Reviewed-By: Heikki Linnakangas
Diffstat (limited to 'src/backend/libpq/be-secure-openssl.c')
-rw-r--r--src/backend/libpq/be-secure-openssl.c28
1 files changed, 25 insertions, 3 deletions
diff --git a/src/backend/libpq/be-secure-openssl.c b/src/backend/libpq/be-secure-openssl.c
index 20b4742d12e..3db358e402f 100644
--- a/src/backend/libpq/be-secure-openssl.c
+++ b/src/backend/libpq/be-secure-openssl.c
@@ -511,6 +511,7 @@ be_tls_read(Port *port, void *ptr, size_t len)
ssize_t n;
int err;
int waitfor;
+ int latchret;
rloop:
errno = 0;
@@ -531,12 +532,29 @@ rloop:
break;
}
+ waitfor = WL_LATCH_SET;
+
if (err == SSL_ERROR_WANT_READ)
- waitfor = WL_SOCKET_READABLE;
+ waitfor |= WL_SOCKET_READABLE;
else
- waitfor = WL_SOCKET_WRITEABLE;
+ waitfor |= WL_SOCKET_WRITEABLE;
- WaitLatchOrSocket(MyLatch, waitfor, port->sock, 0);
+ latchret = WaitLatchOrSocket(MyLatch, waitfor, port->sock, 0);
+
+ /*
+ * We'll, among other situations, get here if the low level
+ * routine doing the actual recv() via the socket got interrupted
+ * by a signal. That's so we can handle interrupts once outside
+ * openssl, so we don't jump out from underneath its covers. We
+ * can check this both, when reading and writing, because even
+ * when writing that's just openssl's doing, not a 'proper' write
+ * initiated by postgres.
+ */
+ if (latchret & WL_LATCH_SET)
+ {
+ ResetLatch(MyLatch);
+ ProcessClientReadInterrupt(); /* preserves errno */
+ }
goto rloop;
case SSL_ERROR_SYSCALL:
/* leave it to caller to ereport the value of errno */
@@ -647,6 +665,10 @@ wloop:
waitfor = WL_SOCKET_WRITEABLE;
WaitLatchOrSocket(MyLatch, waitfor, port->sock, 0);
+ /*
+ * XXX: We'll, at some later point, likely want to add interrupt
+ * processing here.
+ */
goto wloop;
case SSL_ERROR_SYSCALL:
/* leave it to caller to ereport the value of errno */