Fix race condition when opening connection to cluster (#433)

`Postgresql.connection` method could be called from different threads at the same time resulting in more than one connection open but only one used afterwards.
This commit is contained in:
Alexander Kukushkin
2017-04-18 12:44:27 +02:00
committed by GitHub
parent 8b3114a390
commit dea8f22a37
+6 -4
View File
@@ -116,6 +116,7 @@ class Postgresql(object):
self._trigger_file = config.get('recovery_conf', {}).get('trigger_file') or 'promote'
self._trigger_file = os.path.abspath(os.path.join(self._data_dir, self._trigger_file))
self._connection_lock = Lock()
self._connection = None
self._cursor_holder = None
self._sysid = None
@@ -351,10 +352,11 @@ class Postgresql(object):
return ret
def connection(self):
if not self._connection or self._connection.closed != 0:
self._connection = psycopg2.connect(**self._local_connect_kwargs)
self._connection.autocommit = True
self.server_version = self._connection.server_version
with self._connection_lock:
if not self._connection or self._connection.closed != 0:
self._connection = psycopg2.connect(**self._local_connect_kwargs)
self._connection.autocommit = True
self.server_version = self._connection.server_version
return self._connection
def _cursor(self):