Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
fix(daemon): remove temporary packs after interrupted git maintenance (#11093)
Flavien Darchec2bdf2a007f6
diff --git a/server/internal/daemon/gc.go b/server/internal/daemon/gc.goindex 6981f2b5ce..b28bb4c7b0 100644--- a/server/internal/daemon/gc.go+++ b/server/internal/daemon/gc.go@@ -1644,11 +1644,11 @@ func (d *Daemon) pruneWorktreeLocked(ctx context.Context, barePath string) {if ctx.Err() != nil || d.activeTasks.Load() > 0 {return}- before := snapshotRepoMaintenanceLocks(barePath)+ before := snapshotRepoMaintenanceArtifacts(barePath)if out, err := runGitCommandContext(ctx, barePath, step.timeout, step.args...); err != nil {completed = falseif errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) || errors.Is(err, repocache.ErrMaintenancePreempted) {- d.cleanupNewRepoMaintenanceLocks(barePath, before)+ d.cleanupNewRepoMaintenanceArtifacts(barePath, before)}if errors.Is(context.Cause(ctx), repocache.ErrMaintenancePreempted) {d.logger.Info("gc: git maintenance preempted for foreground work",@@ -1686,34 +1686,34 @@ func runGitCommandContext(parent context.Context, barePath string, timeout time.return strings.TrimSpace(string(out)), err}-type repoMaintenanceLockSnapshot map[string]struct{}+type repoMaintenanceArtifactSnapshot map[string]struct{}-// snapshotRepoMaintenanceLocks records only lock paths known to be produced by-// the maintenance commands below. Cleanup later removes a path only if it did-// not exist in this snapshot and the process tree is confirmed gone. Checkout-// waits on the same repo gate, and task dispatch waits for CancelMaintenance's-// barrier, so no agent Git work can create a competing lock before cleanup.-func snapshotRepoMaintenanceLocks(barePath string) repoMaintenanceLockSnapshot {- locks := make(repoMaintenanceLockSnapshot)- for _, path := range repoMaintenanceLockPaths(barePath) {- locks[path] = struct{}{}+// snapshotRepoMaintenanceArtifacts records locks and temporary packs Git+// maintenance can leave behind. Cleanup only removes new paths after the+// process tree has exited. Checkout waits on the same repo gate, and task+// dispatch waits for CancelMaintenance's barrier.+func snapshotRepoMaintenanceArtifacts(barePath string) repoMaintenanceArtifactSnapshot {+ artifacts := make(repoMaintenanceArtifactSnapshot)+ for _, path := range repoMaintenanceArtifactPaths(barePath) {+ artifacts[path] = struct{}{}}- return locks+ return artifacts}-func repoMaintenanceLockPaths(barePath string) []string {- var locks []string+func repoMaintenanceArtifactPaths(barePath string) []string {+ var artifacts []string+ packDir := filepath.Join(barePath, "objects", "pack")for _, name := range []string{"gc.pid", "packed-refs.lock"} {path := filepath.Join(barePath, name)if info, err := os.Lstat(path); err == nil && info.Mode().IsRegular() {- locks = append(locks, path)+ artifacts = append(artifacts, path)}}for _, root := range []string{filepath.Join(barePath, "refs"),filepath.Join(barePath, "logs", "refs"),filepath.Join(barePath, "objects", "info"),- filepath.Join(barePath, "objects", "pack"),+ packDir,} {_ = filepath.WalkDir(root, func(path string, entry os.DirEntry, err error) error {if err != nil {@@ -1725,30 +1725,31 @@ func repoMaintenanceLockPaths(barePath string) []string {}return nil}- if !entry.IsDir() && strings.HasSuffix(entry.Name(), ".lock") {- locks = append(locks, path)+ isTempPack := filepath.Dir(path) == packDir && entry.Type().IsRegular() && strings.HasPrefix(entry.Name(), "tmp_pack_")+ if isTempPack || (!entry.IsDir() && strings.HasSuffix(entry.Name(), ".lock")) {+ artifacts = append(artifacts, path)}return nil})}- return locks+ return artifacts}-func (d *Daemon) cleanupNewRepoMaintenanceLocks(barePath string, before repoMaintenanceLockSnapshot) {- for _, path := range repoMaintenanceLockPaths(barePath) {+func (d *Daemon) cleanupNewRepoMaintenanceArtifacts(barePath string, before repoMaintenanceArtifactSnapshot) {+ for _, path := range repoMaintenanceArtifactPaths(barePath) {if _, existed := before[path]; existed {continue}rel, err := filepath.Rel(barePath, path)if err != nil || rel == "." || strings.HasPrefix(rel, ".."+string(os.PathSeparator)) || filepath.IsAbs(rel) {- d.logger.Warn("gc: refused maintenance lock cleanup outside repo", "repo", barePath, "path", path)+ d.logger.Warn("gc: refused maintenance artifact cleanup outside repo", "repo", barePath, "path", path)continue}if err := os.Remove(path); err != nil && !os.IsNotExist(err) {- d.logger.Warn("gc: maintenance lock cleanup failed", "repo", barePath, "lock", rel, "error", err)+ d.logger.Warn("gc: maintenance artifact cleanup failed", "repo", barePath, "artifact", rel, "error", err)continue}- d.logger.Info("gc: removed lock left by interrupted maintenance", "repo", barePath, "lock", rel)+ d.logger.Info("gc: removed artifact left by interrupted maintenance", "repo", barePath, "artifact", rel)}}diff --git a/server/internal/daemon/gc_test.go b/server/internal/daemon/gc_test.goindex b3863d9106..882cd03d8b 100644--- a/server/internal/daemon/gc_test.go+++ b/server/internal/daemon/gc_test.go@@ -2021,7 +2021,7 @@ func TestCleanupNewRepoMaintenanceLocksPreservesPreexistingFiles(t *testing.T) {if err := os.WriteFile(preexisting, []byte("existing"), 0o644); err != nil {t.Fatal(err)}- before := snapshotRepoMaintenanceLocks(barePath)+ before := snapshotRepoMaintenanceArtifacts(barePath)created := []string{filepath.Join(barePath, "refs", "remotes", "origin", "main.lock"),@@ -2045,7 +2045,7 @@ func TestCleanupNewRepoMaintenanceLocksPreservesPreexistingFiles(t *testing.T) {t.Fatal(err)}- d.cleanupNewRepoMaintenanceLocks(barePath, before)+ d.cleanupNewRepoMaintenanceArtifacts(barePath, before)for _, path := range created {if _, err := os.Stat(path); !os.IsNotExist(err) {t.Errorf("new maintenance lock %s survived, stat error = %v", path, err)@@ -2058,7 +2058,7 @@ func TestCleanupNewRepoMaintenanceLocksPreservesPreexistingFiles(t *testing.T) {}}-func TestPruneWorktreePreemptionCleansLocksBeforeTaskStarts(t *testing.T) {+func TestPruneWorktreePreemptionCleansGitArtifactsBeforeTaskStarts(t *testing.T) {if _, err := os.Stat("/bin/sh"); err != nil {t.Skipf("requires a POSIX shell: %v", err)}@@ -2081,14 +2081,21 @@ func TestPruneWorktreePreemptionCleansLocksBeforeTaskStarts(t *testing.T) {if err := os.MkdirAll(filepath.Dir(lockPath), 0o755); err != nil {t.Fatal(err)}+ packDir := filepath.Join(barePath, "objects", "pack")+ newPack := filepath.Join(packDir, "tmp_pack_new")+ existingPack := filepath.Join(packDir, "tmp_pack_existing")+ if err := os.WriteFile(existingPack, []byte("existing"), 0o644); err != nil {+ t.Fatal(err)+ }startedPath := filepath.Join(t.TempDir(), "maintenance-started")binDir := t.TempDir()fakeGit := filepath.Join(binDir, "git")script := `#!/bin/sh-if [ "$3" = "reflog" ] && [ "$4" = "expire" ]; then+if [ "$3" = "gc" ]; then: > "$2/refs/remotes/origin/main.lock"- : > "$MULTICA_TEST_MAINTENANCE_STARTED"+ : > "$2/objects/pack/tmp_pack_new"trap 'exit 143' TERM INT+ : > "$MULTICA_TEST_MAINTENANCE_STARTED"while :; do sleep 1; donefiexec "$MULTICA_TEST_REAL_GIT" "$@"@@ -2129,6 +2136,12 @@ exec "$MULTICA_TEST_REAL_GIT" "$@"if _, err := os.Stat(lockPath); !os.IsNotExist(err) {t.Fatalf("maintenance lock survived real task preemption, stat error = %v", err)}+ if _, err := os.Stat(newPack); !os.IsNotExist(err) {+ t.Fatalf("new temporary pack survived task preemption, stat error = %v", err)+ }+ if contents, err := os.ReadFile(existingPack); err != nil || string(contents) != "existing" {+ t.Fatalf("pre-existing temporary pack was changed: contents = %q, error = %v", contents, err)+ }if err := cache.Fetch(barePath); err != nil {t.Fatalf("fetch did not recover after preempted maintenance cleanup: %v", err)}