Repository navigation
LockRoster: write atomically and fail on corrupt roster JSON - #10659
underiya-ai wants to merge 1 commit into
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #10659 +/- ##
=======================================
Coverage 89.48% 89.48%
=======================================
Files 105 105
Lines 20343 20347 +4
Branches 3199 3199
=======================================
+ Hits 18203 18208 +5
+ Misses 1486 1485 -1
Partials 654 654 ☔ View full report in Codecov by Harness. |
|
Hi @ThomasWaldmann, Could you please review this PR when you have a moment? It makes Let me know if you'd like any changes! |
ThomasWaldmann
left a comment
There was a problem hiding this comment.
Thanks! Note: on master, fslocking is only used by the legacy borg 1.x repository code (legacy/repository.py, e.g. for borg transfer), see also docs/internals/frontends.rst.
1. File mode regression (must fix): tempfile.mkstemp() creates the file with mode 0600 (ignoring the umask), and os.replace() keeps that mode. With umask 002, the old open("w") gave 0664, the new code gives 0600. In a repository shared by multiple users, this breaks locking:
- while user A holds a lock, user B's
load()fails with an uncaughtPermissionError(before, B could get a shared lock); - if A's process dies, the
0600roster stays behind and stale-lock cleanup can't fix it, because B can't read it.
Please use the existing SaveFile from platform/base.py instead:
with SaveFile(self.path) as f:
json.dump(data, f)It creates the temp file with mkstemp_mode(..., mode=0o666) (so the umask is respected), syncs the file and the directory, does the atomic os.replace and cleans up the temp file on errors.
2. Possible orphaned lock: now that load() can raise, Lock.release() for an exclusive lock can fail in self._roster.modify(EXCLUSIVE, REMOVE) before reaching self._lock.release(), leaving the exclusive lock behind. Please release _lock in a try/finally there.
3. 0-byte special case vs. the rationale: the PR says a corrupt roster must not be treated as "no lockers", but a 0-byte roster still returns {}. A 0-byte file is the most likely result of a crash with the old non-atomic write (which is how borg 1.x writes it), and with atomic saves borg2 does not produce corrupt rosters at all. So the new LockFailed branch only triggers for partially written files. Please either treat both the same, or explain in the comment why 0 bytes is OK.
Also, if it raises, the error message should tell the user how to recover (e.g. remove lock.roster, or borg break-lock with borg 1.x).
4. Tests (minor):
monkeypatch.setattr("borg.fslocking.json.dump", ...)patchesdumpon the globaljsonmodule, not just infslocking.- Please add a test that the saved roster respects the umask (e.g.
0o664with umask002), skipped ifBORG_TESTS_IGNORE_MODESis set.
|
@ThomasWaldmann Thanks for the thorough review! I've updated the PR to address all four points:
Also, I've really enjoyed contributing to BorgBackup so far—if there are any other bugs, edge cases, or smaller features on |
ThomasWaldmann
left a comment
There was a problem hiding this comment.
Thanks for the update! SaveFile, the try/finally in Lock.release(), the 0-byte handling and the umask test all look good now. Two things left:
1. Recovery hint: on master, fslocking is only used for borg 1.x repositories, but borg 2's borg break-lock only works on borg 2 repositories (it has no --from-borg1), so it can't fix this. Please make the hint point to borg 1.x, e.g.:
corrupt lock roster ({err}); if no borg process is using this borg 1.x repository, remove it or run 'borg break-lock' with borg 1.x
2. test_atomic_save_preserves_existing_on_error: the fake SaveFile raises in __enter__ before anything is written, so the test passes no matter what save() does and never exercises the real SaveFile. Please let the real SaveFile run and make the write fail halfway instead. To do that without patching the global json module, replace only the json name in borg.fslocking:
def test_save_keeps_old_roster_on_write_error(self, rosterpath, monkeypatch):
roster = LockRoster(rosterpath, id=ID1)
roster.modify(SHARED, ADD)
def failing_dump(data, f):
f.write('{"shared": [') # partial write
raise OSError("simulated disk full")
monkeypatch.setattr("borg.fslocking.json", SimpleNamespace(load=json.load, dump=failing_dump))
with pytest.raises(OSError):
roster.save({"shared": []})
assert roster.get(SHARED) == {ID1} # old roster still intact
assert [p.name for p in rosterpath.parent.iterdir()] == [rosterpath.name] # no temp file left behind(I ran this locally against the PR, it passes.)
Minor: please squash the two commits into one.
ebc65c7 to
0700284
Compare
|
Thanks! I've updated the recovery hint to specifically reference borg 1.x, replaced the fake |
What does this PR fix?
Previously,
LockRoster.save()wrote directly to the roster file usingopen("w"), which truncates the file before writing. If the process is killed, interrupted, or runs out of disk space mid-write, the roster is left as truncated/corrupt JSON. Furthermore,LockRoster.load()caughtValueErrorand silently returned{}, treating a corrupt roster as "no active locks" and potentially allowing an exclusive lock while readers are still active.Solution
tempfile.mkstemp),flush()+os.fsync(), andos.replace().LockRoster.load(), continue treating missing or 0-byte empty files as{}, but raiseLockFailedon corrupt JSON instead of silently ignoring active lockers.src/borg/testsuite/fslocking_test.pyfor corrupt roster loading and preserving the existing roster ifsave()fails mid-write.