]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Handle SSL SYSCALL error: Bad Address in postgresql/pyscopg2
authorZeke Brechtel <5767468+zkl2@users.noreply.github.com>
Sat, 25 Sep 2021 18:43:28 +0000 (14:43 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Tue, 5 Oct 2021 16:46:22 +0000 (12:46 -0400)
Added a "disconnect" condition for the "SSL SYSCALL error: Bad address"
error message as reported by psycopg2. Pull request courtesy Zeke Brechtel.

Fixes: #5387
Closes: #7087
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/7087
Pull-request-sha: 66af76a107a22d9119edc8edcacc1e4ef66dc50d

Change-Id: Ia4afc9683b8175a8ca282e07e0f83c65657544ab

doc/build/changelog/unreleased_14/5387.rst [new file with mode: 0644]
lib/sqlalchemy/dialects/postgresql/psycopg2.py
test/dialect/postgresql/test_dialect.py

diff --git a/doc/build/changelog/unreleased_14/5387.rst b/doc/build/changelog/unreleased_14/5387.rst
new file mode 100644 (file)
index 0000000..a08c169
--- /dev/null
@@ -0,0 +1,6 @@
+.. change::
+    :tags: bug, postgresql
+    :tickets: 5387
+
+    Added a "disconnect" condition for the "SSL SYSCALL error: Bad address"
+    error message as reported by psycopg2. Pull request courtesy Zeke Brechtel.
index a5a56cb6b0d066896640884e9efc997033600d38..a71bdf7606593919b29a9a8d350b9a1f2ad93f86 100644 (file)
@@ -1046,10 +1046,11 @@ class PGDialect_psycopg2(PGDialect):
                 "losed the connection unexpectedly",
                 # these can occur in newer SSL
                 "connection has been closed unexpectedly",
+                "SSL error: decryption failed or bad record mac",
                 "SSL SYSCALL error: Bad file descriptor",
                 "SSL SYSCALL error: EOF detected",
-                "SSL error: decryption failed or bad record mac",
                 "SSL SYSCALL error: Operation timed out",
+                "SSL SYSCALL error: Bad address",
             ]:
                 idx = str_e.find(msg)
                 if idx >= 0 and '"' not in str_e[:idx]:
index 155019aaea67e945d75646cd65fe421c5009474d..c0eb4410cf972fd3dd92312788df304977ab8e0d 100644 (file)
@@ -255,6 +255,42 @@ $$ LANGUAGE plpgsql;"""
         eq_(cargs, [])
         eq_(cparams["host"], "hostA:portA,hostB,hostC")
 
+    def test_psycopg2_disconnect(self):
+        class Error(Exception):
+            pass
+
+        dbapi = mock.Mock()
+        dbapi.Error = Error
+
+        dialect = psycopg2_dialect.dialect(dbapi=dbapi)
+
+        for error in [
+            # these error messages from libpq: interfaces/libpq/fe-misc.c
+            # and interfaces/libpq/fe-secure.c.
+            "terminating connection",
+            "closed the connection",
+            "connection not open",
+            "could not receive data from server",
+            "could not send data to server",
+            # psycopg2 client errors, psycopg2/conenction.h,
+            # psycopg2/cursor.h
+            "connection already closed",
+            "cursor already closed",
+            # not sure where this path is originally from, it may
+            # be obsolete.   It really says "losed", not "closed".
+            "losed the connection unexpectedly",
+            # these can occur in newer SSL
+            "connection has been closed unexpectedly",
+            "SSL error: decryption failed or bad record mac",
+            "SSL SYSCALL error: Bad file descriptor",
+            "SSL SYSCALL error: EOF detected",
+            "SSL SYSCALL error: Operation timed out",
+            "SSL SYSCALL error: Bad address",
+        ]:
+            eq_(dialect.is_disconnect(Error(error), None, None), True)
+
+        eq_(dialect.is_disconnect("not an error", None, None), False)
+
 
 class PGCodeTest(fixtures.TestBase):
     __only_on__ = "postgresql"