Skip to content

fix(dash-spv): keep the data-directory lock exclusive - #1257

Merged
xdustinface merged 1 commit into
devfrom
pasta/spv-lockfile
Nov 1, 2026
Merged

xdustinface merged 1 commit into
devfrom
pasta/spv-lockfile

Conversation

@PastaPastaPasta

@PastaPastaPasta PastaPastaPasta commented Oct 31, 2026 •

Copy link
Copy Markdown
Member

The data-directory lock in dash-spv (storage/lockfile.rs) has two gaps:

  • LockFile::new opens the lock file with truncate(true) before it calls try_lock. A second process that's refused the lock has already emptied the holder's file and erased its PID.
  • Drop removes the file while the lock is still held. A process that opened the file just before then can lock the now-unlinked inode, while a third process creates a new file at the path and locks that. Both think they own the data directory.

The fix:

  • Open without truncating, and empty the file only once the lock is ours (or locking is unsupported), right before the PID is written. A refused open leaves the file as it was.
  • After taking the lock, check on Unix that the locked file is still the one at the path (same device and inode); if it isn't, open again (up to 8 attempts). Elsewhere std has no file identity to compare, so the check is skipped and behaviour there is unchanged.

Testing:

  • New test a_refused_lock_leaves_the_holders_file: it fails on dev (the holder's PID is gone after a refused open) and passes here.
  • a_lock_on_an_unlinked_file_is_not_held drives the open/drop/lock interleaving directly. It fails if the inode check is disabled.
  • cargo test -p dash-spv passes, including the dashd regtest integration tests, as do cargo clippy --workspace --all-features --all-targets -- -D warnings and cargo fmt --check.

🤖 Generated with Claude Code

PR Hygiene · 14c744b

  • Bots — coderabbitai ✓
  • Self-review — posted; again after any push
  • Build green
  • Approvals
    • files with no dedicated owner (CHANGELOG.md) — QuantumExplorer or ZocoLini or xdustinface
    • dash-spv (dash-spv/src/storage/lockfile.rs) — QuantumExplorer or ZocoLini or xdustinface

When every merge requirement is met, the PR Hygiene check passes. Reviewer limits do not block merging; other required GitHub checks and protections still apply.

Summary by CodeRabbit

  • Bug Fixes
    • Made data-directory locking exclusive: attempts to open a directory that is already locked no longer truncate the lock holder’s PID information.
    • On Unix, opening a lock file that has been unlinked or replaced now retries against the current file. If it cannot acquire the correct lock after repeated attempts, the open is refused.

LockFile::new truncated the lock file when it opened it, before
try_lock, so a refused open erased the holder's PID. Open without
truncating and empty the file only once the lock is ours.

Drop unlinks the file before the lock is released, so an opener that
opened the file earlier could lock the unlinked inode while a later
opener created and locked a new file at the path: two holders of one
data directory. On Unix the locked file must now be the one at the
path (same device and inode), otherwise the open is retried.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between e445410 and 14c744b.

Files selected for processing: 2
  • CHANGELOG.md
  • dash-spv/src/storage/lockfile.rs

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

Walkthrough

Lock-file acquisition now avoids truncating a holder’s PID file when another opener is refused. On Unix, it checks whether the locked file still matches the path and retries if the path was replaced. Tests cover stale contents, refused locks, and an unlinked-file case.

Changes: Lock-file acquisition

Layer / File(s) Summary
Lock acquisition and validation
dash-spv/src/storage/lockfile.rs, CHANGELOG.md
LockFile::new opens without truncation, checks file identity on Unix, and retries if the path no longer refers to the opened file. It truncates and writes the PID only after acquiring the lock. Tests cover stale contents, refused locks, and an unlinked-file case. The changelog records these changes.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Merge Risk: Minimal · up to 14c74

The change keeps the data-directory lock exclusive by no longer truncating a holder's PID file on refused opens and by rechecking file identity on Unix. No merge-blocking issue was found; the only remaining gap is a minor, existing PID-file behavior on platforms without advisory locking.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 75.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documenta… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: preserving exclusivity of the dash-spv data-directory lock.
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: Docstring Coverage

Explanation

Docstring coverage is 75.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 4 functions across 1 files. (1 skipped: 1 unsupported.)

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

Finishing Touches
Generate docstrings
  • Commit to this branch
  • Create a new PR
Generate unit tests (beta)
  • Commit to this branch
  • 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

@github-actions

github-actions Bot commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

Waiting for bot review — coderabbitai not yet. Wait for the missing reviews, or a writer can post /skip-bots to proceed without them; blocking findings still need addressing.
Full checklist in the description.

@github-actions github-actions Bot added the waiting-bots Waiting for the review bots to report on this head label Oct 31, 2026
@PastaPastaPasta

Copy link
Copy Markdown
Member Author

@coderabbitai review


🤖 Posted autonomously by Claude on behalf of pasta.

@coderabbitai

coderabbitai Bot commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor
Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@github-actions

Copy link
Copy Markdown
Contributor

Bots are done — your move: post /self-reviewed.
Full checklist in the description.

@github-actions github-actions Bot added waiting-self-review Waiting for the author to post /self-reviewed and removed waiting-bots Waiting for the review bots to report on this head labels Oct 31, 2026
@codecov

codecov Bot commented Oct 31, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 87.87879% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.50%. Comparing base (e445410) to head (14c744b).

Files with missing lines Patch % Lines
dash-spv/src/storage/lockfile.rs 87.87% 8 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##              dev    #1257   +/-   ##
=======================================
  Coverage   80.49%   80.50%           
=======================================
  Files         291      291           
  Lines       77412    77463   +51     
=======================================
+ Hits        62312    62360   +48     
- Misses      15100    15103    +3     
Flag Coverage Δ
core 78.21% <ø> (ø)
ffi 56.99% <ø> (+0.09%) ⬆️
rpc 52.05% <ø> (ø)
spv 93.69% <87.87%> (-0.04%) ⬇️
wallet 82.57% <ø> (ø)
Files with missing lines Coverage Δ
dash-spv/src/storage/lockfile.rs 92.16% <87.87%> (-2.62%) ⬇️

... and 15 files with indirect coverage changes

@PastaPastaPasta

Copy link
Copy Markdown
Member Author

/self-reviewed

@github-actions

Copy link
Copy Markdown
Contributor

Ready for review — files with no dedicated owner: QuantumExplorer or ZocoLini or xdustinface · dash-spv: QuantumExplorer or ZocoLini or xdustinface.
Full checklist in the description.

@github-actions github-actions Bot added ready-for-human Bots have reported, the author has self-reviewed, and the build is green: this needs a human. and removed waiting-self-review Waiting for the author to post /self-reviewed labels Oct 31, 2026
@xdustinface
xdustinface merged commit c374c77 into dev Nov 1, 2026
42 of 43 checks passed
@xdustinface
xdustinface deleted the pasta/spv-lockfile branch November 1, 2026 00:05
@coderabbitai coderabbitai Bot mentioned this pull request Nov 1, 2026
1 of 5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-for-human Bots have reported, the author has self-reviewed, and the build is green: this needs a human.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants