-
Notifications
You must be signed in to change notification settings - Fork 0
fix(credstore): use gofrs/flock for advisory file locking #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
appleboy
wants to merge
2
commits into
main
Choose a base branch
from
fix/credstore-flock
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,70 +1,57 @@ | ||
| package credstore | ||
|
|
||
| import ( | ||
| "errors" | ||
| "context" | ||
| "fmt" | ||
| "os" | ||
| "time" | ||
|
|
||
| "github.com/gofrs/flock" | ||
| ) | ||
|
|
||
| const ( | ||
| lockMaxRetries = 50 | ||
| lockRetryDelay = 100 * time.Millisecond | ||
| staleLockTimeout = 30 * time.Second | ||
| // lockAcquireTimeout caps how long acquireFileLock will wait for a peer | ||
| // to release. The kernel releases advisory locks automatically on process | ||
| // death, so this is only a safety net for genuinely stuck holders. | ||
| lockAcquireTimeout = 30 * time.Second | ||
|
|
||
| // lockRetryInterval controls the poll frequency while waiting for a lock. | ||
| lockRetryInterval = 100 * time.Millisecond | ||
| ) | ||
|
|
||
| // fileLock represents a file lock. | ||
| // fileLock is an exclusive advisory lock on a sibling `.lock` file. | ||
| // The lock is backed by gofrs/flock, which uses fcntl on POSIX and | ||
| // LockFileEx on Windows — the kernel releases the lock automatically if the | ||
| // holder crashes, so no stale-lock heuristics are needed. | ||
| type fileLock struct { | ||
| lockFile *os.File | ||
| lockPath string | ||
| fl *flock.Flock | ||
| } | ||
|
|
||
| // acquireFileLock acquires an exclusive lock on the token file. | ||
| // Uses a separate lock file to coordinate access across processes. | ||
| // acquireFileLock acquires an exclusive lock on `<filePath>.lock`. The lock | ||
| // file is created if missing and is left on disk after release (the file's | ||
| // presence is not what protects access — the advisory lock is). A stranded | ||
| // lock file from a crashed process does not block new acquirers because the | ||
| // kernel does not hand out the advisory lock to anyone. | ||
| func acquireFileLock(filePath string) (*fileLock, error) { | ||
| lockPath := filePath + ".lock" | ||
|
|
||
| for range lockMaxRetries { | ||
| lockFile, err := os.OpenFile(lockPath, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0o600) | ||
| if err == nil { | ||
| return &fileLock{ | ||
| lockFile: lockFile, | ||
| lockPath: lockPath, | ||
| }, nil | ||
| } | ||
|
|
||
| if os.IsExist(err) { | ||
| if info, statErr := os.Stat(lockPath); statErr == nil { | ||
| if time.Since(info.ModTime()) > staleLockTimeout { | ||
| if remErr := os.Remove(lockPath); remErr != nil && !os.IsNotExist(remErr) { | ||
| return nil, fmt.Errorf( | ||
| "failed to remove stale lock file %s: %w", | ||
| lockPath, | ||
| remErr, | ||
| ) | ||
| } | ||
| continue | ||
| } | ||
| } | ||
| time.Sleep(lockRetryDelay) | ||
| continue | ||
| } | ||
| ctx, cancel := context.WithTimeout(context.Background(), lockAcquireTimeout) | ||
| defer cancel() | ||
|
|
||
| return nil, fmt.Errorf("failed to acquire file lock: %w", err) | ||
| fl := flock.New(lockPath) | ||
| locked, err := fl.TryLockContext(ctx, lockRetryInterval) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("acquire lock %q: %w", lockPath, err) | ||
| } | ||
| if !locked { | ||
| return nil, fmt.Errorf( | ||
| "timeout waiting for file lock %q after %v", lockPath, lockAcquireTimeout, | ||
| ) | ||
| } | ||
|
|
||
| return nil, fmt.Errorf( | ||
| "timeout waiting for file lock after %v", | ||
| time.Duration(lockMaxRetries)*lockRetryDelay, | ||
| ) | ||
| return &fileLock{fl: fl}, nil | ||
| } | ||
|
|
||
| // release releases the file lock. | ||
| // release drops the advisory lock. The lock file itself is left on disk. | ||
| func (fl *fileLock) release() error { | ||
| var closeErr error | ||
| if fl.lockFile != nil { | ||
| closeErr = fl.lockFile.Close() | ||
| } | ||
| removeErr := os.Remove(fl.lockPath) | ||
| return errors.Join(closeErr, removeErr) | ||
| return fl.fl.Unlock() | ||
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
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.
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.
The doc comment is misleading: an orphaned
.lockfile is harmless because no process holds the advisory lock, not because the kernel "does not hand out" the lock. Reword this to clarify that the lock becomes immediately available to new acquirers once no process holds it (including after a crash).