From 09c42481bb69cf56fe5e4d4e6e4a909fc27f1434 Mon Sep 17 00:00:00 2001 From: Andres Freund Date: Fri, 3 Mar 2017 11:02:15 -0800 Subject: [PATCH] Fix SendRemoteCommandParams() handling of a NULL MultiConnection->pgConn. (#1271) Previously we'd segfault in PQisnonblocking() which, contrary to other libpq calls, doesn't handle a NULL PQconn (because there'd be no appropriate return value for that). cr: @jasonmp85 --- .../distributed/connection/remote_commands.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/backend/distributed/connection/remote_commands.c b/src/backend/distributed/connection/remote_commands.c index a99a24870..3c9bf07f5 100644 --- a/src/backend/distributed/connection/remote_commands.c +++ b/src/backend/distributed/connection/remote_commands.c @@ -277,11 +277,22 @@ SendRemoteCommandParams(MultiConnection *connection, const char *command, const char *const *parameterValues) { PGconn *pgConn = connection->pgConn; - bool wasNonblocking = PQisnonblocking(pgConn); + bool wasNonblocking = false; int rc = 0; LogRemoteCommand(connection, command); + /* + * Don't try to send command if connection is entirely gone + * (PQisnonblocking() would crash). + */ + if (!pgConn) + { + return 0; + } + + wasNonblocking = PQisnonblocking(pgConn); + /* make sure not to block anywhere */ if (!wasNonblocking) { @@ -340,7 +351,11 @@ GetRemoteCommandResult(MultiConnection *connection, bool raiseInterrupts) PGresult *result = NULL; bool failed = false; - /* short circuit tests around the more expensive parts of this routine */ + /* + * Short circuit tests around the more expensive parts of this + * routine. This'd also trigger a return in the, unlikely, case of a + * failed/nonexistant connection. + */ if (!PQisBusy(pgConn)) { return PQgetResult(connection->pgConn);