From 5e7ff7b94b7d50cb0d840dbb59551df75f8cf690 Mon Sep 17 00:00:00 2001 From: Remi Gacogne Date: Sun, 14 May 2017 14:52:20 +0200 Subject: [PATCH] auth: Check the connection status in addition to PQstatus() `PQstatus()` only checks the known state of the connection. It's useful because if the connection is already known to be bad we don't need to go further, but it isn't sufficient to detect whether it has been closed since the last time we used it. --- modules/gpgsqlbackend/spgsql.cc | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/modules/gpgsqlbackend/spgsql.cc b/modules/gpgsqlbackend/spgsql.cc index 78e6539a36..cfb0ca27c7 100644 --- a/modules/gpgsqlbackend/spgsql.cc +++ b/modules/gpgsqlbackend/spgsql.cc @@ -349,7 +349,29 @@ void SPgSQL::rollback() { bool SPgSQL::isConnectionUsable() { - return PQstatus(d_db) == CONNECTION_OK; + if (PQstatus(d_db) != CONNECTION_OK) { + return false; + } + + bool usable = false; + int sd = PQsocket(d_db); + bool wasNonBlocking = isNonBlocking(sd); + + if (!wasNonBlocking) { + if (!setNonBlocking(sd)) { + return usable; + } + } + + usable = isTCPSocketUsable(sd); + + if (!wasNonBlocking) { + if (!setBlocking(sd)) { + usable = false; + } + } + + return usable; } void SPgSQL::reconnect() -- 2.47.2