Files
magpie/internal/proc/reach.go
T
yetone f03ec97b8f proc: a PATH folder that doesn't answer is left out of every look (ifccod7391564 on X)
Started with `open -a`, magpie ran but took over 60s to write
"started", while the same binary run from a shell started in 1s. The
reporter's trace: startUp → agent.RenameLegacy → onMachine →
vscodePortableData → binAt("code-insiders"). Without Insiders,
exec.LookPath walks all of PATH, and a folder on a mount that no longer
answers holds open()/stat() until it does, before applog.Start.

The block isn't binAt's alone: startUp builds every agent through
local() in RenameLegacy and again in moveEfforts (Cursor's and
Antigravity's), each asking for its command through binAt/proc, all
walking the same PATH, and lookStamp stats every PATH folder first. So
the bound is where all of them pass, in proc (reach.go): each PATH
folder is asked (a stat and an open) in a goroutine of its own, all at
once, and one that hasn't answered in 1s is left out of the stamp and of
the look until its probe returns; a probe still out isn't started again,
so later looks skip the folder at once. With such a folder, the command
is looked for in the others, each as a path (exec.LookPath on dir/name).
An answer is trusted for a minute. findMise and FindShell's last resort
used exec.LookPath directly and now go through Look.

Test: TestHungPathFolderIsLeftOut puts a folder whose probe never
returns before the folder with the tool: three looks find the tool, PATH
is never walked whole, the folder is probed once; once it answers, PATH
is walked as before.

Run: go test ./internal/proc ./internal/claudetui on mote ok; go vet for
darwin, linux and windows ok; the proc look tests on the Windows box
(win-run) ok.
Broken by hand: with lookPath not asking hungDirs, the test is red
("PATH was walked 3 times", wsl-run).
Not tried: a real dead network mount on PATH; the test fakes it at the
probe. Direct exec.LookPath calls outside proc (daemon, library, update)
aren't on the startup path and are unchanged.
2026-11-10 18:33:35 +08:00

177 lines
4.0 KiB
Go

package proc
import (
"os"
"os/exec"
"path/filepath"
"sync"
"time"
)
// A folder on PATH on a mount that no longer answers (a network share
// gone away, an autofs path whose server is down) holds every stat and
// open in it until the mount comes back, which can be never. Looking a
// command up walks PATH in order, so a command not found before such a
// folder blocked whoever asked: magpie's start-up asks for every agent's
// command (agent.RenameLegacy → vscodePortableData → code-insiders), and
// `open -a` took over a minute to start while the same binary run from a
// shell, with another PATH, took one second (ifccod7391564 on X). A PATH
// folder is now asked in a goroutine of its own first, and one that
// hasn't answered in reachWait is left out of every look until it does.
// reachWait is how long a PATH folder has to answer; reachKeep how long
// its answer is trusted before it is asked again.
var (
reachWait = time.Second
reachKeep = time.Minute
)
// probeDir is the asking: a stat and an open of the folder, as a lookup
// does. Whatever either answers, the folder answered. A var for the tests.
var probeDir = func(dir string) {
if st, err := os.Stat(dir); err == nil && st.IsDir() {
if f, err := os.Open(dir); err == nil {
f.Close()
}
}
}
var reach struct {
sync.Mutex
m map[string]*reachProbe
}
type reachProbe struct {
done chan struct{}
deadline time.Time // when a probe not done counts as hung
at time.Time // when it answered, set before done closes
}
// answers reports whether dir answers within reachWait. A probe still out
// past its deadline isn't started again: every look leaves the folder out
// at once until that probe returns.
func answers(dir string) bool {
if dir == "" {
return true
}
now := time.Now()
reach.Lock()
p := reach.m[dir]
if p != nil {
select {
case <-p.done:
if now.Sub(p.at) < reachKeep {
reach.Unlock()
return true
}
p = nil // stale: asked again below
default:
}
}
if p == nil {
p = &reachProbe{done: make(chan struct{}), deadline: now.Add(reachWait)}
if reach.m == nil {
reach.m = map[string]*reachProbe{}
}
reach.m[dir] = p
go func(p *reachProbe) {
probeDir(dir)
p.at = time.Now()
close(p.done)
}(p)
}
reach.Unlock()
wait := time.Until(p.deadline)
if wait <= 0 {
select {
case <-p.done:
return true
default:
return false
}
}
t := time.NewTimer(wait)
defer t.Stop()
select {
case <-p.done:
return true
case <-t.C:
return false
}
}
// hungDirs are the folders of dirs that don't answer; nil when every one
// does. The folders are asked at once, so several gone mounts cost one
// reachWait, not one each.
func hungDirs(dirs []string) map[string]bool {
if allAnswered(dirs) {
return nil
}
var (
mu sync.Mutex
hung map[string]bool
wg sync.WaitGroup
)
for _, d := range dirs {
if d == "" {
continue
}
wg.Add(1)
go func(d string) {
defer wg.Done()
if !answers(d) {
mu.Lock()
if hung == nil {
hung = map[string]bool{}
}
hung[d] = true
mu.Unlock()
}
}(d)
}
wg.Wait()
return hung
}
// allAnswered reports whether every folder of dirs answered within
// reachKeep, so a look asks none of them (no goroutine each).
func allAnswered(dirs []string) bool {
now := time.Now()
reach.Lock()
defer reach.Unlock()
for _, d := range dirs {
if d == "" {
continue
}
p := reach.m[d]
if p == nil {
return false
}
select {
case <-p.done:
if now.Sub(p.at) >= reachKeep {
return false
}
default:
return false
}
}
return true
}
// lookIn is exec.LookPath over dirs alone, in their order: the command's
// name in each folder asked as a path, as exec.LookPath does along PATH.
// The current folder is left out, as exec.LookPath refuses a command
// found there (exec.ErrDot).
func lookIn(name string, dirs []string) (string, error) {
for _, d := range dirs {
if d == "" || d == "." {
continue
}
if p, err := execLookPath(filepath.Join(d, name)); err == nil {
return p, nil
}
}
return "", &exec.Error{Name: name, Err: exec.ErrNotFound}
}