-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adjustments to remove dangling repository locks
Signed-off-by: Bruno Sofiato <[email protected]>
- Loading branch information
Showing
5 changed files
with
204 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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