From 636750499ac4ff6ecf0eadb4e36eb859a4f99541 Mon Sep 17 00:00:00 2001 From: Zeke Brechtel <5767468+zkl2@users.noreply.github.com> Date: Sun, 12 Sep 2021 11:00:37 -0600 Subject: [PATCH] adding SSL SYSCALL error: Bad address + dialect testing --- .../dialects/postgresql/psycopg2.py | 6 +-- test/dialect/postgresql/test_dialect.py | 38 +++++++++++++++++++ 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/lib/sqlalchemy/dialects/postgresql/psycopg2.py b/lib/sqlalchemy/dialects/postgresql/psycopg2.py index e28c01f113..4b86b6ade0 100644 --- a/lib/sqlalchemy/dialects/postgresql/psycopg2.py +++ b/lib/sqlalchemy/dialects/postgresql/psycopg2.py @@ -1041,15 +1041,13 @@ class PGDialect_psycopg2(PGDialect): # 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 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]: diff --git a/test/dialect/postgresql/test_dialect.py b/test/dialect/postgresql/test_dialect.py index a86da8ce7d..a5f87b9f86 100644 --- a/test/dialect/postgresql/test_dialect.py +++ b/test/dialect/postgresql/test_dialect.py @@ -255,6 +255,44 @@ $$ 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 = getattr(pscyopg2_dialect, dialect_name).dialect(dbapi=dbapi) + 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", + # 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" -- 2.47.3