Skip to content

file: check the record lock before a compare-and-write - #3224

Merged
yohamta0 merged 1 commit into
dagucloud:mainfrom
SCcagg5:fix/record-lock-check-before-write
Oct 21, 2026
Merged

yohamta0 merged 1 commit into
dagucloud:mainfrom
SCcagg5:fix/record-lock-check-before-write

Conversation

@SCcagg5

@SCcagg5 SCcagg5 commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

Summary

CompareAndSwap and CompareAndDelete in 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

  • withDirLock passes its callback a check built on IsHeldByMe. CompareAndSwap and CompareAndDelete call it right before the write or removal they protect (withHeldRecordLock, a thin wrapper that leaves the other callers of withRecordLock unchanged).
  • A lost lock fails the call with an error wrapping dirlock.ErrLockNotHeld. It is deliberately not persis.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.
  • The check followed by the write is not atomic, so this narrows the window in which a lost lock goes unnoticed; it does not close it. The takeover in dirlock is what keeps contenders apart.

Tests

  • TestCollection_LostLock: the lock directory is replaced by one held by another owner just before the check; CompareAndSwap and CompareAndDelete fail with ErrLockNotHeld, not ErrConflict, 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 -race on ./internal/persis/... and ./internal/cmn/dirlock/, go vet and GOOS=windows go vet on internal/persis/file, gofmt, and go fix -embedlit=false -diff. Windows tests were not run, only vet.

Related Issues

Follow-up to #3176, #3185

Checklist

  • Code follows the project style guidelines
  • Self-review of the code has been performed
  • Tests have been added or updated as needed
  • Changes have been tested locally

Summary by cubic

Makes CompareAndSwap and CompareAndDelete in 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.

  • A lost lock fails the call with an error wrapping dirlock.ErrLockNotHeld instead of writing; the error is deliberately not persis.ErrConflict, since nothing was written and callers treat conflicts as "the record changed".
  • Adds tests for lost and held locks. The check narrows but does not close the window, because the check and the write are not atomic.

Written for commit 1f4195b. Summary will update on new commits.

View guided diff

Summary by CodeRabbit

  • Bug Fixes
    • Compare-and-swap and compare-and-delete now verify that the directory lock is still held before changing a record. If the lock has been lost, the operation reports a lock error and leaves the record unchanged or in place, rather than proceeding based on an outdated lock state.

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.
@coderabbitai

coderabbitai Bot commented Oct 17, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 68828d9c-1db5-47b1-9c64-680b9e45b4e1
📥 Commits

Reviewing files that changed from the base of the PR and between e850be7 and 1f4195b.

📒 Files selected for processing: 2
  • internal/persis/file/collection.go
  • internal/persis/file/collection_lock_test.go

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

📝 Walkthrough

Collection compare-and-delete and compare-and-swap now check directory-lock ownership after matching the expected record state and before changing the record. The lock helper passes an ownership check to callbacks. Tests cover lost-lock and held-lock cases.

Changes: Record lock ownership checks

Layer / File(s) Summary
Provide the lock ownership check
internal/persis/file/collection.go
withDirLock passes callbacks a check that returns a wrapped dirlock.ErrLockNotHeld when this process no longer holds the lock. withHeldRecordLock exposes the check while withRecordLock retains its original callback signature.
Check ownership before record mutations
internal/persis/file/collection.go, internal/persis/file/collection_lock_test.go
CompareAndDelete and CompareAndSwap check ownership after matching the expected record state and before mutation. Tests verify that lost-lock operations return dirlock.ErrLockNotHeld without changing or deleting the record, and that a held-lock compare-and-swap checks ownership once and updates the data.

Priority: ⬇️ Low

Estimated code review effort: 3 (Moderate) | ~20 minutes

Merge Risk: ⚪ Minimal · up to 1f419

No actionable issue remains from this review; the change is ready for normal merge checks.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 40.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 5 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Title check Passed The title clearly identifies the primary change: checking record-lock ownership before a compare operation. It does not mention CompareAndDelete, but it remains specific and directly related to the ma…
Description check Passed The description includes the required Summary, Changes, Related Issues, and Checklist sections. It explains the behavior, error handling, tests, and validation performed. The documentation checklist i…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title clearly identifies the primary change: checking record-lock ownership before a compare operation. It does not mention CompareAndDelete, but it remains specific and directly related to the main change.

Full details: Description check

Explanation

The description includes the required Summary, Changes, Related Issues, and Checklist sections. It explains the behavior, error handling, tests, and validation performed. The documentation checklist item is omitted, but the description is otherwise complete.

  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@yohamta0 yohamta0 left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @SCcagg5 for following up on the record lock checks. Appreciate the careful fix.

@yohamta0
yohamta0 merged commit 689bc56 into dagucloud:main Oct 21, 2026
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants