Replace PID-file process killing with a real fcntl.flock() lock (#6)
* Replace PID-file process killing with a real fcntl.flock() lock set_backups_lock()/release_backups_lock() now use an exclusive fcntl.flock() on the lock file's inode instead of trusting the PID written inside it. The kernel releases the lock automatically on normal exit, on any signal, and on a crash - no PID liveness check or staleness heuristic is needed, and there is nothing left in this code path that sends a signal to another process. - The open file descriptor is held for the full operation in a module-level _held_locks map keyed by the backups directory, and release_backups_lock() only ever releases a descriptor this process itself acquired, never one it infers from the lock file's content. - PID and start time are still written into the lock file, but purely as diagnostics for whoever reads it (e.g. in a warning log line); they play no role in acquisition or release decisions anymore. - --force no longer sends SIGTERM/SIGKILL to the previous holder. It now blocks on the same flock() until that process releases the lock on its own (exit, signal, or crash all count). - release_backups_lock() intentionally stops unlinking the lock file: since flock() locks an inode rather than a path, unlinking while another process is blocked opening the same path would let that process (or a third one) acquire an independent lock on a fresh inode at the same path, defeating mutual exclusion. Rewrites TestBackupLock to match: a real second file descriptor with its own flock() stands in for "another process" in the fast unit tests, and a new subprocess-based test proves end-to-end that only one process can hold the lock and that killing the holder with SIGKILL frees it for the next acquirer without our code ever calling os.kill. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> * Address Copilot review feedback on flock-based lock - Make the fcntl import optional (try/except ImportError -> None), so importing curateipsum.backup no longer crashes on Windows before cli.py's own "Not supported platform" check gets a chance to run. - Guard set_backups_lock() against being called twice for a directory this process already holds: it now fails fast instead of silently overwriting the tracked fd in _held_locks, which would leak the original descriptor and leave the lock held even after release_backups_lock() closes only the newest one. - Wrap the post-acquire metadata write (ftruncate/write/fsync) in a try/except: a failure there now releases the flock and closes the fd before returning False, instead of leaving the lock held with no caller aware of it. - Cap _read_lock_metadata()'s read and strip newlines before logging, so a stale or forged lock file can't inject bogus log lines or force an unbounded read. Adds a regression test for the double-acquire guard. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> --------- Co-authored-by: Maxim Shulga <maxim.shulga@alludo.com> Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
5 files changed
+305
-263
No files matched your search
@@ -852,8 +852,10 @@ class TestErrorRecovery:
|
||||
# Release lock
|
||||
bk.release_backups_lock(str(backups_dir))
|
||||
|
||||
# Lock should be removed
|
||||
assert not os.path.exists(lock_path)
|
||||
# Lock should be free again (re-acquirable), even though the lock
|
||||
# file itself is intentionally left in place
|
||||
assert bk.set_backups_lock(str(backups_dir))
|
||||
bk.release_backups_lock(str(backups_dir))
|
||||
|
||||
# Restore original function and verify backup can proceed
|
||||
monkeypatch.undo()
|
||||
@@ -915,8 +917,10 @@ class TestErrorRecovery:
|
||||
assert os.path.exists(lock_path)
|
||||
bk.release_backups_lock(str(backups_dir))
|
||||
|
||||
# Lock should be removed
|
||||
assert not os.path.exists(lock_path)
|
||||
# Lock should be free again (re-acquirable), even though the lock
|
||||
# file itself is intentionally left in place
|
||||
assert bk.set_backups_lock(str(backups_dir))
|
||||
bk.release_backups_lock(str(backups_dir))
|
||||
|
||||
def test_permission_error_during_cleanup(
|
||||
self, integration_dirs, monkeypatch
|
||||
|
||||
Reference in new issue
Block a user