Acquire the lock, waiting up to timeout seconds. Args: timeout: Maximum time to wait for lock (seconds) Returns: True if lock was acquired, False if timeout occurred Raises: KeyboardInterrupt: If user interrupts during lock acqu
(self, timeout: float = LOCK_TIMEOUT_S)
| 208 | return False |
| 209 | |
| 210 | def acquire(self, timeout: float = LOCK_TIMEOUT_S) -> bool: |
| 211 | """ |
| 212 | Acquire the lock, waiting up to timeout seconds. |
| 213 | |
| 214 | Args: |
| 215 | timeout: Maximum time to wait for lock (seconds) |
| 216 | |
| 217 | Returns: |
| 218 | True if lock was acquired, False if timeout occurred |
| 219 | |
| 220 | Raises: |
| 221 | KeyboardInterrupt: If user interrupts during lock acquisition |
| 222 | """ |
| 223 | from ci.util.global_interrupt_handler import is_interrupted |
| 224 | |
| 225 | if self._is_acquired: |
| 226 | return True # Already acquired |
| 227 | |
| 228 | # Check for stale lock BEFORE attempting acquisition |
| 229 | self._check_stale_lock() |
| 230 | |
| 231 | my_pid = os.getpid() |
| 232 | hostname = platform.node() |
| 233 | start_time = time.time() |
| 234 | warning_shown = False |
| 235 | last_stale_check = 0.0 |
| 236 | |
| 237 | while True: |
| 238 | if is_interrupted(): |
| 239 | print("\nKeyboardInterrupt: Aborting lock acquisition") |
| 240 | raise KeyboardInterrupt() |
| 241 | |
| 242 | elapsed = time.time() - start_time |
| 243 | |
| 244 | # Check for stale lock periodically (every ~1 second) |
| 245 | if elapsed - last_stale_check >= 1.0: |
| 246 | if self._check_stale_lock(): |
| 247 | print("Stale lock removed, retrying acquisition...") |
| 248 | last_stale_check = elapsed |
| 249 | |
| 250 | # Try to acquire |
| 251 | try: |
| 252 | success = self._db.try_acquire( |
| 253 | self._lock_name, |
| 254 | my_pid, |
| 255 | hostname, |
| 256 | f"build:{self.lock_name}", |
| 257 | mode="write", |
| 258 | ) |
| 259 | if success: |
| 260 | self._is_acquired = True |
| 261 | return True |
| 262 | except KeyboardInterrupt as ki: |
| 263 | handle_keyboard_interrupt(ki) |
| 264 | raise |
| 265 | except Exception: |
| 266 | pass # Continue the loop |
| 267 |