From 7dcc245ad090d9f02599acbd23c70127ddbce8a3 Mon Sep 17 00:00:00 2001 From: Zaar Hai Date: Mon, 30 Nov 2015 14:39:07 +0200 Subject: [PATCH] Catching all syncronous errors. Fixes #134. Things like `cursor.execute` can throw not only `psycopg2.Error` exeception and their derivatives, but also standard Python errors like `IndexError` bacause badly formatted SQL. In the latter case, after such exception the connection became stale (i.e. forever-busy) in the Pool. --- momoko/connection.py | 2 +- tests.py | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/momoko/connection.py b/momoko/connection.py index 79d9ab1..5959def 100644 --- a/momoko/connection.py +++ b/momoko/connection.py @@ -457,7 +457,7 @@ def when_available(fut): log.debug("Obtained connection: %s", conn.fileno) try: future_or_result = method(conn, *args, **kwargs) - except psycopg2.Error as error: + except Exception as error: log.debug("Method failed synchronously") return self._retry(retry, when_available, conn, keep, future) diff --git a/tests.py b/tests.py index ca924b8..ac02cd7 100644 --- a/tests.py +++ b/tests.py @@ -580,6 +580,15 @@ def test_getconn_manage_with_exception(self): pass self.assertEqual(len(self.db.conns.busy), 0, msg="Some connections were not recycled") + @gen_test + def test_non_psycopg2_errors(self): + """Testing that non-psycopg2 errors are catched properly""" + try: + sql = yield self.conn.execute("SELECT %s %s;", (1,)) + except IndexError: + pass + self.assertEqual(len(self.db.conns.busy), 0, msg="Some connections were not recycled") + class MomokoPoolDataTestProxy(ProxyMixIn, MomokoPoolDataTest): pass