Acquire the platform lock.
(self)
| 209 | return False |
| 210 | |
| 211 | def acquire(self) -> None: |
| 212 | """Acquire the platform lock.""" |
| 213 | if self.is_locked: |
| 214 | return # Already acquired |
| 215 | |
| 216 | my_pid = os.getpid() |
| 217 | hostname = platform.node() |
| 218 | start_time = time.time() |
| 219 | warning_shown = False |
| 220 | last_stale_check = 0.0 |
| 221 | |
| 222 | while True: |
| 223 | # Check for keyboard interrupt |
| 224 | if is_interrupted(): |
| 225 | print("\nKeyboardInterrupt: Aborting platform lock acquisition") |
| 226 | raise KeyboardInterrupt() |
| 227 | |
| 228 | elapsed = time.time() - start_time |
| 229 | |
| 230 | # Check for stale lock periodically (every ~1 second) |
| 231 | if elapsed - last_stale_check >= 1.0: |
| 232 | if self._check_stale_lock(): |
| 233 | print("Stale platform lock removed, retrying acquisition...") |
| 234 | last_stale_check = elapsed |
| 235 | |
| 236 | # Try to acquire |
| 237 | try: |
| 238 | success = self._db.try_acquire( |
| 239 | self._lock_name, |
| 240 | my_pid, |
| 241 | hostname, |
| 242 | f"platform:{self.lock_file_path}", |
| 243 | mode="write", |
| 244 | ) |
| 245 | if success: |
| 246 | self.is_locked = True |
| 247 | print(f"Acquired platform lock: {self.lock_file_path}") |
| 248 | return |
| 249 | except KeyboardInterrupt as ki: |
| 250 | handle_keyboard_interrupt(ki) |
| 251 | raise |
| 252 | except Exception: |
| 253 | pass # Continue the loop |
| 254 | |
| 255 | # Check if we should show warning (after 1 second) |
| 256 | if not warning_shown and elapsed >= 1.0: |
| 257 | yellow = "\033[33m" |
| 258 | reset = "\033[0m" |
| 259 | print( |
| 260 | f"{yellow}Waiting to acquire platform lock at {self.lock_file_path}{reset}" |
| 261 | ) |
| 262 | warning_shown = True |
| 263 | |
| 264 | # Check for timeout (after 5 seconds) |
| 265 | if elapsed >= 5.0: |
| 266 | raise TimeoutError( |
| 267 | f"Failed to acquire platform lock within 5 seconds. " |
| 268 | f"Lock: {self.lock_file_path}. " |
no test coverage detected