Files
w3cjandClaude Opus 5.5 ae77c7bc45 fix(daemon): a stopped process counts as stopped once it's gone, not once its arguments go
A proxy restarted on a running VM (its company proxy settings changed,
say) was stopped with SIGTERM, and Proc.Stop took it as stopped as soon
as its arguments could no longer be read. An ending process loses its
memory before its files: on macOS kern.procargs2 fails some milliseconds
before its listening socket closes (every time, in a test of 30 stops),
and on Linux /proc/PID/cmdline reads empty. When Stop looked in that
window, Prepare found the VM's proxy port still taken, failed the launch
with proxy_port_taken and stopped the VM (Unproxied): e2e's company
proxy checks 15 and 17 failed in turn, and the checks after them found
the VM stopped.

- daemon.Proc.Stop waits until no process has the pid, or another
  process does (Proc.ended); one whose arguments can't be read is still
  ending.
- platform.ProcessArgs on Linux returns an error for an empty cmdline
  (an ending or unreaped process), as macOS does, never [""].
- Tests: TestProcStopWaitsForTheEnd (fails before the fix: Stop returned
  while the process still ran) and TestProcessArgsEnded.
- e2e: check prints a failed check's "failed: " lines first, then its
  last lines (5, or check_lines); checks 15 and 17 are functions
  (corp_through, corp_direct) whose steps each name themselves, then
  corp_why shows the VM's status, its proxy, the launch's output, the
  proxy's log and events, and the company proxy's log and events.
- docs: internals/network.md says when the old proxy counts as stopped.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-02 22:46:56 -07:00

62 lines
1.9 KiB
Go

//go:build darwin || linux
package platform
import (
"os"
"os/exec"
"slices"
"testing"
"time"
)
// A process's arguments, as it was started with them.
func TestProcessArgs(t *testing.T) {
got, err := ProcessArgs(os.Getpid())
if err != nil {
t.Fatal(err)
}
if !slices.Equal(got[1:], os.Args[1:]) {
t.Errorf("ProcessArgs = %q, want %q", got, os.Args)
}
// sh ignores its extra arguments (GNU sleep would exit on them), and
// stays itself while sleep isn't its last command.
cmd := exec.CommandContext(t.Context(), "sh", "-c", "sleep 30; :", "sh", "a b", "")
if err := cmd.Start(); err != nil {
t.Fatal(err)
}
defer func() { _ = cmd.Process.Kill(); _ = cmd.Wait() }()
// Right after Start, the child may not have exec'd sleep yet: Linux
// shows the forked Go process's arguments until it does.
want := []string{"sh", "-c", "sleep 30; :", "sh", "a b", ""}
for deadline := time.Now().Add(5 * time.Second); time.Now().Before(deadline); time.Sleep(10 * time.Millisecond) {
if got, err = ProcessArgs(cmd.Process.Pid); err == nil && slices.Equal(got, want) {
return
}
}
t.Errorf("ProcessArgs = %q, %v", got, err)
}
// A process that has ended but isn't reaped yet (a zombie) has no
// arguments to read, as one that's ending has none: an error, never an
// empty list taken for another process's.
func TestProcessArgsEnded(t *testing.T) {
cmd := exec.CommandContext(t.Context(), "true")
if err := cmd.Start(); err != nil {
t.Fatal(err)
}
defer func() { _ = cmd.Wait() }()
pid := cmd.Process.Pid
// It's a zombie once its arguments are no longer true's.
var got []string
var err error
for deadline := time.Now().Add(5 * time.Second); time.Now().Before(deadline); time.Sleep(10 * time.Millisecond) {
if got, err = ProcessArgs(pid); err != nil || !slices.Equal(got, []string{"true"}) && !slices.Equal(got, os.Args) {
break
}
}
if err == nil {
t.Errorf("ProcessArgs of an ended process = %q", got)
}
}