77# Strategy: launch the binary under a wrapper "parent" process (stdin kept open
88# via a FIFO so the server doesn't see EOF), record the child's PID, then kill
99# the wrapper. The watchdog should notice the changed ppid and exit within a
10- # few seconds. Skipped on Windows-like shells (the watchdog is POSIX-only).
10+ # few seconds. On Windows (MSYS2 shells) the same tree applies: the wrapper is
11+ # an MSYS bash process, the child a native binary whose parent handle the
12+ # watchdog waits on; killing the wrapper terminates that parent process (#914).
1113set -euo pipefail
1214
1315ROOT=" $( cd " $( dirname " ${BASH_SOURCE[0]} " ) /.." && pwd) "
1416BINARY=" ${CBM_TEST_BINARY:- ${ROOT} / build/ c/ codebase-memory-mcp} "
1517
18+ windows_mode=0
1619case " $( uname -s) " in
1720 MINGW* |MSYS* |CYGWIN* )
18- echo " skipping parent watchdog test on Windows"
19- exit 0
21+ windows_mode=1
2022 ;;
2123esac
2224
@@ -30,33 +32,91 @@ source "${ROOT}/scripts/test-runtime.sh"
3032cbm_test_runtime_init
3133tmpdir=" ${CBM_TEST_RUNTIME_ROOT} "
3234wrapper_pid=" "
35+ writer_pid=" "
36+
37+ kill_hard () {
38+ # MSYS signal delivery does not reliably terminate the Windows process
39+ # behind an MSYS pid (observed: a wrapper bash kept running after kill -9,
40+ # so a child's real parent never actually died). Force-kill through
41+ # TerminateProcess on the Windows pid as well. The winpid must be resolved
42+ # BEFORE the msys kill: kill -9 removes the pid from the MSYS process table
43+ # immediately, and the mapping would be lost. `ps -W` exists only on MSYS —
44+ # under `set -o pipefail` a failing ps would abort this helper on POSIX —
45+ # so the pipeline is guarded and simply yields an empty winpid elsewhere.
46+ local pid=" $1 " winpid
47+ winpid=" $( ps -W 2> /dev/null | awk -v m=" ${pid} " ' $1==m {print $4; exit}' || true) "
48+ kill -9 " ${pid} " 2> /dev/null || true
49+ [[ -n " ${winpid} " ]] && taskkill //F //PID " ${winpid} " > /dev/null 2>&1 || true
50+ }
51+
3352cleanup () {
3453 if [[ -s " ${tmpdir} /child.pid" ]]; then
3554 local child_pid
3655 child_pid=" $( cat " ${tmpdir} /child.pid" 2> /dev/null || true) "
37- [[ -n " ${child_pid} " ]] && kill " ${child_pid} " 2> /dev/null || true
56+ [[ -n " ${child_pid} " ]] && kill_hard " ${child_pid} " || true
57+ fi
58+ # On Windows the pipe writer is an orphaned helper that outlives the wrapper
59+ # by design (it holds stdin open); only the test knows its PID.
60+ if [[ -n " ${writer_pid} " ]]; then
61+ kill_hard " ${writer_pid} " || true
3862 fi
39- [[ -n " ${wrapper_pid} " ]] && kill " ${wrapper_pid} " 2> /dev/null || true
63+ [[ -n " ${wrapper_pid} " ]] && kill_hard " ${wrapper_pid} " || true
4064 cbm_test_runtime_cleanup " ${BINARY} "
4165}
4266trap cleanup EXIT
4367
44- # Wrapper "parent": opens the FIFO read-write so it stays open, launches the
45- # MCP server with that FIFO as stdin, records the child PID, then waits.
46- cat > " ${tmpdir} /wrapper.sh" << 'SH '
68+ if (( windows_mode )) ; then
69+ # MSYS FIFOs are not readable by native Windows binaries (the server blocks
70+ # forever), so the stdin-holder is an anonymous PIPE instead: a writer helper
71+ # forwards the initialize request once the test drops it into a file, then
72+ # holds the pipe's write end open forever. Killing the wrapper orphans the
73+ # writer too, so stdin never sees EOF — the child's exit can only come from
74+ # the parent-death watchdog.
75+ cat > " ${tmpdir} /wrapper.sh" << 'SH '
76+ #!/usr/bin/env bash
77+ set -euo pipefail
78+ echo $$ >"${TMPDIR_PATH}/writer.pid"
79+ (
80+ for _ in {1..300}; do
81+ if [[ -s "${REQ_FILE}" ]]; then
82+ cat "${REQ_FILE}"
83+ exec sleep 3600
84+ fi
85+ sleep 0.1
86+ done
87+ ) | "${CBM_BINARY}" >"${TMPDIR_PATH}/child.out" 2>"${TMPDIR_PATH}/child.err" &
88+ echo "$!" >"${TMPDIR_PATH}/child.pid"
89+ wait
90+ SH
91+ chmod +x " ${tmpdir} /wrapper.sh"
92+ : > " ${tmpdir} /request.json"
93+
94+ CBM_BINARY=" ${BINARY} " REQ_FILE=" ${tmpdir} /request.json" TMPDIR_PATH=" ${tmpdir} " \
95+ " ${tmpdir} /wrapper.sh" &
96+ wrapper_pid=$!
97+ for _ in {1..50}; do
98+ [[ -s " ${tmpdir} /writer.pid" ]] && break
99+ sleep 0.1
100+ done
101+ [[ -s " ${tmpdir} /writer.pid" ]] && writer_pid=" $( cat " ${tmpdir} /writer.pid" ) "
102+ else
103+ # Wrapper "parent": opens the FIFO read-write so it stays open, launches the
104+ # MCP server with that FIFO as stdin, records the child PID, then waits.
105+ cat > " ${tmpdir} /wrapper.sh" << 'SH '
47106#!/usr/bin/env bash
48107set -euo pipefail
49108exec 3<>"${FIFO}"
50109"${CBM_BINARY}" <&3 >"${TMPDIR_PATH}/child.out" 2>"${TMPDIR_PATH}/child.err" &
51110echo "$!" >"${TMPDIR_PATH}/child.pid"
52111wait
53112SH
54- chmod +x " ${tmpdir} /wrapper.sh"
55- mkfifo " ${tmpdir} /stdin"
113+ chmod +x " ${tmpdir} /wrapper.sh"
114+ mkfifo " ${tmpdir} /stdin"
56115
57- CBM_BINARY=" ${BINARY} " FIFO=" ${tmpdir} /stdin" TMPDIR_PATH=" ${tmpdir} " \
58- " ${tmpdir} /wrapper.sh" &
59- wrapper_pid=$!
116+ CBM_BINARY=" ${BINARY} " FIFO=" ${tmpdir} /stdin" TMPDIR_PATH=" ${tmpdir} " \
117+ " ${tmpdir} /wrapper.sh" &
118+ wrapper_pid=$!
119+ fi
60120
61121# Wait for the child PID file to appear.
62122for _ in {1..50}; do
80140# the frontend reached its stdio loop after installing the parent watchdog.
81141# The old mem.init log sync point belonged to the pre-daemon architecture: the
82142# shared daemon now owns memory initialization, so a frontend need not emit it.
143+ if (( windows_mode )) ; then
144+ request_target=" ${tmpdir} /request.json"
145+ else
146+ request_target=" ${tmpdir} /stdin"
147+ fi
83148printf ' %s\n' \
84149 ' {"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"parent-watchdog-test","version":"1.0"}}}' \
85- > " ${tmpdir} /stdin "
150+ > " ${request_target} "
86151for _ in {1..150}; do
87152 if [[ -s " ${tmpdir} /child.out" ]] &&
88153 grep -Eq ' "id"[[:space:]]*:[[:space:]]*1' " ${tmpdir} /child.out" ; then
@@ -98,8 +163,35 @@ if ! grep -Eq '"id"[[:space:]]*:[[:space:]]*1' "${tmpdir}/child.out" 2>/dev/null
98163fi
99164
100165# Kill the wrapper parent: the orphaned child must now self-exit.
101- kill -9 " ${wrapper_pid} "
102- wait " ${wrapper_pid} " 2> /dev/null || true
166+ if (( windows_mode )) ; then
167+ # Two Windows-specific traps make "kill -9 $wrapper_pid" wrong here:
168+ # 1. MSYS kill -9 does not reliably terminate the Windows process behind
169+ # an MSYS pid (a wrapper bash keeps running), and
170+ # 2. the wrapper's background PIPELINE puts an intermediate subshell bash
171+ # between wrapper and server, and the watchdog watches the
172+ # Windows-physical parent (Toolhelp ParentProcessId) — that subshell.
173+ # Resolve the child's actual Windows parent and TerminateProcess exactly
174+ # that one, mirroring a force-killed MCP client. The wrapper is NOT waited
175+ # on yet: its wait covers the whole pipeline job, and the stdin-holding
176+ # writer must survive until the child's exit is observed, or stdin would
177+ # EOF and the test could pass without the watchdog doing anything.
178+ child_winpid=" $( ps -W 2> /dev/null | awk -v m=" ${child_pid} " ' $1==m {print $4; exit}' ) "
179+ if [[ -z " ${child_winpid} " ]]; then
180+ echo " child windows pid not found for msys pid ${child_pid} " >&2
181+ exit 3
182+ fi
183+ parent_winpid=" $( powershell.exe -NoProfile -Command \
184+ " (Get-CimInstance Win32_Process -Filter \" ProcessId=${child_winpid} \" ).ParentProcessId" \
185+ 2> /dev/null | tr -d ' [:space:]' ) "
186+ if [[ -z " ${parent_winpid} " || ! " ${parent_winpid} " =~ ^[0-9]+$ ]]; then
187+ echo " could not resolve the child's windows parent pid" >&2
188+ exit 3
189+ fi
190+ taskkill //F //PID " ${parent_winpid} " > /dev/null 2>&1 || true
191+ else
192+ kill -9 " ${wrapper_pid} "
193+ wait " ${wrapper_pid} " 2> /dev/null || true
194+ fi
103195
104196deadline=$(( SECONDS + 15 ))
105197while (( SECONDS < deadline )) ; do
@@ -109,10 +201,14 @@ while (( SECONDS < deadline )); do
109201 fi
110202 # A zombie no longer holds stdin or runs the MCP loop; kill -0 still reports
111203 # it until launchd/test parent reaps it, so treat that as a successful exit.
112- child_state=" $( ps -p " ${child_pid} " -o stat= 2> /dev/null | tr -d ' [:space:]' || true) "
113- if [[ " ${child_state} " == Z* ]]; then
114- echo " ok: child ${child_pid} exited after parent death (zombie awaiting reap)"
115- exit 0
204+ # Windows has no zombie state — an exited process simply disappears from
205+ # kill -0 — so this probe is POSIX-only.
206+ if [[ " ${windows_mode} " -eq 0 ]]; then
207+ child_state=" $( ps -p " ${child_pid} " -o stat= 2> /dev/null | tr -d ' [:space:]' || true) "
208+ if [[ " ${child_state} " == Z* ]]; then
209+ echo " ok: child ${child_pid} exited after parent death (zombie awaiting reap)"
210+ exit 0
211+ fi
116212 fi
117213 sleep 0.2
118214done
0 commit comments