3333
3434class DebugLock :
3535 """A wrapper around threading.Lock that provides detailed debugging for lock acquisition/release"""
36+
3637 def __init__ (self , name : str = "DebugLock" ):
3738 self ._lock = threading .Lock ()
3839 self ._name = name
@@ -48,6 +49,7 @@ def __init__(self, name: str = "DebugLock"):
4849 handler .setFormatter (formatter )
4950 self ._debug_logger .addHandler (handler )
5051 self ._debug_logger .setLevel (logging .DEBUG )
52+
5153 def acquire (self , blocking = True , timeout = - 1 ):
5254 current = threading .current_thread ()
5355 thread_info = f"{ current .name } -{ current .ident } "
@@ -64,7 +66,9 @@ def acquire(self, blocking=True, timeout=-1):
6466 acquired = self ._lock .acquire (blocking , timeout )
6567 if acquired :
6668 self ._owner = thread_info
67- self ._debug_logger .info (f":white_check_mark: ACQUIRED: { thread_info } got the lock" )
69+ self ._debug_logger .info (
70+ f":white_check_mark: ACQUIRED: { thread_info } got the lock"
71+ )
6872 if self ._waiters :
6973 self ._debug_logger .info (
7074 f":clipboard: WAITERS: { len (self ._waiters )} threads waiting: { self ._waiters } "
@@ -76,6 +80,7 @@ def acquire(self, blocking=True, timeout=-1):
7680 if thread_info in self ._waiters :
7781 self ._waiters .remove (thread_info )
7882 return acquired
83+
7984 def release (self ):
8085 current = threading .current_thread ()
8186 thread_info = f"{ current .name } -{ current .ident } "
@@ -84,7 +89,9 @@ def release(self):
8489 f":rotating_light: ERROR: { thread_info } trying to release lock owned by { self ._owner } "
8590 )
8691 else :
87- self ._debug_logger .info (f":unlock: RELEASED: { thread_info } released the lock" )
92+ self ._debug_logger .info (
93+ f":unlock: RELEASED: { thread_info } released the lock"
94+ )
8895 self ._owner = None
8996 # Remove from waiters if present
9097 if thread_info in self ._waiters :
@@ -94,9 +101,11 @@ def release(self):
94101 f":loudspeaker: NEXT: { len (self ._waiters )} threads still waiting: { self ._waiters } "
95102 )
96103 self ._lock .release ()
104+
97105 def __enter__ (self ):
98106 self .acquire ()
99107 return self
108+
100109 def __exit__ (self , exc_type , exc_val , exc_tb ):
101110 self .release ()
102111
@@ -426,7 +435,9 @@ class TelemetryClientFactory:
426435 _executor : Optional [ThreadPoolExecutor ] = None
427436 _initialized : bool = False
428437 # _lock = threading.Lock() # Thread safety for factory operations
429- _lock = DebugLock ("TelemetryClientFactory" ) # Thread safety for factory operations with debugging
438+ _lock = DebugLock (
439+ "TelemetryClientFactory"
440+ ) # Thread safety for factory operations with debugging
430441 _original_excepthook = None
431442 _excepthook_installed = False
432443
0 commit comments