From: Remi Gacogne Date: Sun, 14 May 2017 12:52:20 +0000 (+0200) Subject: auth: Check the connection status in addition to PQstatus() X-Git-Tag: rec-4.1.0-alpha1~34^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5e7ff7b94b7d50cb0d840dbb59551df75f8cf690;p=thirdparty%2Fpdns.git 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. --- 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()