Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions changes/unreleased/fix-windows-finder-follows-junctions.md
Original file line number Diff line number Diff line change
@@ -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 (`<home>/llama-swap` -> `<home>/llama-swap-versions/<id>`). 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.
32 changes: 31 additions & 1 deletion internal/nodeswap/deps_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
Expand Down Expand Up @@ -49,14 +50,21 @@ 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
// (<home>/llama-swap -> <home>/llama-swap-versions/<id>) 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 {
abs = exePath
}
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)
Expand All @@ -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) {
Expand Down
81 changes: 81 additions & 0 deletions internal/nodeswap/finder_windows_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
//go:build windows

package nodeswap

import (
"io"
"os"
"path/filepath"
"testing"
"time"

"github.com/dmmdea/offload-harness/internal/winexec"
)

// 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 := winexec.Command("cmd", "/c", "mklink", "/J", live, gen).CombinedOutput(); err != nil {
t.Fatalf("mklink /J: %v: %s", err, out)
}
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)
}
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)
}
}
Loading