Skip to content

Commit 31eee45

Browse files
authored
Merge pull request #1147 from engalar/fix/windows-local-run-process-helpers
fix: keep `run --local` working on Windows (process liveness + tree teardown)
2 parents 6bd256b + 2a4d614 commit 31eee45

7 files changed

Lines changed: 319 additions & 15 deletions

File tree

‎.github/workflows/push-test.yml‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,42 @@ jobs:
5151
exit 1
5252
fi
5353
54+
# The Windows process helpers (cmd/mxcli/docker/procgroup_windows.go) cannot be
55+
# exercised by the ubuntu job: Signal(0) succeeds on Linux and there is no
56+
# process tree to reap. `mxcli run --local` hung forever on Windows at
57+
# "Starting mxbuild --serve..." because alive() asked Signal(0) (unsupported on
58+
# Windows, so a live mxbuild read as dead) and Stop() killed only the wrapper,
59+
# leaving mxbuild's Deno web-ext worker holding the stdout pipe so cmd.Wait()
60+
# blocked. This job runs the Windows-only regression tests on a real Windows
61+
# runner so neither half can come back.
62+
windows-process-regression:
63+
runs-on: windows-latest
64+
steps:
65+
- uses: actions/checkout@v7
66+
- uses: actions/setup-go@v7
67+
with:
68+
go-version: '1.26.6'
69+
- name: Test the Windows process helpers
70+
shell: bash
71+
# Scoped with -run: the rest of the docker package's tests assert POSIX
72+
# file modes and shell out to `sh`, so the full package does not pass on
73+
# Windows (tracked in #897). The test binary is still COMPILED for this
74+
# platform, so a Windows build break in the package is caught too.
75+
#
76+
# -run can pass vacuously if the tests are renamed or deleted, so assert
77+
# that the expected number actually ran.
78+
run: |
79+
out=$(go test -v -count=1 -run 'TestProcessAlive|TestKillProcessGroup|TestServeServer_AliveTracksProcess|TestLocalRuntime_AliveTracksProcess' ./cmd/mxcli/docker/)
80+
echo "$out"
81+
n=$(printf '%s\n' "$out" | grep -c '^--- PASS: Test' || true)
82+
echo "windows process tests executed: $n"
83+
if [ "$n" -lt 5 ]; then
84+
echo "FAIL: expected at least 5 Windows process tests to run, -run matched $n."
85+
echo " procgroup_windows_test.go must keep its processAlive / tree-kill /"
86+
echo " alive() tests, or the Windows local-boot regression is unguarded."
87+
exit 1
88+
fi
89+
5490
build-and-test:
5591
runs-on: ubuntu-latest
5692
steps:

‎CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
3838

3939
### Fixed
4040

41+
- **`mxcli run --local` hung forever on Windows at `Starting mxbuild --serve...`** — two POSIX assumptions in `cmd/mxcli/docker` combined. `ServeServer.alive()` (and `LocalRuntime.alive()`) asked `os.Process.Signal(syscall.Signal(0))`, which Go implements as `EWINDOWS` ("not supported by windows") for every signal but `Kill` — so a live mxbuild read as dead and `waitReady()` aborted the boot the instant it launched it. And `Stop()` waited on `cmd.Wait()` after a `killProcessGroup` that on Windows only called `p.Kill()`: mxbuild.exe is a wrapper that launches a Deno web-ext worker (`modeler/tools/deno/win-x64/deno.exe`) which inherits the stdout/stderr pipe `exec.Cmd` hands out, so the orphan kept the pipe open and `Wait()` never saw EOF — no error, no exit, just a hang. Windows now has a real liveness check (`OpenProcess(SYNCHRONIZE)` + `WaitForSingleObject`) and a tree kill (`taskkill /F /T`) behind `signalProcessGroup`/`killProcessGroup`; the POSIX implementations are unchanged. The Windows-only tests in `procgroup_windows_test.go` pin both halves — the tree-kill test fails on the pre-fix code, verified as a control — and a `windows-latest` CI job runs them (asserting they actually executed) so neither can come back.
42+
4143
- **`raise error;` on a microflow's main flow passed check and exec, then failed the build** (mendixlabs/mxcli#1030) — with `[error] [CE0710] "The main flow cannot join an error flow or end in an error event."`, one per microflow. Mendix's error event *re-raises the error being handled*, so it is legal only where an error is in scope: inside an `on error { … }` handler. Studio Pro will not draw the connection from the normal flow to an error event; mxcli could, and did. It is now **MDL084**, at error severity, so `exec`'s pre-flight refuses the script with nothing written (`--no-check` still applies it, for reproducing the build failure).
4244

