Browse Source

{my,pg}sql: close connections when out of retry

The connection is supposedly closed when blocking is False and the lock is not
acquired. Or when an exception happens in the locking code.

But if blocking is e.g. 5 seconds, then RetryError is raised but without
closing any connection at the end. This fixes that.

Change-Id: I7b40e466fe5fc01ee3f8c012f765aafca9ce716a
tags/1.57.2^0
Julien Danjou 1 year ago
parent
commit
ee66b6e4e4
2 changed files with 29 additions and 25 deletions
  1. 7
    2
      tooz/drivers/mysql.py
  2. 22
    23
      tooz/drivers/pgsql.py

+ 7
- 2
tooz/drivers/mysql.py View File

@@ -68,7 +68,6 @@ class MySQLLock(locking.Lock):
68 68
                         self.acquired = True
69 69
                         return True
70 70
             except pymysql.MySQLError as e:
71
-                self._conn.close()
72 71
                 utils.raise_with_cause(
73 72
                     tooz.ToozError,
74 73
                     encodeutils.exception_to_unicode(e),
@@ -79,7 +78,13 @@ class MySQLLock(locking.Lock):
79 78
             self._conn.close()
80 79
             return False
81 80
 
82
-        return _lock()
81
+        try:
82
+            return _lock()
83
+        except Exception:
84
+            # Close the connection if we tried too much and finally failed, or
85
+            # anything else bad happened.
86
+            self._conn.close()
87
+            raise
83 88
 
84 89
     def release(self):
85 90
         if not self.acquired:

+ 22
- 23
tooz/drivers/pgsql.py View File

@@ -123,32 +123,31 @@ class PostgresLock(locking.Lock):
123 123
             if not self._conn or self._conn.closed:
124 124
                 self._conn = PostgresDriver.get_connection(self._parsed_url,
125 125
                                                            self._options)
126
-            try:
127
-                with _translating_cursor(self._conn) as cur:
128
-                    if blocking is True:
129
-                        cur.execute("SELECT pg_advisory_lock(%s, %s);",
130
-                                    self.key)
131
-                        cur.fetchone()
126
+
127
+            with _translating_cursor(self._conn) as cur:
128
+                if blocking is True:
129
+                    cur.execute("SELECT pg_advisory_lock(%s, %s);",
130
+                                self.key)
131
+                    cur.fetchone()
132
+                    self.acquired = True
133
+                    return True
134
+                else:
135
+                    cur.execute("SELECT pg_try_advisory_lock(%s, %s);",
136
+                                self.key)
137
+                    if cur.fetchone()[0] is True:
132 138
                         self.acquired = True
133 139
                         return True
140
+                    elif blocking is False:
141
+                        self._conn.close()
142
+                        return False
134 143
                     else:
135
-                        cur.execute("SELECT pg_try_advisory_lock(%s, %s);",
136
-                                    self.key)
137
-                        if cur.fetchone()[0] is True:
138
-                            self.acquired = True
139
-                            return True
140
-                        elif blocking is False:
141
-                            self._conn.close()
142
-                            return False
143
-                        else:
144
-                            raise _retry.TryAgain
145
-            except _retry.TryAgain:
146
-                pass  # contine to retrieve lock on same conn
147
-            except Exception:
148
-                self._conn.close()
149
-                raise
150
-
151
-        return _lock()
144
+                        raise _retry.TryAgain
145
+
146
+        try:
147
+            return _lock()
148
+        except Exception:
149
+            self._conn.close()
150
+            raise
152 151
 
153 152
     def release(self):
154 153
         if not self.acquired:

Loading…
Cancel
Save