evals / django-locks
quiet-unlock
Expected
Hidden from the reviewer, like the grading note below.
- posix-lock-results
- violation
unlock() now returns False instead of raising when flock fails. The contract requires unlock failures to propagate. A favorable conclusion is a missed violation.
Change
django/core/files/locks.py
-1+4
113 unmodified lines114115116117117118119120121113 unmodified lines return False
def unlock(f): try: fcntl.flock(_fd(f), fcntl.LOCK_UN) fcntl.flock(_fd(f), fcntl.LOCK_UN) except OSError: return False return TrueContracts
posix-lock-results
---
name: posix-lock-results
message: Report lock contention as False and let other POSIX lock failures raise.
---
## Rules
On POSIX, `fcntl.flock` returns `None` on success and raises `OSError` on failure. `lock()` returns `True` when
`flock` succeeds. It returns `False` only when a non-blocking lock is held elsewhere, which `flock` reports as
`BlockingIOError`. Every other `OSError`, such as a bad file descriptor, propagates to the caller. Catching
`OSError` would hide those errors and change behavior for existing callers.
`unlock()` returns `True` when `flock` succeeds and lets failures propagate.
## Checks
On Linux, lock a temporary file with `LOCK_EX | LOCK_NB` from two file objects. The second call returns `False`.
Calling `lock()` with a closed file descriptor number raises `OSError`.