GeezerVison is showing github.com — fix(securefile): reclaim stale lock files to prevent permanent DOS by euxarist · Pull Request #615 · Twigpine/zero
Conversation
euxarist
commented
Jul 9, 2026
•
edited by coderabbitai
Bot
Loading
Uh oh!
There was an error while loading. Please reload this page.
Summary
Fixes a medium-severity issue where stale .lock files left behind after abrupt process crashes (e.g. SIGKILL or system panic) cause all future attempts to create/acquire secrets to fail with a permanent timeout, resulting in a denial of service (DOS).
This PR brings the stale lock reclamation logic from internal/cron/lock.go and internal/oauth/lock.go over to internal/securefile/securefile.go:
- Write process-specific owner tokens to the
.lock file.
- Inspect the lock file modification time if O_EXCL acquisition fails.
- Reclaim lock files older than 10 seconds (
secureLockStaleAfter) using the same atomic rename-aside routine (reclaimStaleLock).
Changes
internal/securefile/securefile.go
- Define
secureLockStaleAfter and atomic counter secureLockSeq.
- Update
createSecretFile to write owner tokens, release only owned lock files, and detect and reclaim stale locks.
- Add
reclaimStaleLock helper.
internal/securefile/securefile_test.go
- Add
TestCreateSecretFileReclaimsStaleLock to verify that stale locks are correctly reclaimed.
Test plan
go test ./internal/securefile/... — ok
Summary by CodeRabbit
- Bug Fixes
- Improved secret file creation to safely detect and reclaim stale lock files.
- Updated lock handling so cleanup only occurs when the lock content still matches the current operation.
- Strengthened error handling around lock-file writing and lifecycle to reduce leftover locks.
- Tests
- Added a new test to verify stale lock reclamation during secret creation.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
When createSecretFile fails to acquire the O_EXCL lock because it already exists, check if the file is stale (older than 10 seconds). If so, atomically reclaim it via a rename-aside check, preventing a permanent DOS after abrupt process crashes. Update the lock file to write and verify process-specific tokens, and add TestCreateSecretFileReclaimsStaleLock to cover the stale reclaim flow.
coderabbitai
Bot
commented
Jul 9, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
|

No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 80e83cbb-8bbb-4472-a796-d9899a096200
📥 Commits
Reviewing files that changed from the base of the PR and between 5634a97 and 3bbdf84.
📒 Files selected for processing (1)
internal/securefile/securefile.go
🚧 Files skipped from review as they are similar to previous changes (1)
- internal/securefile/securefile.go
Walkthrough
This PR updates secret lock-file handling in internal/securefile/securefile.go with atomic lock tokens, stale-lock detection, and reclamation after a timeout. It also adds a test covering recovery from a stale .lock file.
Changes
Secure Lock Reclamation
| Layer / File(s) |
Summary |
Lock token generation and conditional cleanup internal/securefile/securefile.go |
Adds sync/atomic, secureLockStaleAfter, and secureLockSeq; lock tokens now include PID, time, and an atomic sequence, and cleanup only removes the lock file when its contents still match the token. |
Stale lock detection and reclamation internal/securefile/securefile.go |
createSecretFile now checks lock-file mtime against secureLockStaleAfter and calls reclaimStaleLock, which renames the lock, checks elapsed time, and restores or removes it. |
Stale lock reclaim test internal/securefile/securefile_test.go |
Adds time and TestCreateSecretFileReclaimsStaleLock, which backdates a lock file, calls createSecretFile, and verifies the lock is reclaimed and removed afterward. |
Estimated code review effort: 3 (Moderate) | ~25 minutes
Sequence Diagram(s)
sequenceDiagram
participant createSecretFile
participant LockFile
participant reclaimStaleLock
createSecretFile->>LockFile: inspect existing .lock mtime
LockFile-->>createSecretFile: stale or active
alt stale
createSecretFile->>reclaimStaleLock: reclaim stale lock
reclaimStaleLock->>LockFile: rename to .stale.<token>
reclaimStaleLock->>LockFile: compare elapsed time to secureLockStaleAfter
alt still stale
reclaimStaleLock->>LockFile: remove stale file
else not stale
reclaimStaleLock->>LockFile: restore original name
end
else active
createSecretFile->>createSecretFile: retry acquisition
end
createSecretFile->>LockFile: write new token and continue
Loading
Suggested reviewers: Vasanthdev2004, kevincodex1
🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)
| Check name |
Status |
Explanation |
Resolution |
| Docstring Coverage |
⚠️ Warning |
Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. |
Write docstrings for the functions missing them to satisfy the coverage threshold. |
✅ 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 accurately summarizes the main change: reclaiming stale securefile lock files to avoid a permanent denial of service. |
| 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. |
✨ Finishing Touches
🧪 Generate unit tests (beta)
- Create PR with unit tests
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.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
Stale .lock after a hard crash is a real DoS on createSecretFile (AES key mint for encrypted-file creds). Owner token + mtime stale check + rename-aside reclaim matches the proven cron/oauth pattern and avoids blind Remove races. Release only deletes when the token still matches. Test pins reclaim of a backdated lock. All CI green. Ship it.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Review
Verdict: Approve with notes (independent pass; agree with @anandh8x on the securefile fix).
Correct
Stale path.lock after hard crash is a real permanent DOS on secret mint (retry budget ~1s, then fail forever). Owner token + mtime gate + rename-aside reclaim matches the cron/oauth locker pattern and avoids blind Remove races. Release only when the lock still holds our token. Secret publish remains temp+rename. TestCreateSecretFileReclaimsStaleLock pins the happy path. CI green; go test -race ./internal/securefile/ passes. Local merge with main is clean.
Notes
- Same DOS still lives in
internal/oauth/encrypt.go createSecretFile (no token, no reclaim, blind defer os.Remove). This PR fixes the securefile/credstore path only. Follow-up: port the same pattern or share one helper.
- Recovery is after
secureLockStaleAfter (10s), not necessarily on the first post-crash call if the lock is still “fresh” within the wait window — fine, just not instantaneous.
- Process: no linked issue /
issue-approved.
No disagreement with shipping the securefile change.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Reviewers
anandh8x
anandh8x approved these changes
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
|