-
Notifications
You must be signed in to change notification settings - Fork 4
lock state file writes #7
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e568d7f
add file locking to prevent concurrent stack file edits
skarim 211e563
lock failure exit code
skarim 37e32a5
only lock for writes
skarim 629ea06
non-blocking saves for non-critical writes
skarim d5bb33e
additional lock file test
skarim fc4528b
protect against time of check vs use race condition
skarim 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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| package stack | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "time" | ||
| ) | ||
|
|
||
| const lockFileName = "gh-stack.lock" | ||
|
|
||
| // LockError is returned when the stack file lock cannot be acquired. | ||
| // Callers can check for this with errors.As to distinguish lock failures | ||
| // from other errors. | ||
| type LockError struct { | ||
| Err error | ||
| } | ||
|
|
||
| func (e *LockError) Error() string { return e.Err.Error() } | ||
| func (e *LockError) Unwrap() error { return e.Err } | ||
|
skarim marked this conversation as resolved.
|
||
|
|
||
| // StaleError is returned when the stack file was modified on disk since it | ||
| // was loaded. This indicates another process wrote to the file concurrently. | ||
| // Callers can check for this with errors.As. | ||
| type StaleError struct { | ||
| Err error | ||
| } | ||
|
|
||
| func (e *StaleError) Error() string { return e.Err.Error() } | ||
| func (e *StaleError) Unwrap() error { return e.Err } | ||
|
|
||
| // LockTimeout is how long Lock() will wait for the exclusive lock before | ||
| // giving up. With the lock held only during file writes (milliseconds), | ||
| // this timeout primarily guards against a hung process holding the lock. | ||
| var LockTimeout = 5 * time.Second | ||
|
|
||
| // lockRetryInterval is the sleep between non-blocking lock attempts. | ||
| const lockRetryInterval = 100 * time.Millisecond | ||
|
|
||
| // FileLock provides an exclusive advisory lock on the stack file to prevent | ||
| // concurrent writes between multiple gh-stack processes. | ||
| type FileLock struct { | ||
| f *os.File | ||
| } | ||
|
|
||
| // Lock acquires an exclusive lock on the stack file in the given git directory. | ||
| // It retries with a non-blocking attempt every 100ms for up to LockTimeout. | ||
| // | ||
| // Most callers should not use Lock directly — stack.Save() acquires the lock | ||
| // automatically. Use Lock only when you need to hold the lock across multiple | ||
| // operations (e.g. Load-Modify-Save as an atomic unit). | ||
|
skarim marked this conversation as resolved.
|
||
| func Lock(gitDir string) (*FileLock, error) { | ||
| path := filepath.Join(gitDir, lockFileName) | ||
| f, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0644) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("opening lock file: %w", err) | ||
| } | ||
|
|
||
| deadline := time.Now().Add(LockTimeout) | ||
| for { | ||
| err := tryLockFile(f) | ||
| if err == nil { | ||
| return &FileLock{f: f}, nil | ||
| } | ||
| if !isLockBusy(err) { | ||
| // Unexpected error (e.g. bad fd) — don't retry. | ||
| f.Close() | ||
| return nil, fmt.Errorf("locking stack file: %w", err) | ||
| } | ||
| if time.Now().After(deadline) { | ||
| f.Close() | ||
| return nil, &LockError{Err: fmt.Errorf( | ||
| "timed out waiting for stack lock after %s — another gh-stack process may be running", LockTimeout)} | ||
| } | ||
| time.Sleep(lockRetryInterval) | ||
| } | ||
|
skarim marked this conversation as resolved.
|
||
| } | ||
|
|
||
| // Unlock releases the lock. The lock file is intentionally left on disk to | ||
| // avoid a race where another process opens the same path, blocks on flock, | ||
| // then wakes up holding a lock on an unlinked inode while a third process | ||
| // creates a new file and locks a different inode. | ||
| func (l *FileLock) Unlock() { | ||
| if l == nil || l.f == nil { | ||
| return | ||
| } | ||
| unlockFile(l.f) | ||
| l.f.Close() | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.