From b9a9af7ef3942634a1ebd8aea53864fc6fe7530f Mon Sep 17 00:00:00 2001 From: dmmdea Date: Wed, 11 Nov 2026 10:27:44 -0500 Subject: [PATCH 1/2] fix(nodeswap): Windows finder matches a node started through a junction CIM reports the path a process was started through. A llama-swap launched from its junctioned live directory never compared equal to the generation path the swap names, so the post-restart check and the rollback's check both failed against a running node. Match by file identity (os.SameFile) after fetching candidates by name; exact spelling still matches and unreadable rows are unchanged. Co-Authored-By: Claude Opus 5.5 --- .../fix-windows-finder-follows-junctions.md | 6 ++ internal/nodeswap/deps_windows.go | 32 +++++++- internal/nodeswap/finder_windows_test.go | 80 +++++++++++++++++++ 3 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 changes/unreleased/fix-windows-finder-follows-junctions.md create mode 100644 internal/nodeswap/finder_windows_test.go diff --git a/changes/unreleased/fix-windows-finder-follows-junctions.md b/changes/unreleased/fix-windows-finder-follows-junctions.md new file mode 100644 index 000000000..ca4059e46 --- /dev/null +++ b/changes/unreleased/fix-windows-finder-follows-junctions.md @@ -0,0 +1,6 @@ +### Fixed - a Windows swap sees a node started through a junction + +- **Why.** On Windows, llama-swap's live directory is a junction into its generation directory (`/llama-swap` -> `/llama-swap-versions/`). The process finder compared the path CIM reports, which is the path the process was started through (the junction), with the generation path the swap names, by spelling. The two never matched. Measured on a Windows node: the swap restarted llama-swap, and both the post-restart check and the rollback's check reported "no process appeared" against a running node. +- **What it does.** The finder fetches processes by file name and then matches a path by the file it names (`os.SameFile`). It still accepts an exact spelling. A junction or symlink on either side, or a short 8.3 name, now matches. A process whose path the OS will not give is still returned as unreadable, as before. The Linux finder already resolved links. +- **Where.** `internal/nodeswap/deps_windows.go` (`findProcessesByExeWindows`, `sameExecutable`). +- **Check.** One Windows test starts a real process through a junction and asks the finder for both the junction path and the generation path. On the old code it fails ("not found in []"); with the fix it passes. diff --git a/internal/nodeswap/deps_windows.go b/internal/nodeswap/deps_windows.go index f895fc524..37e3a5479 100644 --- a/internal/nodeswap/deps_windows.go +++ b/internal/nodeswap/deps_windows.go @@ -6,6 +6,7 @@ import ( "context" "encoding/json" "fmt" + "os" "path/filepath" "strconv" "strings" @@ -49,6 +50,13 @@ type cimProcRow struct { // elevated in session 0, and an unelevated swap saw no node to stop and no new node to verify, // then rolled back against the running node. Such a row is returned when its Name is the target's // file name, marked Unreadable, so the swap can refuse instead of guessing. +// +// A path is matched by the file it names, not by its spelling. CIM reports the path a process was +// started through, and a node whose live directory is a junction into a generation directory +// (/llama-swap -> /llama-swap-versions/) runs from the junction's path while the +// swap names the generation's. Measured 2026-11-11 on a Windows node: the restarted llama-swap ran, +// and the post-restart check and the rollback's check both reported no process, because the two +// spellings never compared equal. deps_other.go resolves links for the same reason. func findProcessesByExeWindows(exePath string) ([]ProcessInfo, error) { abs, err := filepath.Abs(exePath) if err != nil { @@ -56,7 +64,7 @@ func findProcessesByExeWindows(exePath string) ([]ProcessInfo, error) { } script := fmt.Sprintf( `Get-CimInstance Win32_Process -ErrorAction SilentlyContinue | `+ - `Where-Object { ($_.ExecutablePath -and ($_.ExecutablePath -ieq %s)) -or (-not $_.ExecutablePath -and ($_.Name -ieq %s)) } | `+ + `Where-Object { ($_.ExecutablePath -ieq %s) -or ($_.Name -ieq %s) } | `+ `Select-Object ProcessId,CommandLine,ExecutablePath,Name | ConvertTo-Json -Compress`, psQuote(abs), psQuote(filepath.Base(abs))) raw, err := runPowerShell(context.Background(), 20*time.Second, script) @@ -71,13 +79,35 @@ func findProcessesByExeWindows(exePath string) ([]ProcessInfo, error) { if perr != nil { return nil, fmt.Errorf("parsing CIM process query output: %w (raw: %s)", perr, truncate(string(out), 300)) } + var want os.FileInfo + if fi, serr := os.Stat(abs); serr == nil { + want = fi + } procs := make([]ProcessInfo, 0, len(rows)) for _, r := range rows { + if r.ExecutablePath != "" && !sameExecutable(abs, want, r.ExecutablePath) { + continue + } procs = append(procs, ProcessInfo{PID: r.ProcessId, CommandLine: r.CommandLine, ExecutablePath: r.ExecutablePath, Unreadable: r.ExecutablePath == ""}) } return procs, nil } +// sameExecutable reports whether the process path got names the file at want: the same spelling, or +// the same file reached another way (a junction or symlink on either side, a short 8.3 name). wantInfo +// is want's os.Stat, nil when want does not exist; a path that cannot be read then only matches by +// spelling. +func sameExecutable(want string, wantInfo os.FileInfo, got string) bool { + if strings.EqualFold(filepath.Clean(got), filepath.Clean(want)) { + return true + } + if wantInfo == nil { + return false + } + gi, err := os.Stat(got) + return err == nil && os.SameFile(wantInfo, gi) +} + // parseCimRows handles ConvertTo-Json's shape quirk: a single match returns // a bare object, not a one-element array. func parseCimRows(out []byte) ([]cimProcRow, error) { diff --git a/internal/nodeswap/finder_windows_test.go b/internal/nodeswap/finder_windows_test.go new file mode 100644 index 000000000..998a71844 --- /dev/null +++ b/internal/nodeswap/finder_windows_test.go @@ -0,0 +1,80 @@ +//go:build windows + +package nodeswap + +import ( + "io" + "os" + "os/exec" + "path/filepath" + "testing" + "time" +) + +// TestFinderSeesAProcessStartedThroughAJunction is the 2026-11-11 swap failure on a Windows node: +// llama-swap's live directory is a junction into its generation directory, the restarted process +// ran from the junction's path, and the finder, asked for the generation's path, saw nothing, so +// both the post-restart check and the rollback's check failed against a running node. +func TestFinderSeesAProcessStartedThroughAJunction(t *testing.T) { + if os.Getenv("NODESWAP_JUNCTION_HELPER") == "1" { + time.Sleep(60 * time.Second) + return + } + self, err := os.Executable() + if err != nil { + t.Fatal(err) + } + root := t.TempDir() + gen := filepath.Join(root, "probe-versions", "v1") + if err := os.MkdirAll(gen, 0o755); err != nil { + t.Fatal(err) + } + genExe := filepath.Join(gen, "nodeswap-junction-probe.exe") + copyExe(t, self, genExe) + live := filepath.Join(root, "probe") + if out, err := exec.Command("cmd", "/c", "mklink", "/J", live, gen).CombinedOutput(); err != nil { + t.Fatalf("mklink /J: %v: %s", err, out) + } + cmd := exec.Command(filepath.Join(live, "nodeswap-junction-probe.exe"), "-test.run=^TestFinderSeesAProcessStartedThroughAJunction$") + cmd.Env = append(os.Environ(), "NODESWAP_JUNCTION_HELPER=1") + if err := cmd.Start(); err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = cmd.Process.Kill(); _ = cmd.Wait() }) + + for _, asked := range []string{genExe, filepath.Join(live, "nodeswap-junction-probe.exe")} { + procs, err := findProcessesByExeWindows(asked) + if err != nil { + t.Fatal(err) + } + found := false + for _, p := range procs { + if p.PID == cmd.Process.Pid { + found = true + } + } + if !found { + t.Errorf("asked for %s: pid %d (started through the junction) not found in %+v", asked, cmd.Process.Pid, procs) + } + } +} + +func copyExe(t *testing.T, src, dst string) { + t.Helper() + in, err := os.Open(src) + if err != nil { + t.Fatal(err) + } + defer in.Close() + out, err := os.Create(dst) + if err != nil { + t.Fatal(err) + } + if _, err := io.Copy(out, in); err != nil { + out.Close() + t.Fatal(err) + } + if err := out.Close(); err != nil { + t.Fatal(err) + } +} From e095c11cf83220311f3b2dd4b8727e01aec3eb3e Mon Sep 17 00:00:00 2001 From: dmmdea Date: Wed, 11 Nov 2026 10:45:48 -0500 Subject: [PATCH 2/2] test(nodeswap): start the junction probe hidden (winexec), per the hide lint Co-Authored-By: Claude Opus 5.5 --- internal/nodeswap/finder_windows_test.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/internal/nodeswap/finder_windows_test.go b/internal/nodeswap/finder_windows_test.go index 998a71844..26a1ea777 100644 --- a/internal/nodeswap/finder_windows_test.go +++ b/internal/nodeswap/finder_windows_test.go @@ -5,10 +5,11 @@ package nodeswap import ( "io" "os" - "os/exec" "path/filepath" "testing" "time" + + "github.com/dmmdea/offload-harness/internal/winexec" ) // TestFinderSeesAProcessStartedThroughAJunction is the 2026-11-11 swap failure on a Windows node: @@ -32,10 +33,10 @@ func TestFinderSeesAProcessStartedThroughAJunction(t *testing.T) { genExe := filepath.Join(gen, "nodeswap-junction-probe.exe") copyExe(t, self, genExe) live := filepath.Join(root, "probe") - if out, err := exec.Command("cmd", "/c", "mklink", "/J", live, gen).CombinedOutput(); err != nil { + if out, err := winexec.Command("cmd", "/c", "mklink", "/J", live, gen).CombinedOutput(); err != nil { t.Fatalf("mklink /J: %v: %s", err, out) } - cmd := exec.Command(filepath.Join(live, "nodeswap-junction-probe.exe"), "-test.run=^TestFinderSeesAProcessStartedThroughAJunction$") + cmd := winexec.Command(filepath.Join(live, "nodeswap-junction-probe.exe"), "-test.run=^TestFinderSeesAProcessStartedThroughAJunction$") cmd.Env = append(os.Environ(), "NODESWAP_JUNCTION_HELPER=1") if err := cmd.Start(); err != nil { t.Fatal(err)