4345
The report's own diagnosis — a trailing End event appended because `RaiseErrorStmt` never set the builder's "ends with return" flag — is not the cause: `isTerminalStmt` has treated it as a terminator all along, and the graph mxcli builds for `raise error;` is exactly the one the report asks for (start event, error event, one sequence flow, no trailing End event, no outgoing flow), pinned by a test. No wiring makes a main-flow error event legal, which is why the fix is a refusal rather than a builder change.

‎cmd/mxcli/docker/localboot.go‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -711,16 +711,18 @@ func (rt *LocalRuntime) Log() string { return rt.log.String() }
711711

712712
// alive reports whether the runtime process is still running.
713713
//
714-
// Signal(0) is only a correct liveness test BECAUSE watchExit reaps: it succeeds
715-
// on an unreaped zombie — measured, proc state `Z`, err nil — and returns
716-
// "process already finished" once Wait has run. Before the reaper existed this
717-
// function reported a runtime that had terminated itself hours earlier as alive
718-
// (mxcli-formula1 FINDINGS §60). Removing watchExit silently breaks this line.
714+
// Signal(0) is only a correct liveness test on POSIX, and even there only
715+
// BECAUSE watchExit reaps: it succeeds on an unreaped zombie — measured, proc
716+
// state `Z`, err nil — and returns "process already finished" once Wait has run.
717+
// Before the reaper existed this function reported a runtime that had terminated
718+
// itself hours earlier as alive (mxcli-formula1 FINDINGS §60). Removing watchExit
719+
// silently breaks that on POSIX. On Windows Signal(0) is not supported at all, so
720+
// processAlive uses WaitForSingleObject instead (see procgroup_windows.go).
719721
func (rt *LocalRuntime) alive() bool {
720722
if rt.cmd == nil || rt.cmd.Process == nil {
721723
return false
722724
}
723-
return rt.cmd.Process.Signal(syscall.Signal(0)) == nil
725+
return processAlive(rt.cmd.Process)
724726
}
725727

726728
// Stop shuts the runtime down gracefully via the admin API, then terminates the

‎cmd/mxcli/docker/mxserve.go‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,12 +385,16 @@ func (s *ServeServer) Build(req BuildRequest) (*BuildResult, error) {
385385
return &res, nil
386386
}
387387

388-
// alive reports whether the serve process is still running (Linux: signal 0).
388+
// alive reports whether the serve process is still running.
389+
//
390+
// Delegates to processAlive: Signal(0) is a correct liveness test on POSIX but
391+
// returns EWINDOWS on Windows, where it made waitReady() treat a just-started
392+
// mxbuild as dead.
389393
func (s *ServeServer) alive() bool {
390394
if s.cmd == nil || s.cmd.Process == nil {
391395
return false
392396
}
393-
return s.cmd.Process.Signal(syscall.Signal(0)) == nil
397+
return processAlive(s.cmd.Process)
394398
}
395399

396400
// Log returns the captured mxbuild --serve output (for diagnostics).

