]> git.ipfire.org Git - pbs.git/commitdiff
users: Create a LDAP connection per thread
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 15 May 2023 16:19:49 +0000 (16:19 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 15 May 2023 16:19:49 +0000 (16:19 +0000)
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 <michael.tremer@ipfire.org>
src/buildservice/users.py

index 025a0cc17508d473f6a4b7f75b8037bdca538910..de5d2066bfb8f1476931f336111b978deecf17ea 100644 (file)
@@ -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)