Repository navigation
file: check the record lock before a compare-and-write - #3224
Conversation
CompareAndSwap and CompareAndDelete read a record, compare it and write it under a per-record directory lock, but never checked afterwards that they still held that lock. If stale-lock recovery had handed the lock to another process in the meantime, both could write. Let withDirLock pass its callback a check built on IsHeldByMe, and call it in CompareAndSwap and CompareAndDelete right before the write they protect. A lost lock now fails the call with an error wrapping dirlock.ErrLockNotHeld instead of overwriting the record. It is not reported as persis.ErrConflict: nothing was written, and callers treat a conflict as "the record changed". The check and the write are not atomic, so this only narrows the window; the stale-lock takeover itself is what keeps contenders apart.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
📝 Walkthrough
Merge Risk: ⚪ Minimal · up to
|
Summary
CompareAndSwapandCompareAndDeletein the file backend read a record, compare it and write it under a per-record directory lock, but never check afterwards that they still hold that lock. #3185 made the stale-lock takeover safe against other contenders (#3176). This adds the follow-up that the maintainer asked for there: a re-check right before the write.Changes
withDirLockpasses its callback a check built onIsHeldByMe.CompareAndSwapandCompareAndDeletecall it right before the write or removal they protect (withHeldRecordLock, a thin wrapper that leaves the other callers ofwithRecordLockunchanged).dirlock.ErrLockNotHeld. It is deliberately notpersis.ErrConflict: nothing was written, and callers (for example the dispatch store) treat a conflict as "the record changed" and drop the entry from their index, which would lose a task that is still pending.dirlockis what keeps contenders apart.Tests
TestCollection_LostLock: the lock directory is replaced by one held by another owner just before the check;CompareAndSwapandCompareAndDeletefail withErrLockNotHeld, notErrConflict, and the record is unchanged. It fails when the check is disabled.TestCollection_HeldLock: with the lock held, the write goes through and the check runs once.Checked with
go test -raceon./internal/persis/...and./internal/cmn/dirlock/,go vetandGOOS=windows go vetoninternal/persis/file,gofmt, andgo fix -embedlit=false -diff. Windows tests were not run, only vet.Related Issues
Follow-up to #3176, #3185
Checklist
Summary by cubic
Makes
CompareAndSwapandCompareAndDeletein the file backend verify they still hold the per-record lock right before writing, so a lock taken over by stale-lock recovery no longer lets them overwrite or delete a record owned by another process.dirlock.ErrLockNotHeldinstead of writing; the error is deliberately notpersis.ErrConflict, since nothing was written and callers treat conflicts as "the record changed".Written for commit 1f4195b. Summary will update on new commits.
Summary by CodeRabbit