diff --git a/pikit_api/releases.py b/pikit_api/releases.py index 0490258..5bd9c6c 100644 --- a/pikit_api/releases.py +++ b/pikit_api/releases.py @@ -400,6 +400,19 @@ def fetch_text_with_auth(url: str): def acquire_lock(): try: ensure_dir(UPDATE_LOCK.parent) + # Clear stale lock if the recorded PID is not running + if UPDATE_LOCK.exists(): + try: + pid = int(UPDATE_LOCK.read_text().strip() or "0") + if pid > 0: + os.kill(pid, 0) + else: + UPDATE_LOCK.unlink(missing_ok=True) + except OSError: + # Process not running + UPDATE_LOCK.unlink(missing_ok=True) + except Exception: + UPDATE_LOCK.unlink(missing_ok=True) lockfile = UPDATE_LOCK.open("w") fcntl.flock(lockfile.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB) lockfile.write(str(os.getpid()))