Skip to content

Commit 1af53d1

Browse files
committed
more logging of processes in process group if the lock file is found
Signed-off-by: reggie-k <regina.voloshin@codefresh.io>
1 parent d5e06bb commit 1af53d1

File tree

4 files changed

+56
-36
lines changed

4 files changed

+56
-36
lines changed

reposerver/repository/repository.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2536,19 +2536,19 @@ func fetch(gitClient git.Client, targetRevisions []string) error {
25362536
// removeStaleGitLocks best-effort removes common stale git lock files in the repository.
25372537
// It is safe to call while holding the per-repo lock and before running any git commands.
25382538

2539-
func removeStaleGitLock(repoRoot string) {
2540-
path := filepath.Join(repoRoot, ".git", "HEAD.lock")
2541-
log.Infof("Checking whether HEAD.lock exists %s", path)
2542-
if _, err := os.Stat(path); err == nil {
2543-
log.Warnf("HEAD.lock present in git repository %s, removing it", repoRoot)
2544-
if rmErr := os.Remove(path); rmErr != nil {
2545-
log.Errorf("Failed to remove git lock %s: %v", path, rmErr)
2546-
}
2547-
}
2548-
}
2539+
// func removeStaleGitLock(repoRoot string) {
2540+
// path := filepath.Join(repoRoot, ".git", "HEAD.lock")
2541+
// log.Infof("Checking whether HEAD.lock exists %s", path)
2542+
// if _, err := os.Stat(path); err == nil {
2543+
// log.Warnf("HEAD.lock present in git repository %s, removing it", repoRoot)
2544+
// if rmErr := os.Remove(path); rmErr != nil {
2545+
// log.Errorf("Failed to remove git lock %s: %v", path, rmErr)
2546+
// }
2547+
// }
2548+
// }
25492549

25502550
func checkoutRevision(gitClient git.Client, revision string, submoduleEnabled bool) error {
2551-
removeStaleGitLock(gitClient.Root())
2551+
// removeStaleGitLock(gitClient.Root())
25522552

25532553
err := gitClient.Init()
25542554
if err != nil {

reposerver/repository/repository_test.go

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3016,28 +3016,28 @@ func TestInit(t *testing.T) {
30163016
initGitRepo(t, newGitRepoOptions{path: path.Join(dir, "repo2"), remote: "https://github.com/argo-cd/test-repo2", createPath: true, addEmptyCommit: false})
30173017
}
30183018

3019-
func TestRemoveStaleGitHeadLock(t *testing.T) {
3020-
dir := t.TempDir()
3021-
// create a fake repo structure with .git/HEAD.lock
3022-
gitDir := path.Join(dir, ".git")
3023-
require.NoError(t, os.MkdirAll(gitDir, 0o755))
3024-
headLock := path.Join(gitDir, "HEAD.lock")
3025-
require.NoError(t, os.WriteFile(headLock, []byte("test"), 0o644))
3026-
3027-
// sanity: lock exists before
3028-
_, err := os.Stat(headLock)
3029-
require.NoError(t, err)
3019+
// func TestRemoveStaleGitHeadLock(t *testing.T) {
3020+
// dir := t.TempDir()
3021+
// // create a fake repo structure with .git/HEAD.lock
3022+
// gitDir := path.Join(dir, ".git")
3023+
// require.NoError(t, os.MkdirAll(gitDir, 0o755))
3024+
// headLock := path.Join(gitDir, "HEAD.lock")
3025+
// require.NoError(t, os.WriteFile(headLock, []byte("test"), 0o644))
30303026

3031-
removeStaleGitLock(dir)
3027+
// // sanity: lock exists before
3028+
// _, err := os.Stat(headLock)
3029+
// require.NoError(t, err)
30323030

3033-
// lock should be gone after cleanup
3034-
_, err = os.Stat(headLock)
3035-
require.Error(t, err)
3036-
require.True(t, os.IsNotExist(err))
3031+
// removeStaleGitLock(dir)
30373032

3038-
// calling again should be a no-op (no error)
3039-
removeStaleGitLock(dir)
3040-
}
3033+
// // lock should be gone after cleanup
3034+
// _, err = os.Stat(headLock)
3035+
// require.Error(t, err)
3036+
// require.True(t, os.IsNotExist(err))
3037+
3038+
// // calling again should be a no-op (no error)
3039+
// removeStaleGitLock(dir)
3040+
// }
30413041

30423042
// TestCheckoutRevisionCanGetNonstandardRefs shows that we can fetch a revision that points to a non-standard ref. In
30433043
// other words, we haven't regressed and caused this issue again: https://github.com/argoproj/argo-cd/issues/4935
@@ -3080,7 +3080,7 @@ func TestCheckoutRevisionPresentSkipFetch(t *testing.T) {
30803080

30813081
gitClient := &gitmocks.Client{}
30823082
gitClient.On("Init").Return(nil)
3083-
gitClient.On("Root").Return("<repo-root>")
3083+
// gitClient.On("Root").Return("<repo-root>")
30843084
gitClient.On("IsRevisionPresent", revision).Return(true)
30853085
gitClient.On("Checkout", revision, mock.Anything).Return("", nil)
30863086

@@ -3093,7 +3093,7 @@ func TestCheckoutRevisionNotPresentCallFetch(t *testing.T) {
30933093

30943094
gitClient := &gitmocks.Client{}
30953095
gitClient.On("Init").Return(nil)
3096-
gitClient.On("Root").Return("<repo-root>")
3096+
// gitClient.On("Root").Return("<repo-root>")
30973097
gitClient.On("IsRevisionPresent", revision).Return(false)
30983098
gitClient.On("Fetch", "").Return(nil)
30993099
gitClient.On("Checkout", revision, mock.Anything).Return("", nil)
@@ -3108,7 +3108,7 @@ func TestFetch(t *testing.T) {
31083108

31093109
gitClient := &gitmocks.Client{}
31103110
gitClient.On("Init").Return(nil)
3111-
gitClient.On("Root").Return("<repo-root>")
3111+
// gitClient.On("Root").Return("<repo-root>")
31123112
gitClient.On("IsRevisionPresent", revision1).Once().Return(true)
31133113
gitClient.On("IsRevisionPresent", revision2).Once().Return(false)
31143114
gitClient.On("Fetch", "").Return(nil)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
marker

util/exec/exec.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,13 +190,31 @@ func RunCommandExt(cmd *exec.Cmd, opts CmdOpts) (string, error) {
190190
return
191191
}
192192
lockPath := filepath.Join(cmd.Dir, ".git", "HEAD.lock")
193-
_, statErr := os.Stat(lockPath)
193+
fileInfo, statErr := os.Stat(lockPath)
194194
exists := statErr == nil
195-
logCtx.WithFields(logrus.Fields{
195+
fields := logrus.Fields{
196196
"headLockPath": lockPath,
197197
"headLockExists": exists,
198198
"where": where,
199-
}).Info("HEAD.lock status execId=" + execId)
199+
}
200+
if exists {
201+
fields["headLockSize"] = fileInfo.Size()
202+
fields["headLockMode"] = fileInfo.Mode().String()
203+
fields["headLockModTime"] = fileInfo.ModTime()
204+
fields["headLockIsDir"] = fileInfo.IsDir()
205+
206+
pgid, pgErr := syscall.Getpgid(cmd.Process.Pid)
207+
if pgErr == nil && pgid > 0 {
208+
out, _ := exec.Command(
209+
"ps",
210+
"-o", "pid,ppid,pgid,etime,comm,args",
211+
"--no-headers",
212+
"--pgroup", strconv.Itoa(pgid),
213+
).CombinedOutput()
214+
fields["processesInGroup"] = strings.TrimSpace(string(out))
215+
}
216+
}
217+
logCtx.WithFields(fields).Info("HEAD.lock status")
200218
}
201219

202220
var stdout bytes.Buffer
@@ -261,6 +279,7 @@ func RunCommandExt(cmd *exec.Cmd, opts CmdOpts) (string, error) {
261279
if timeoutBehavior.ShouldWait {
262280
select {
263281
case <-done:
282+
logHeadLockStatus("timeout-waited-done")
264283
case <-fatalTimeoutCh:
265284
// upgrades to fatal signal (default SIGKILL) if cmd does not respect the initial signal
266285
if cmd.Process != nil {

0 commit comments

Comments
 (0)