‎cmd/mxcli/docker/procgroup_unix.go‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,13 @@ func signalProcessGroup(p *os.Process, sig syscall.Signal) error {
4646
func killProcessGroup(p *os.Process) error {
4747
return signalProcessGroup(p, syscall.SIGKILL)
4848
}
49+
50+
// processAlive reports whether p is still running. On POSIX, signal 0 succeeds
51+
// for a live process; it also succeeds for an unreaped zombie, which is why the
52+
// callers that care (LocalRuntime) reap via watchExit.
53+
func processAlive(p *os.Process) bool {
54+
if p == nil {
55+
return false
56+
}
57+
return p.Signal(syscall.Signal(0)) == nil
58+
}

‎cmd/mxcli/docker/procgroup_windows.go‎

Lines changed: 66 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,88 @@ package docker
77
import (
88
"os"
99
"os/exec"
10+
"strconv"
1011
"syscall"
1112
)
1213

13-
// procgroup_windows.go provides no-op / single-process fallbacks for the
14-
// process-group helpers. Windows has no POSIX process groups; the warm loop is a
15-
// Linux-devcontainer feature, so this only needs to keep the package compiling
16-
// and preserve the prior single-PID signalling behaviour.
14+
// procgroup_windows.go provides the Windows implementations of the process
15+
// helpers. Windows has no POSIX process groups, so the "group" operations are
16+
// implemented by terminating the whole process tree with taskkill instead.
1717

18-
// setProcessGroup is a no-op on Windows.
18+
// setProcessGroup is a no-op on Windows: there is no PGID to set. Tree teardown
19+
// is handled by signalProcessGroup / killProcessGroup below.
1920
func setProcessGroup(cmd *exec.Cmd) {}
2021

21-
// signalProcessGroup signals the process itself (no group semantics on Windows).
22+
// signalProcessGroup terminates the process tree led by p.
23+
//
24+
// Windows has no signal semantics — os.Process.Signal only supports Kill and
25+
// returns EWINDOWS for everything else — so a "graceful signal" cannot be
26+
// delivered and the process would otherwise linger until the caller's grace
27+
// period expired. Terminating the tree here keeps shutdown prompt.
2228
func signalProcessGroup(p *os.Process, sig syscall.Signal) error {
2329
if p == nil {
2430
return nil
2531
}
32+
if err := killProcessTree(p.Pid); err == nil {
33+
return nil
34+
}
2635
return p.Signal(sig)
2736
}
2837

29-
// killProcessGroup force-terminates the process.
38+
// killProcessGroup force-terminates the process tree led by p.
39+
//
40+
// The tree, not just p: mxbuild.exe is a wrapper that launches a Deno web-ext
41+
// worker (modeler/tools/deno/win-x64/deno.exe) which inherits the stdout/stderr
42+
// pipe this package hands to exec.Cmd. Killing only the wrapper leaves that
43+
// worker alive holding the pipe, so cmd.Wait() never sees EOF and blocks
44+
// forever — which is why `mxcli run --local` used to hang at "Starting mxbuild
45+
// --serve..." with no error. taskkill /T reaps the grandchild too.
46+
//
47+
// The direct p.Kill() is only a fallback for when taskkill is unavailable: once
48+
// the tree kill has terminated p, calling Kill on it again returns "invalid
49+
// argument", which must not be reported as a teardown failure.
3050
func killProcessGroup(p *os.Process) error {
3151
if p == nil {
3252
return nil
3353
}
54+
if err := killProcessTree(p.Pid); err == nil {
55+
return nil
56+
}
3457
return p.Kill()
3558
}
59+
60+
// killProcessTree force-terminates pid and all of its descendants, returning the
61+
// taskkill error (or nil when pid <= 0, which is nothing to do).
62+
func killProcessTree(pid int) error {
63+
if pid <= 0 {
64+
return nil
65+
}
66+
cmd := exec.Command("taskkill", "/F", "/T", "/PID", strconv.Itoa(pid))
67+
cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
68+
return cmd.Run()
69+
}
70+
71+
// processAlive reports whether p is still running.
72+
//
73+
// os.Process.Signal(syscall.Signal(0)) is NOT a liveness test on Windows: Go
74+
// returns EWINDOWS ("not supported by windows") for every signal except Kill, so
75+
// it reports a live process as dead. waitReady() relied on it, concluded mxbuild
76+
// had exited the instant it was launched, and the local loop never got past
77+
// "Starting mxbuild --serve...". WaitForSingleObject on a SYNCHRONIZE handle is
78+
// the real check: it returns WAIT_TIMEOUT while the process runs and
79+
// WAIT_OBJECT_0 once it has terminated (even before it is reaped).
80+
func processAlive(p *os.Process) bool {
81+
if p == nil {
82+
return false
83+
}
84+
h, err := syscall.OpenProcess(syscall.SYNCHRONIZE, false, uint32(p.Pid))
85+
if err != nil {
86+
return false
87+
}
88+
defer syscall.CloseHandle(h)
89+
event, err := syscall.WaitForSingleObject(h, 0)
90+
if err != nil {
91+
return false
92+
}
93+
return event == syscall.WAIT_TIMEOUT
94+
}
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
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

Comments
 (0)