From 50b4e796b7e6442743b85196fd65c60500fdec8e Mon Sep 17 00:00:00 2001 From: w3cj Date: Tue, 3 Nov 2026 11:18:26 -0700 Subject: [PATCH] fix(daemon): a proxy just started whose arguments read as none yet is still waited for On Linux, a process devvm has just started shows empty arguments in /proc//cmdline for a moment: exec lets the parent go on once the new program replaces its memory, and the kernel sets the argument area only once the program has loaded. Proc.Ready took that moment as the proxy having ended, so a start under load failed at once with proxy_start_failed while the proxy ran on, its log and status empty. Ready now counts the process as running while its pid file names it, it is alive, and its arguments are its own or can't be read yet (Proc.starting); an ended one is reaped by Detach, so it stops counting. Tests: TestReadyArgsUnread (fails on the old code). Co-Authored-By: Claude Opus 5.5 --- docs/content/docs/internals/network.md | 2 +- internal/daemon/daemon_test.go | 28 ++++++++++++++++++++++++++ internal/daemon/proc.go | 17 +++++++++++++++- 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/docs/content/docs/internals/network.md b/docs/content/docs/internals/network.md index 68b345c7..cb4eb702 100644 --- a/docs/content/docs/internals/network.md +++ b/docs/content/docs/internals/network.md @@ -93,7 +93,7 @@ The proxy exits when the VM's Lima hostagent does, which it learns from the host A VM that starts again gets a new proxy on the same port. A running VM also gets a new proxy when its list of policies changed, when its CA was replaced, when its proxy is from another devvm binary, when the [CAs in your CA folder](#a-companys-ca) changed, or when the [company proxy](#through-a-company-proxy) settings in devvm's environment differ from the ones the proxy started with. To start a new proxy, devvm stops the old one with `SIGTERM`, and with `SIGKILL` when it's still running two seconds later. The old proxy counts as stopped only once its process is gone, not once its arguments can no longer be read, because an ending process loses its memory before its files, and for that moment its port still looks taken by another program, which would fail the launch with `proxy_port_taken`. `SIGKILL` goes only to a process whose arguments devvm can still read as the proxy's, so a proxy still ending gets none, and neither does another process given its pid since, even one whose arguments devvm can't read. If it still runs two seconds after that, devvm stops with `proxy_stop_failed` and starts nothing, because the old proxy would go on with its old settings. -devvm counts a proxy as started only once the proxy says it's listening on the VM's port and is still running, because a program that took the port first would otherwise get the VM's traffic. The proxy says so in a status file of its own in the VM's run folder (`vms//run/proxy.status`), which devvm names to it when it starts it and removes before each start. The file holds the proxy's pid and the address it listens on, so a status that an earlier proxy left, or that another program wrote, never counts. If the proxy doesn't start, devvm stops and names the proxy's log (`proxy_start_failed`). When a stopped VM's port was taken by another program, devvm chooses a new free port. A running VM's backend holds the port it started with, so when a running VM's proxy is gone and another program took its port, devvm can't start the proxy again (`proxy_port_taken`). devvm then stops the VM (`net_proxy_down_stopped`), and it does the same when the proxy fails to start for any other reason, because a VM with network policy never runs on without its proxy. The next `devvm` starts both. An interrupt during the start of a VM with network policy stops the VM and its proxy too (`interrupted_start`, a cleanup that `Env.start` registers around the backend's `Start`), because a backend can leave the VM running while its start still waits for it: Lima's hostagent keeps running the VM after `limactl start` ends, and devvm records the confined start only once `limactl start` returns. Without the stop, the VM would run with no record of its confined start, so `devvm ls` would show it as `unenforced` and its next launch would offer to restart it. +devvm counts a proxy as started only once the proxy says it's listening on the VM's port and is still running, because a program that took the port first would otherwise get the VM's traffic. The proxy says so in a status file of its own in the VM's run folder (`vms//run/proxy.status`), which devvm names to it when it starts it and removes before each start. The file holds the proxy's pid and the address it listens on, so a status that an earlier proxy left, or that another program wrote, never counts. While devvm waits, the proxy counts as running as long as its pid file still names it, it hasn't ended, and its arguments are the VM's proxy's or can't be read yet. On Linux, a process that has just started shows no arguments for a moment, because the kernel lets devvm go on as the new program replaces devvm's copy and shows the program's arguments only once it has loaded. If the proxy doesn't start, devvm stops and names the proxy's log (`proxy_start_failed`). When a stopped VM's port was taken by another program, devvm chooses a new free port. A running VM's backend holds the port it started with, so when a running VM's proxy is gone and another program took its port, devvm can't start the proxy again (`proxy_port_taken`). devvm then stops the VM (`net_proxy_down_stopped`), and it does the same when the proxy fails to start for any other reason, because a VM with network policy never runs on without its proxy. The next `devvm` starts both. An interrupt during the start of a VM with network policy stops the VM and its proxy too (`interrupted_start`, a cleanup that `Env.start` registers around the backend's `Start`), because a backend can leave the VM running while its start still waits for it: Lima's hostagent keeps running the VM after `limactl start` ends, and devvm records the confined start only once `limactl start` returns. Without the stop, the VM would run with no record of its confined start, so `devvm ls` would show it as `unenforced` and its next launch would offer to restart it. ## Policy files diff --git a/internal/daemon/daemon_test.go b/internal/daemon/daemon_test.go index 4d7c01b6..d3c73a0a 100644 --- a/internal/daemon/daemon_test.go +++ b/internal/daemon/daemon_test.go @@ -412,6 +412,34 @@ func TestReady(t *testing.T) { } } +// A process just started whose arguments can't be read yet (Linux's +// moment between exec letting its parent go on and the program loaded) +// is still waited for; one whose arguments are another program's isn't. +func TestReadyArgsUnread(t *testing.T) { + p, start, ours := proc(t) + status := filepath.Join(t.TempDir(), "v.status") + script := `sleep 0.1; printf '%s\n%s\n' $$ 127.0.0.1:3128 > "$` + StatusEnv + `"; exec sleep 30` + pid, err := p.Start(start, Cmd{Path: "/bin/sh", Args: []string{"-c", script}, Status: status}) + if err != nil { + t.Fatal(err) + } + args, unread := p.Args, 3 + p.Args = func(pid int) ([]string, error) { + if unread > 0 { + unread-- + return nil, errors.New("no arguments yet") + } + return args(pid) + } + if got, ok := p.Ready(status, pid, 20*time.Millisecond, 100); !ok || got != "127.0.0.1:3128" || unread != 0 { + t.Errorf("Ready = %q, %v; %d unread looks left", got, ok, unread) + } + delete(ours, pid) + if _, ok := p.Ready(status, pid, time.Millisecond, 10); ok { + t.Error("ready as another program") + } +} + // Ready writes the status file its start named, with its own pid, and // takes the name out of its environment; without one it does nothing. func TestReadyWrites(t *testing.T) { diff --git a/internal/daemon/proc.go b/internal/daemon/proc.go index 57e24aa4..6142735b 100644 --- a/internal/daemon/proc.go +++ b/internal/daemon/proc.go @@ -125,7 +125,22 @@ func (p Proc) ended(pid int) bool { // Ready waits for the process pid, which Start started with status, to // say it's ready there (WaitReady), while it runs as this one. func (p Proc) Ready(status string, pid int, tick time.Duration, tries int) (string, bool) { - return WaitReady(status, pid, func() bool { return p.runs(pid) }, tick, tries) + return WaitReady(status, pid, func() bool { return p.starting(pid) }, tick, tries) +} + +// starting reports whether pid, which Start just started, still runs as +// this one, as runs does, except that one whose arguments can't be read +// yet still counts: Linux lets the parent go on as exec replaces the +// process's memory, then shows its new arguments only once the program +// is loaded, so for a moment they read as none. One that has ended is +// reaped (Detach), so it no longer counts once it's gone. +func (p Proc) starting(pid int) bool { + cur, ok := readLine(p.PIDFile) + if !ok || cur != pid || !Alive(pid) { + return false + } + args, err := p.Args(pid) + return err != nil || p.Is(args) } // writeLine writes s as a file's one line, mode 600, in a single write: