Skip to content

Suppress ValueError in _try_break_stale_lock for corrupted lock files#496

Merged
gaborbernat merged 1 commit intotox-dev:mainfrom
bysiber:fix/soft-lock-valueerror-corrupted
Feb 21, 2026
Merged

Suppress ValueError in _try_break_stale_lock for corrupted lock files#496
gaborbernat merged 1 commit intotox-dev:mainfrom
bysiber:fix/soft-lock-valueerror-corrupted

Conversation

@bysiber
Copy link
Copy Markdown
Contributor

@bysiber bysiber commented Feb 20, 2026

SoftFileLock._try_break_stale_lock() wraps its body in suppress(OSError) to handle I/O errors gracefully. However, if the lock file content is corrupted (e.g. non-numeric PID line), int(pid_str) raises ValueError which is not suppressed, crashing the lock acquisition.

This can happen when:

  • The lock file was partially written (process killed mid-write)
  • The file was corrupted by an external tool
  • A different application wrote to the same path
import socket
from filelock import SoftFileLock

# Simulate a corrupted lock file with non-numeric PID
with open("test.lock", "w") as f:
    f.write(f"not_a_pid\n{socket.gethostname()}\n")

lock = SoftFileLock("test.lock")
lock.acquire()  # ValueError: invalid literal for int() with base 10: 'not_a_pid'

The fix adds ValueError to the suppress() call, so corrupted lock files are treated the same as unreadable ones — the stale lock check is skipped and acquisition proceeds normally.

The suppress(OSError) context manager doesn't catch ValueError, which
is raised by int(pid_str) when the lock file content is corrupted or
contains non-numeric data on the PID line. This crashes the lock
acquisition instead of gracefully skipping the stale lock check.

A corrupted lock file should be treated the same as any other
unreadable lock file — the stale lock check should be skipped
and acquisition should proceed normally.
@gaborbernat gaborbernat enabled auto-merge (squash) February 21, 2026 02:45
@gaborbernat gaborbernat merged commit a960747 into tox-dev:main Feb 21, 2026
32 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants