-
-
Notifications
You must be signed in to change notification settings - Fork 6k
Adjustments to remove dangling repository locks #32485
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
Draft
bsofiato
wants to merge
1
commit into
go-gitea:main
Choose a base branch
from
bsofiato:bugfix/repo-locks-cleanup
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.
+204
−2
Draft
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright 2024 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package git | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"time" | ||
|
||
"code.gitea.io/gitea/modules/log" | ||
"code.gitea.io/gitea/modules/setting" | ||
) | ||
|
||
func ForciblyUnlockRepository(ctx context.Context, repoPath string) error { | ||
return cleanLocksIfNeeded(repoPath, time.Now()) | ||
} | ||
|
||
func ForciblyUnlockRepositoryIfNeeded(ctx context.Context, repoPath string) error { | ||
lockThreshold := time.Now().Add(-1 * setting.Repository.DanglingLockThreshold) | ||
return cleanLocksIfNeeded(repoPath, lockThreshold) | ||
} | ||
|
||
func cleanLocksIfNeeded(repoPath string, threshold time.Time) error { | ||
if repoPath == "" { | ||
return nil | ||
} | ||
log.Trace("Checking if repository %s is locked [lock threshold is %s]", repoPath, threshold) | ||
return filepath.Walk(repoPath, func(filePath string, fileInfo os.FileInfo, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
if err := cleanLockIfNeeded(filePath, fileInfo, threshold); err != nil { | ||
log.Error("Failed to remove lock file %s: %v", filePath, err) | ||
return err | ||
} | ||
return nil | ||
}) | ||
} | ||
|
||
func cleanLockIfNeeded(filePath string, fileInfo os.FileInfo, threshold time.Time) error { | ||
if isLock(fileInfo) { | ||
if fileInfo.ModTime().Before(threshold) { | ||
if err := os.Remove(filePath); err != nil && !os.IsNotExist(err) { | ||
return err | ||
} | ||
log.Info("Lock file %s has been removed since its older than %s [timestamp: %s]", filePath, threshold, fileInfo.ModTime()) | ||
return nil | ||
} | ||
log.Warn("Cannot exclude lock file %s because it is younger than the threshold %s [timestamp: %s]", filePath, threshold, fileInfo.ModTime()) | ||
return nil | ||
} | ||
return nil | ||
} | ||
|
||
func isLock(lockFile os.FileInfo) bool { | ||
return !lockFile.IsDir() && strings.HasSuffix(lockFile.Name(), ".lock") | ||
} |
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,101 @@ | ||
// Copyright 2024 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package git | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"os/exec" | ||
"runtime" | ||
"testing" | ||
"time" | ||
|
||
"code.gitea.io/gitea/modules/setting" | ||
"code.gitea.io/gitea/modules/test" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
// This test mimics a repository having dangling locks. If the locks are older than the threshold, they should be | ||
// removed. Otherwise, they'll remain and the command will fail. | ||
|
||
func TestMaintainExistentLock(t *testing.T) { | ||
if runtime.GOOS != "linux" { | ||
// Need to use touch to change the last access time of the lock files | ||
t.Skip("Skipping test on non-linux OS") | ||
} | ||
|
||
shouldRemainLocked := func(lockFiles []string, err error) { | ||
assert.Error(t, err) | ||
for _, lockFile := range lockFiles { | ||
assert.FileExists(t, lockFile) | ||
} | ||
} | ||
|
||
shouldBeUnlocked := func(lockFiles []string, err error) { | ||
assert.NoError(t, err) | ||
for _, lockFile := range lockFiles { | ||
assert.NoFileExists(t, lockFile) | ||
} | ||
} | ||
|
||
t.Run("2 days lock file (1 hour threshold)", func(t *testing.T) { | ||
doTestLockCleanup(t, "2 days", time.Hour, shouldBeUnlocked) | ||
}) | ||
|
||
t.Run("1 hour lock file (1 hour threshold)", func(t *testing.T) { | ||
doTestLockCleanup(t, "1 hour", time.Hour, shouldBeUnlocked) | ||
}) | ||
|
||
t.Run("1 minutes lock file (1 hour threshold)", func(t *testing.T) { | ||
doTestLockCleanup(t, "1 minutes", time.Hour, shouldRemainLocked) | ||
}) | ||
|
||
t.Run("1 hour lock file (2 hour threshold)", func(t *testing.T) { | ||
doTestLockCleanup(t, "1 hour", 2*time.Hour, shouldRemainLocked) | ||
}) | ||
} | ||
|
||
func doTestLockCleanup(t *testing.T, lockAge string, threshold time.Duration, expectedResult func(lockFiles []string, err error)) { | ||
defer test.MockVariableValue(&setting.Repository, setting.Repository)() | ||
|
||
setting.Repository.DanglingLockThreshold = threshold | ||
|
||
if tmpDir, err := os.MkdirTemp("", "cleanup-after-crash"); err != nil { | ||
t.Fatal(err) | ||
} else { | ||
defer os.RemoveAll(tmpDir) | ||
|
||
if err := os.CopyFS(tmpDir, os.DirFS("../../tests/gitea-repositories-meta/org3/repo3.git")); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
lockFiles := lockFilesFor(tmpDir) | ||
|
||
os.MkdirAll(tmpDir+"/objects/info/commit-graphs", os.ModeSticky|os.ModePerm) | ||
|
||
for _, lockFile := range lockFiles { | ||
createLockFiles(t, lockFile, lockAge) | ||
} | ||
|
||
cmd := NewCommand(context.Background(), "fetch") | ||
_, _, cmdErr := cmd.RunStdString(&RunOpts{Dir: tmpDir}) | ||
|
||
expectedResult(lockFiles, cmdErr) | ||
} | ||
} | ||
|
||
func lockFilesFor(path string) []string { | ||
return []string{ | ||
path + "/config.lock", | ||
path + "/HEAD.lock", | ||
path + "/objects/info/commit-graphs/commit-graph-chain.lock", | ||
} | ||
} | ||
|
||
func createLockFiles(t *testing.T, file, lockAge string) { | ||
cmd := exec.Command("touch", "-m", "-a", "-d", "-"+lockAge, file) | ||
if err := cmd.Run(); err != nil { | ||
t.Error(err) | ||
} | ||
} |
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.
Wouldn't this log useless warnings a lot on windows?