Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Another Variant for detecting if a connection is closed #87

Merged
merged 1 commit into from

2 participants

@dirkmueller

I seem to get "unknown error" on connections where the postgresql
database was forcefully restarted, which is not detected as a
disconnect.

@zzzeek
Owner

what if we just add a check, "if connection.closed return True" ? start moving away from checking these error codes if connection.closed is accurate.

@dirkmueller

is the "closed" attribute always guaranteed to exist? I wasn't sure on that. I can update the pull request as you proposed, though testing that change will take a bit.

@dirkmueller dirkmueller Another Variant for detecting if a connection is closed
If there is a closed attribute on the connection and
it is true, return true. Implements a todo in the code
and helps in one specific disconnect case where it previously
did not match because the error message was "unknown error".
7ee8cd8
@zzzeek
Owner

maybe not in older psycopg2 versions, otherwise it seems likely I would have used it earlier. I think the way it is is fine. so on your end you don't have a consistent way to test?

@dirkmueller

I have, it just took me some time to resetup the test environment and I was busy with OpenStack summit travel. This change seems to work for me as well.

@zzzeek
Owner

OK, how far back do you want to go, 0.9 OK or 0.8 ?

@dirkmueller

0.9 is enough for me. Thanks!

@zzzeek zzzeek merged commit 7ee8cd8 into zzzeek:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 11, 2014
  1. @dirkmueller

    Another Variant for detecting if a connection is closed

    dirkmueller authored
    If there is a closed attribute on the connection and
    it is true, return true. Implements a todo in the code
    and helps in one specific disconnect case where it previously
    did not match because the error message was "unknown error".
This page is out of date. Refresh to see the latest.
Showing with 5 additions and 1 deletion.
  1. +5 −1 lib/sqlalchemy/dialects/postgresql/psycopg2.py
View
6 lib/sqlalchemy/dialects/postgresql/psycopg2.py
@@ -489,12 +489,16 @@ def create_connect_args(self, url):
def is_disconnect(self, e, connection, cursor):
if isinstance(e, self.dbapi.Error):
+ # Is the connection already marked as closed?
+ if getattr(connection, 'closed', False):
+ return True
+
str_e = str(e).partition("\n")[0]
for msg in [
# these error messages from libpq: interfaces/libpq/fe-misc.c
# and interfaces/libpq/fe-secure.c.
# TODO: these are sent through gettext in libpq and we can't
- # check within other locales - consider using connection.closed
+ # check within other locales
'terminating connection',
'closed the connection',
'connection not open',
Something went wrong with that request. Please try again.