From: Michael Tremer Date: Mon, 15 May 2023 16:19:49 +0000 (+0000) Subject: users: Create a LDAP connection per thread X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=fa598127c8dba410ff157361168561e77333ce16;p=pbs.git users: Create a LDAP connection per thread The LDAP Python module is not thread-safe and in order to avoid any deadlocks, we just create multiple connections. Signed-off-by: Michael Tremer --- diff --git a/src/buildservice/users.py b/src/buildservice/users.py index 025a0cc1..de5d2066 100644 --- a/src/buildservice/users.py +++ b/src/buildservice/users.py @@ -5,6 +5,7 @@ import email.utils import ldap import logging import pickle +import threading import time import tornado.locale @@ -34,17 +35,22 @@ LDAP_ATTRS = ( ) class Users(base.Object): - #def init(self): - # self.ldap = ldap.LDAP(self.backend) + def init(self): + # Initialize thread-local storage + self.local = threading.local() - @lazy_property + @property def ldap(self): - ldap_uri = self.backend.config.get("ldap", "uri") + if not hasattr(self.local, "ldap"): + # Fetch the LDAP URI + ldap_uri = self.backend.config.get("ldap", "uri") + + log.debug("Connecting to %s..." % ldap_uri) - log.debug("Connecting to %s..." % ldap_uri) + # Establish LDAP connection + self.local.ldap = ldap.initialize(ldap_uri) - # Establish LDAP connection - return ldap.initialize(ldap_uri) + return self.local.ldap def _get_user(self, query, *args): res = self.db.get(query, *args)