From ccb1175f9bd85e3ed85cc3ed5f3001e5773a8b15 Mon Sep 17 00:00:00 2001
From: "Stephen R. van den Berg" <srb@cuci.nl>
Date: Wed, 29 May 2019 10:43:45 +0200
Subject: [PATCH] pgsql: Eliminate race for db connections still open at
 program exit.

---
 lib/modules/Sql.pmod/pgsql_util.pmod | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/lib/modules/Sql.pmod/pgsql_util.pmod b/lib/modules/Sql.pmod/pgsql_util.pmod
index 645b494dae..b4d5f4d93f 100644
--- a/lib/modules/Sql.pmod/pgsql_util.pmod
+++ b/lib/modules/Sql.pmod/pgsql_util.pmod
@@ -630,7 +630,8 @@ outer:
                 destruct(result);
           };
         destruct(nostash);
-        socket->set_non_blocking();			// Drop all callbacks
+        if (socket->set_non_blocking)
+          socket->set_non_blocking();			// Drop all callbacks
         PD("%d>Close socket\n", socket->query_fd());
         socket->close();		// This will be an asynchronous close
       }
@@ -1358,7 +1359,6 @@ class sql_result {
         _fetchlimit = pgsqlsess->_fetchlimit;
         _bindportal();
         conxsess bindbuffer = c->start();
-        _unnamedstatementkey = 0;
         stmtifkey = 0;
         CHAIN(bindbuffer)->add_int8('B')->add_hstring(plugbuffer, 4, 4);
         if (!_tprepared && sizeof(_preparedname))
@@ -1368,6 +1368,7 @@ class sql_result {
                                   || sizeof(_query) >= MINPREPARELENGTH &&
                                   execfetchlimit->match(_query))
                              && _fetchlimit, bindbuffer);
+        _unnamedstatementkey = 0;
       }
     }
   }
@@ -1504,7 +1505,7 @@ class sql_result {
       datarowtypes = emptyarray;
       _ddescribe->broadcast();
     }
-    if (delayederror && !pgsqlsess.delayederror)
+    if (delayederror && pgsqlsess && !pgsqlsess.delayederror)
       pgsqlsess.delayederror = delayederror;	// Preserve error upstream
     pgsqlsess = 0;
   }
@@ -1532,7 +1533,7 @@ class sql_result {
 
   protected void destroy() {
     catch {			   // inside destructors, exceptions don't work
-      _releasesession();
+      _releasesession("ABORT");
     };
   }
 
@@ -2563,7 +2564,7 @@ class proxy {
         showportalstack("THROWN");
 #endif
         if (objectp(portal))
-          portal->_releasesession();
+          portal->_releasesession("ERROR");
         portal = 0;
         if (!waitforauthready)
           continue;		// Only continue if authentication did not fail
-- 
GitLab