|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +//go:build windows |
| 4 | + |
| 5 | +package docker |
| 6 | + |
| 7 | +import ( |
| 8 | + "os" |
| 9 | + "os/exec" |
| 10 | + "strings" |
| 11 | + "syscall" |
| 12 | + "testing" |
| 13 | + "time" |
| 14 | +) |
| 15 | + |
| 16 | +// These tests exist because `mxcli run --local` hung forever on Windows at |
| 17 | +// "Starting mxbuild --serve...". Two POSIX assumptions were behind it, both in |
| 18 | +// the process helpers this file exercises: |
| 19 | +// |
| 20 | +// 1. alive() used os.Process.Signal(syscall.Signal(0)). Go returns EWINDOWS |
| 21 | +// ("not supported by windows") for every signal except Kill — even for a |
| 22 | +// process that is very much alive — so waitReady() concluded mxbuild had |
| 23 | +// exited the instant it was launched. |
| 24 | +// |
| 25 | +// 2. Stop() waited on cmd.Wait(), and mxbuild.exe is a wrapper that launches a |
| 26 | +// Deno web-ext worker which inherits the stdout/stderr pipe exec.Cmd hands |
| 27 | +// out. Killing only the wrapper (the old killProcessGroup did p.Kill()) |
| 28 | +// left the worker holding the pipe, so Wait() never saw EOF and blocked |
| 29 | +// forever. |
| 30 | +// |
| 31 | +// The unix implementation is covered by procgroup_unix_test.go; this file is the |
| 32 | +// Windows half, and the CI job that runs it is what keeps it from regressing. |
| 33 | + |
| 34 | +// TestWindowsProcessHelper is not a test of its own: it is the re-exec target the |
| 35 | +// tests below use to get a real, controllable process tree on Windows (where |
| 36 | +// there is no `sh -c`). It exits before the testing framework prints anything. |
| 37 | +func TestWindowsProcessHelper(t *testing.T) { |
| 38 | + switch os.Getenv("MXCLI_PROC_HELPER") { |
| 39 | + case "": |
| 40 | + return // normal `go test` run: this is not a test |
| 41 | + case "sleep": |
| 42 | + time.Sleep(60 * time.Second) |
| 43 | + os.Exit(0) |
| 44 | + case "spawn": |
| 45 | + // A grandchild that inherits our stdout/stderr — the inherited pipe is |
| 46 | + // exactly what kept cmd.Wait() blocked in the field. |
| 47 | + gc := exec.Command("cmd", "/c", "ping", "-n", "60", "127.0.0.1") |
| 48 | + gc.Stdout = os.Stdout |
| 49 | + gc.Stderr = os.Stderr |
| 50 | + if err := gc.Start(); err != nil { |
| 51 | + os.Exit(3) |
| 52 | + } |
| 53 | + os.Stdout.WriteString("grandchild-started\n") |
| 54 | + time.Sleep(60 * time.Second) |
| 55 | + os.Exit(0) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +// helperCmd re-execs this test binary in the requested helper mode. |
| 60 | +func helperCmd(t *testing.T, mode string) *exec.Cmd { |
| 61 | + t.Helper() |
| 62 | + cmd := exec.Command(os.Args[0], "-test.run=TestWindowsProcessHelper") |
| 63 | + cmd.Env = append(os.Environ(), "MXCLI_PROC_HELPER="+mode) |
| 64 | + return cmd |
| 65 | +} |
| 66 | + |
| 67 | +// processAlive must report a running process as alive. The CONTROL in the middle |
| 68 | +// is the point: it asserts that the API processAlive replaces really is unusable |
| 69 | +// on Windows, so nobody "simplifies" the workaround back into the bug. |
| 70 | +func TestProcessAlive_ReportsRunningProcess(t *testing.T) { |
| 71 | + cmd := helperCmd(t, "sleep") |
| 72 | + if err := cmd.Start(); err != nil { |
| 73 | + t.Fatalf("start helper: %v", err) |
| 74 | + } |
| 75 | + t.Cleanup(func() { _ = cmd.Process.Kill() }) |
| 76 | + |
| 77 | + if !processAlive(cmd.Process) { |
| 78 | + t.Fatal("processAlive() = false for a running process") |
| 79 | + } |
| 80 | + if err := cmd.Process.Signal(syscall.Signal(0)); err == nil { |
| 81 | + t.Log("note: os.Process.Signal(0) succeeded on Windows; " + |
| 82 | + "processAlive's WaitForSingleObject may no longer be required") |
| 83 | + } |
| 84 | + |
| 85 | + _ = cmd.Process.Kill() |
| 86 | + _ = cmd.Wait() |
| 87 | + if processAlive(cmd.Process) { |
| 88 | + t.Fatal("processAlive() = true for an exited process") |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func TestProcessAlive_FalseWhenNilOrExited(t *testing.T) { |
| 93 | + if processAlive(nil) { |
| 94 | + t.Fatal("processAlive(nil) = true") |
| 95 | + } |
| 96 | + cmd := exec.Command("cmd", "/c", "exit", "0") |
| 97 | + if err := cmd.Start(); err != nil { |
| 98 | + t.Fatalf("start: %v", err) |
| 99 | + } |
| 100 | + _ = cmd.Wait() |
| 101 | + if processAlive(cmd.Process) { |
| 102 | + t.Fatal("processAlive() = true for an exited process") |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +// ServeServer.alive() is the exact call waitReady() makes. Before the fix it |
| 107 | +// returned false for a live mxbuild, which is what aborted the local boot. |
| 108 | +func TestServeServer_AliveTracksProcess(t *testing.T) { |
| 109 | + if (&ServeServer{}).alive() { |
| 110 | + t.Fatal("alive() = true with no process") |
| 111 | + } |
| 112 | + |
| 113 | + cmd := helperCmd(t, "sleep") |
| 114 | + if err := cmd.Start(); err != nil { |
| 115 | + t.Fatalf("start helper: %v", err) |
| 116 | + } |
| 117 | + s := &ServeServer{cmd: cmd, log: &syncBuffer{}} |
| 118 | + |
| 119 | + if !s.alive() { |
| 120 | + t.Fatal("ServeServer.alive() = false for a running process " + |
| 121 | + "(the Windows Signal(0) bug that aborted the local boot)") |
| 122 | + } |
| 123 | + |
| 124 | + _ = cmd.Process.Kill() |
| 125 | + _ = cmd.Wait() |
| 126 | + if s.alive() { |
| 127 | + t.Fatal("ServeServer.alive() = true after the process exited") |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +// LocalRuntime.alive() shares the same helper and the same fix. |
| 132 | +func TestLocalRuntime_AliveTracksProcess(t *testing.T) { |
| 133 | + cmd := helperCmd(t, "sleep") |
| 134 | + if err := cmd.Start(); err != nil { |
| 135 | + t.Fatalf("start helper: %v", err) |
| 136 | + } |
| 137 | + rt := &LocalRuntime{cmd: cmd, log: &syncBuffer{}} |
| 138 | + |
| 139 | + if !rt.alive() { |
| 140 | + t.Fatal("LocalRuntime.alive() = false for a running process") |
| 141 | + } |
| 142 | + |
| 143 | + _ = cmd.Process.Kill() |
| 144 | + _ = cmd.Wait() |
| 145 | + if rt.alive() { |
| 146 | + t.Fatal("LocalRuntime.alive() = true after the process exited") |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +// TestKillProcessGroup_ReapsGrandchildAndUnblocksWait is the regression test for |
| 151 | +// the hang. The helper spawns a grandchild that inherits the stdout pipe; the old |
| 152 | +// single-PID kill left it alive, so cmd.Wait() blocked forever (there was no |
| 153 | +// error, no exit — just a hung `mxcli run --local`). A tree kill closes the pipe |
| 154 | +// and Wait() returns. |
| 155 | +// |
| 156 | +// It would have failed on the pre-fix code: p.Kill() terminates only the helper, |
| 157 | +// the grandchild keeps the write end open, and Wait() never returns. |
| 158 | +func TestKillProcessGroup_ReapsGrandchildAndUnblocksWait(t *testing.T) { |
| 159 | + var log syncBuffer |
| 160 | + cmd := helperCmd(t, "spawn") |
| 161 | + cmd.Stdout = &log |
| 162 | + cmd.Stderr = &log |
| 163 | + setProcessGroup(cmd) |
| 164 | + if err := cmd.Start(); err != nil { |
| 165 | + t.Fatalf("start helper: %v", err) |
| 166 | + } |
| 167 | + t.Cleanup(func() { _ = killProcessGroup(cmd.Process) }) |
| 168 | + |
| 169 | + // Wait for the grandchild to be running and holding the inherited pipe. |
| 170 | + deadline := time.Now().Add(15 * time.Second) |
| 171 | + for !strings.Contains(log.String(), "grandchild-started") { |
| 172 | + if time.Now().After(deadline) { |
| 173 | + t.Fatalf("helper never reported its grandchild; output so far: %q", log.String()) |
| 174 | + } |
| 175 | + time.Sleep(50 * time.Millisecond) |
| 176 | + } |
| 177 | + |
| 178 | + done := make(chan error, 1) |
| 179 | + go func() { done <- cmd.Wait() }() |
| 180 | + |
| 181 | + if err := killProcessGroup(cmd.Process); err != nil { |
| 182 | + t.Fatalf("killProcessGroup: %v", err) |
| 183 | + } |
| 184 | + |
| 185 | + select { |
| 186 | + case <-done: |
| 187 | + case <-time.After(20 * time.Second): |
| 188 | + t.Fatal("cmd.Wait() did not return after killProcessGroup: a surviving " + |
| 189 | + "grandchild still holds the stdout pipe (the old single-PID kill hung here)") |
| 190 | + } |
| 191 | +} |
0 commit comments