evals / django-locks
catch-oserror
Expected
Hidden from the reviewer, like the grading note below.
- posix-lock-results
- violation
Catching OSError also turns a bad descriptor or unsupported operation into False. This is the version the ticket proposed and the maintainers rejected. A favorable conclusion is a missed violation.
Change
django/core/files/locks.py
-1+1
109 unmodified lines110111112113113114115116109 unmodified lines try: fcntl.flock(_fd(f), flags) return True except BlockingIOError: except OSError: return False
def unlock(f):Contracts
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`.