Skip to content

Self-hosted workflows for coding-agent delivery. Orchestrate CodeBuddy, Codex and Claude Code with scripts and human approval gates. Includes Local workflows for coding assistants.

【架构优化】从 gitIndexLockHeldUnder 中拆出单进程 fd 目录扫描 fdDirHoldsFile (#752)

fx-kurumi8a8637aa222f
diff --git a/internal/runtime/git_index_lock_fd_test.go b/internal/runtime/git_index_lock_fd_test.go
new file mode 100644
index 00000000..d933d31a
--- /dev/null
+++ b/internal/runtime/git_index_lock_fd_test.go
@@ -0,0 +1,65 @@
+package runtime
+
+import (
+ "errors"
+ "os"
+ "path/filepath"
+ "testing"
+)
+
+func TestFdDirHoldsFile(t *testing.T) {
+ root := t.TempDir()
+ lockPath := filepath.Join(root, "index.lock")
+ if err := os.WriteFile(lockPath, nil, 0o600); err != nil {
+ t.Fatal(err)
+ }
+ lock, err := os.Lstat(lockPath)
+ if err != nil {
+ t.Fatal(err)
+ }
+ other := filepath.Join(root, "other")
+ if err := os.WriteFile(other, nil, 0o600); err != nil {
+ t.Fatal(err)
+ }
+ alias := filepath.Join(root, "alias.lock")
+ if err := os.Link(lockPath, alias); err != nil {
+ t.Fatal(err)
+ }
+ // fdDir builds a fake /proc/<pid>/fd directory whose entries are symlinks.
+ fdDir := func(targets ...string) string {
+ dir := filepath.Join(t.TempDir(), "fd")
+ if err := os.Mkdir(dir, 0o755); err != nil {
+ t.Fatal(err)
+ }
+ for i, target := range targets {
+ if err := os.Symlink(target, filepath.Join(dir, string(rune('0'+i)))); err != nil {
+ t.Fatal(err)
+ }
+ }
+ return dir
+ }
+ for name, tc := range map[string]struct {
+ targets []string
+ held bool
+ }{
+ "empty": {nil, false},
+ "pipes-sockets": {[]string{"pipe:[123]", "socket:[456]", "anon_inode:[eventfd]"}, false},
+ "other-file": {[]string{other}, false},
+ "dangling": {[]string{filepath.Join(root, "gone")}, false},
+ "same-path": {[]string{"pipe:[1]", other, lockPath}, true},
+ "same-inode-alt": {[]string{alias}, true},
+ } {
+ t.Run(name, func(t *testing.T) {
+ held, err := fdDirHoldsFile(fdDir(tc.targets...), lockPath, lock)
+ if err != nil || held != tc.held {
+ t.Fatalf("held=%v err=%v, want held=%v", held, err, tc.held)
+ }
+ })
+ }
+ t.Run("unreadable-dir", func(t *testing.T) {
+ held, err := fdDirHoldsFile(filepath.Join(root, "missing", "fd"), lockPath, lock)
+ if held || !errors.Is(err, os.ErrNotExist) {
+ t.Fatalf("held=%v err=%v, want not-exist error", held, err)
+ }
+ })
+}
diff --git a/internal/runtime/git_isolation.go b/internal/runtime/git_isolation.go
index 7c1dd552..6bae9221 100644
--- a/internal/runtime/git_isolation.go
+++ b/internal/runtime/git_isolation.go
@@ -294,27 +294,15 @@ func gitIndexLockHeldUnder(procRoot, lockPath string, self uint32) (inUse, confi
if _, err := strconv.Atoi(entry.Name()); err != nil {
continue
}
- dir := filepath.Join(procRoot, entry.Name(), "fd")
- fds, readErr := os.ReadDir(dir)
+ held, readErr := fdDirHoldsFile(filepath.Join(procRoot, entry.Name(), "fd"), want, lock)
if readErr != nil {
if foreignOwner && !errors.Is(readErr, os.ErrNotExist) && procMayOwnFile(filepath.Join(procRoot, entry.Name()), stat.Uid) {
uncertain = true
}
continue
}
- for _, fd := range fds {
- link := filepath.Join(dir, fd.Name())
- target, linkErr := os.Readlink(link)
- // Sockets, pipes and anonymous inodes are never the lock file.
- if linkErr != nil || !strings.HasPrefix(target, "/") {
- continue
- }
- if target == want {
- return true, true, nil
- }
- if open, statErr := os.Stat(link); statErr == nil && os.SameFile(lock, open) {
- return true, true, nil
- }
+ if held {
+ return true, true, nil
}
}
if uncertain && time.Since(lock.ModTime()) >= gitIndexLockForeignStaleAfter {
@@ -325,6 +313,31 @@ func gitIndexLockHeldUnder(procRoot, lockPath string, self uint32) (inUse, confi
var gitIndexLockForeignStaleAfter = 30 * time.Minute
+// fdDirHoldsFile reports whether any descriptor in one process's fd directory
+// points at the lock file, either by path (want) or by inode (lock). The error
+// is only the failure to read the fd directory itself.
+func fdDirHoldsFile(dir, want string, lock os.FileInfo) (bool, error) {
+ fds, err := os.ReadDir(dir)
+ if err != nil {
+ return false, err
+ }
+ for _, fd := range fds {
+ link := filepath.Join(dir, fd.Name())
+ target, linkErr := os.Readlink(link)
+ // Sockets, pipes and anonymous inodes are never the lock file.
+ if linkErr != nil || !strings.HasPrefix(target, "/") {
+ continue
+ }
+ if target == want {
+ return true, nil
+ }
+ if open, statErr := os.Stat(link); statErr == nil && os.SameFile(lock, open) {
+ return true, nil
+ }
+ }
+ return false, nil
+}
+
// procMayOwnFile reports whether the process may run as uid. Unknown state
// counts as a match, so an unreadable process can only preserve the lock.
func procMayOwnFile(procDir string, uid uint32) bool {