From ee62da3f4b29e0fe62ed9b05599983b8b3a08934 Mon Sep 17 00:00:00 2001
From: "Stephen R. van den Berg" <srb@cuci.nl>
Date: Thu, 29 Jun 2017 09:05:43 +0200
Subject: [PATCH] pgsql: Suppress errors if the server gets lost.

---
 lib/modules/Sql.pmod/pgsql.pike      | 12 +++++++-----
 lib/modules/Sql.pmod/pgsql_util.pmod |  2 +-
 2 files changed, 8 insertions(+), 6 deletions(-)

diff --git a/lib/modules/Sql.pmod/pgsql.pike b/lib/modules/Sql.pmod/pgsql.pike
index cff8ae5375..4fc0c6c424 100644
--- a/lib/modules/Sql.pmod/pgsql.pike
+++ b/lib/modules/Sql.pmod/pgsql.pike
@@ -5,8 +5,8 @@
 //! This is an interface to the PostgreSQL database
 //! server. This module is independent of any external libraries.
 //! Note that you @b{do not@} need to have a
-//! PostgreSQL server running on your host to use this module: you can
-//! connect to the database over a TCP/IP socket.
+//! PostgreSQL server running on @b{your@} host to use this module: you can
+//! connect to the database over a TCP/IP socket on a different host.
 //!
 //! This module replaces the functionality of the older @[Sql.postgres]
 //! and @[Postgres.postgres] modules.
@@ -1193,9 +1193,11 @@ private void procmessage() {
       }
     };				// We only get here if there is an error
     if(err==MAGICTERMINATE) {	// Announce connection termination to server
-      object cs = ci->start();
-      CHAIN(cs)->add("X\0\0\0\4");
-      cs->sendcmd(SENDOUT);
+      catch {
+        object cs = ci->start();
+        CHAIN(cs)->add("X\0\0\0\4");
+        cs->sendcmd(SENDOUT);
+      };
       terminating=1;
       err=0;
     } else if(stringp(err)) {
diff --git a/lib/modules/Sql.pmod/pgsql_util.pmod b/lib/modules/Sql.pmod/pgsql_util.pmod
index 713073f8c2..7c33a8272d 100644
--- a/lib/modules/Sql.pmod/pgsql_util.pmod
+++ b/lib/modules/Sql.pmod/pgsql_util.pmod
@@ -200,7 +200,7 @@ class bufcon {
       if (mode == SYNCSEND) {
         add(PGSYNC);
         realbuffer->stashqueue->write(1);
-        mode = SENDOUT;
+        mode = SENDOUT;	    // Demote it to prevent an extra SYNC upon stashflush
       }
     }
     realbuffer->stash->add(this);
-- 
GitLab