Files
w3cjandClaude Opus 5.5 54cce97049 fix(ports): a stopped VM no longer keeps its listed ports from other VMs
Only a running VM holds a host port. Each VM's forwarder records the
ports it forwards, and those it tried and couldn't take
(run/ports.forwarded: its pid, then each list on a line; an empty one
when it starts, and devvm removes it under ports.lock before each
start). A forwarder reserves another VM's ports only while that VM has a
claim: its forwarder runs, or devvm holds its ports.lock to start one
again (the restart window). It never takes the ports such a VM forwards
now, nor those it lists unless this VM lists them too. A stopped or
crashed VM holds nothing. A reserved port the VM listens on waits and is
forwarded once free, so a VM gets its port back within about 20 s of the
other VM stopping. The host decides when a port is given up: every
forwarder closes a forward whose port is now reserved every 10 s on its
own clock (Forwarder.Recheck), so a VM that holds its session open and
stops reporting can't keep another VM's listed port, and closing a
forward closes the connections it accepted, so a browser's keep-alive
connection never reaches the old VM afterwards. Two VMs starting at once
are settled by the host's bind. The ports devvm chooses (ssh, proxy,
desktop) still avoid every VM's listed ports, running or not.

- ports: Used(d) for the allocator; Reserved, Holders and Live for the
  forwarders
- vmssh: Forwarder.Recheck, Note and release; a forward tracks and
  closes its connections; Forwards.Reserved, Held, Taken, live,
  recorded and record; a start keeps a port another VM holds in -only
  and warns ports_held for any holder, listed or every-port; Main takes
  paths.Dirs
- devvm ports warns ports_held and, for a running VM's listed port its
  forwarder recorded as a program's, ports_taken, reading the
  forwarder's record and binding nothing; --json adds held_by
- backend.CountPorts; the stub's ssh master reports again when
  guest-ports changes
- tests: TestReserved, TestForwarderWaits, TestForwarderGivesUpSilently,
  TestForwarderClosesConnections, TestForwardsHeld (restart window,
  every-port holder, no record, Taken from the record alone, another
  process's or no record), TestForwardsRecordFails, TestForwardsStart
  (old record removed), ports_stopped.txtar (CJ's case end to end, and
  an every-port VM leaving a running VM's listed port alone),
  layout.txtar
- docs: ports-size's Two VMs and one port (the browser origin) and its
  JSON, internals' Ports other VMs forward, design.md's ports note and
  the listed ports' threat check; CHANGELOG Fixed

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-13 00:20:43 -07:00

38 lines
1.3 KiB
Go

package vmssh
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/devvmhq/devvm/internal/paths"
)
// A forwarder needs the VM's lifeline, naming a process that runs as it
// starts (daemon.Lifeline): without one, or with one whose process is
// gone, it doesn't run. Nor does one without how it reaches the VM.
func TestForwarderMain(t *testing.T) {
d := paths.Dirs{Data: t.TempDir(), Config: t.TempDir()}
base := []string{"-vm", "v", "-user", "u", "-key", "/k", "-known-hosts", "/kh", "-control", "/c", "-port", "1"}
if err := Main(t.Context(), base, nil, d, &strings.Builder{}); err == nil {
t.Error("no lifeline: ran")
}
gone := filepath.Join(t.TempDir(), "ha.pid")
if err := Main(t.Context(), append(base, "-lifeline", gone), nil, d, &strings.Builder{}); err == nil {
t.Error("an empty lifeline: ran")
}
if err := os.WriteFile(gone, []byte("999999999\n"), 0o600); err != nil {
t.Fatal(err)
}
if err := Main(t.Context(), append(base, "-lifeline", gone), nil, d, &strings.Builder{}); err == nil {
t.Error("a lifeline whose process is gone: ran")
}
if err := Main(t.Context(), []string{"-vm", "v", "-lifeline", gone}, nil, d, &strings.Builder{}); err == nil {
t.Error("no port, user or key: ran")
}
if err := Main(t.Context(), []string{"-no-such-flag"}, nil, d, &strings.Builder{}); err == nil {
t.Error("an unknown flag: ran")
}
}