@@ -79,7 +79,7 @@ class FileLocksSnapshot:
7979 On init, orphaned locks are removed.
8080 """
8181
82- __slots__ = ("any_has_locks" , "any_write_locks " , "any_has_write_locks " , "locks" )
82+ __slots__ = ("any_has_locks" , "any_has_write_locks " , "any_write_locks " , "locks" )
8383
8484 locks : list [LockFileMeta ]
8585 any_has_locks : bool
@@ -142,15 +142,15 @@ class AbstractLock:
142142 provides a blueprint for derived classes to implement.
143143 """
144144
145- __slots__ = ("db_name" , "need_lock " , "has_lock " , "snapshot " , "mode" , "is_alivekeep_alive_thread " )
145+ __slots__ = ("db_name" , "has_lock " , "is_alive " , "keep_alive_thread " , "mode" , "need_lock" , "snapshot " )
146146
147147 db_name : str
148148 need_lock : LockFileMeta
149149 has_lock : LockFileMeta
150150 snapshot : FileLocksSnapshot
151151 mode : str
152152 is_alive : bool
153- keep_alive_thread : threading .Thread
153+ keep_alive_thread : threading .Thread | None
154154
155155 def __init__ (self , db_name : str ) -> None :
156156 # Normalize db_name to avoid file naming conflicts
@@ -197,7 +197,8 @@ def _start_keep_alive_thread(self) -> None:
197197 """
198198
199199 if self .keep_alive_thread is not None :
200- raise RuntimeError ("Keep alive thread already exists." )
200+ msg = "Keep alive thread already exists."
201+ raise RuntimeError (msg )
201202
202203 self .is_alive = True
203204 self .keep_alive_thread = threading .Thread (target = self ._keep_alive_thread , daemon = False )
@@ -227,7 +228,7 @@ def _unlock(self) -> None:
227228 def __enter__ (self ) -> None :
228229 self ._lock ()
229230
230- def __exit__ (self , exc_type , exc_val , exc_tb ) -> None : # noqa: ANN001
231+ def __exit__ (self , exc_type , exc_val , exc_tb ) -> None :
231232 self ._unlock ()
232233
233234
@@ -248,7 +249,8 @@ def _lock(self) -> None:
248249 # If this thread already holds a read lock, raise an exception.
249250 if self .snapshot .exists (self .has_lock ):
250251 os .unlink (self .need_lock .path )
251- raise RuntimeError ("Thread already has a read lock. Do not try to obtain a read lock twice." )
252+ msg = "Thread already has a read lock. Do not try to obtain a read lock twice."
253+ raise RuntimeError (msg )
252254
253255 start_time = time .time ()
254256
@@ -264,7 +266,8 @@ def _lock(self) -> None:
264266 return
265267 time .sleep (SLEEP_TIMEOUT )
266268 if time .time () - start_time > AQUIRE_LOCK_TIMEOUT :
267- raise RuntimeError ("Timeout while waiting for read lock." )
269+ msg = "Timeout while waiting for read lock."
270+ raise RuntimeError (msg )
268271 self .snapshot = FileLocksSnapshot (self .need_lock )
269272
270273
@@ -285,7 +288,8 @@ def _lock(self) -> None:
285288 # If this thread already holds a write lock, raise an exception.
286289 if self .snapshot .exists (self .has_lock ):
287290 os .unlink (self .need_lock .path )
288- raise RuntimeError ("Thread already has a write lock. Do not try to obtain a write lock twice." )
291+ msg = "Thread already has a write lock. Do not try to obtain a write lock twice."
292+ raise RuntimeError (msg )
289293
290294 start_time = time .time ()
291295
@@ -299,5 +303,6 @@ def _lock(self) -> None:
299303 return
300304 time .sleep (SLEEP_TIMEOUT )
301305 if time .time () - start_time > AQUIRE_LOCK_TIMEOUT :
302- raise RuntimeError ("Timeout while waiting for write lock." )
306+ msg = "Timeout while waiting for write lock."
307+ raise RuntimeError (msg )
303308 self .snapshot = FileLocksSnapshot (self .need_lock )
0 commit comments