Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions pkg/go/cmd/codebase-memory-mcp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ const (
requestTimeout = 2 * time.Minute
connectTimeout = 15 * time.Second
responseHeaderTimeout = 30 * time.Second
candidateTimeout = 15 * time.Second
maxArchiveMembers = 64

runtimeSetLockName = ".codebase-memory-mcp-runtime.lock"
Expand Down Expand Up @@ -1008,16 +1007,19 @@ func validateWindowsZipMember(raw string) (string, error) {
}

func verifyCandidate(path string) error {
ctx, cancel := context.WithTimeout(context.Background(), candidateTimeout)
defer cancel()
cmd := exec.CommandContext(ctx, path, "--version")
return verifyCandidateWithCommand(path, exec.CommandContext)
}

func verifyCandidateWithCommand(
path string, command func(context.Context, string, ...string) *exec.Cmd,
) error {
// Verification is decided by the child exit; the smoke harness bounds hangs.
ctx := context.Background()
cmd := command(ctx, path, "--version")
cmd.Stdin = nil
cmd.Stdout = io.Discard
cmd.Stderr = io.Discard
if err := cmd.Run(); err != nil {
if ctx.Err() != nil {
return fmt.Errorf("downloaded binary verification timed out: %w", ctx.Err())
}
return fmt.Errorf("downloaded binary failed to run: %w", err)
}
return nil
Expand Down
100 changes: 100 additions & 0 deletions pkg/go/cmd/codebase-memory-mcp/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import (
"archive/zip"
"bytes"
"compress/gzip"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net"
"net/http"
"net/http/httptest"
"os"
Expand Down Expand Up @@ -1388,3 +1390,101 @@ func TestReleaseVersionFromModuleVersion(t *testing.T) {
}
}
}

// Re-executed as a candidate binary; the connection holds it until released.
func TestVerifyCandidateProcess(t *testing.T) {
address := os.Getenv("CBM_TEST_VERIFY_ADDRESS")
if address == "" {
return
}
connection, err := net.Dial("tcp", address)
if err != nil {
os.Exit(2)
}
defer connection.Close()
if _, err := connection.Write([]byte("ready")); err != nil {
os.Exit(3)
}
var release [1]byte
if _, err := io.ReadFull(connection, release[:]); err != nil {
os.Exit(4)
}
os.Exit(int(release[0]))
}

func TestVerifyCandidateWaitsForResult(t *testing.T) {
for _, exitCode := range []byte{0, 7} {
t.Run(fmt.Sprintf("exit_%d", exitCode), func(t *testing.T) {
listener, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
defer listener.Close()
// This bound only aborts a hung test, never decides a successful result.
guard, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
accepted := make(chan net.Conn, 1)
go func() {
connection, err := listener.Accept()
if err == nil {
accepted <- connection
}
}()
done := make(chan error, 1)
go func() {
done <- verifyCandidateWithCommand("candidate", func(ctx context.Context, path string, args ...string) *exec.Cmd {
if _, deadline := ctx.Deadline(); deadline {
t.Error("candidate verification must wait for its result, without a deadline")
}
if path != "candidate" || !reflect.DeepEqual(args, []string{"--version"}) {
t.Errorf("candidate command = %q %q", path, args)
}
command := exec.CommandContext(guard, os.Args[0], "-test.run=^TestVerifyCandidateProcess$")
command.Env = append(os.Environ(), "CBM_TEST_VERIFY_ADDRESS="+listener.Addr().String())
return command
})
}()
var connection net.Conn
select {
case connection = <-accepted:
case err := <-done:
t.Fatalf("verification returned before handshake: %v", err)
case <-guard.Done():
t.Fatal("candidate handshake hung")
}
defer connection.Close()
if err := connection.SetDeadline(time.Now().Add(time.Minute)); err != nil {
t.Fatal(err)
}
ready := make([]byte, len("ready"))
if _, err := io.ReadFull(connection, ready); err != nil || string(ready) != "ready" {
t.Fatalf("candidate handshake = %q, %v", ready, err)
}
select {
case err := <-done:
t.Fatalf("verification returned while candidate held: %v", err)
default:
}
if _, err := connection.Write([]byte{exitCode}); err != nil {
t.Fatal(err)
}
select {
case err := <-done:
if exitCode == 0 && err != nil {
t.Fatal(err)
}
if exitCode != 0 {
var exitError *exec.ExitError
if !errors.As(err, &exitError) || exitError.ExitCode() != int(exitCode) {
t.Fatalf("verification error = %v, want exit %d", err, exitCode)
}
}
case <-guard.Done():
t.Fatal("verification hung after candidate release")
}
})
}
if err := verifyCandidate(filepath.Join(t.TempDir(), "missing")); err == nil {
t.Fatal("verification accepted a missing executable")
}
}
2 changes: 2 additions & 0 deletions scripts/ci/test-package-wrappers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ if ! "${PYTHON[@]}" -c 'import sys; raise SystemExit(0 if sys.version_info >= (3
exit 2
fi

"${PYTHON[@]}" "$ROOT/tests/test_smoke_wrapper_run.py"

echo "=== Package wrapper: Go ==="
(
cd "$ROOT/pkg/go"
Expand Down
37 changes: 37 additions & 0 deletions scripts/smoke-wrapper-run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env python3
"""Run a wrapper smoke command; reserve the outer bound for hang diagnosis."""

import os
import signal
import subprocess
import sys


def run(command, timeout=600):
process = subprocess.Popen(command, start_new_session=os.name != "nt")
try:
return process.wait(timeout=timeout)
except subprocess.TimeoutExpired:
print("HANG go: wrapper smoke did not complete within the outer bound "
"(download, verification or execution); terminating its process tree",
file=sys.stderr, flush=True)
# Kill descendants too: killing only the wrapper leaves its verifier alive.
if os.name == "nt":
subprocess.run(["taskkill", "/PID", str(process.pid), "/T", "/F"],
check=True, timeout=30)
else:
try:
os.killpg(process.pid, signal.SIGKILL)
except ProcessLookupError:
pass
process.wait(timeout=30)
return 124


if __name__ == "__main__":
try:
result = run(sys.argv[1:])
except (OSError, subprocess.SubprocessError) as error:
print(f"wrapper smoke could not run or clean up: {error}", file=sys.stderr)
sys.exit(125)
sys.exit(result if result >= 0 else 128 - result)
14 changes: 12 additions & 2 deletions scripts/smoke-wrappers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,18 @@ smoke_go() {
ran go "$dir/install.log" "$rc"
return
fi
# The first run downloads the binary into the wrapper's cache.
isolated "$dir" "$dir/codebase-memory-mcp$EXE" --version >"$dir/run.log" 2>&1 || rc=$?
local py=""
if command -v python3 >/dev/null 2>&1; then
py=python3
elif command -v python >/dev/null 2>&1; then
py=python
else
missing go "Python 3 (outer hang guard)"
return
fi
# The first run downloads the binary. The outer guard diagnoses hangs;
# the verifier itself waits for the binary's exit, regardless of runner speed.
isolated "$dir" "$py" "$ROOT/scripts/smoke-wrapper-run.py" "$dir/codebase-memory-mcp$EXE" --version >"$dir/run.log" 2>&1 || rc=$?
ran go "$dir/run.log" "$rc"
}

Expand Down
1 change: 1 addition & 0 deletions scripts/test-windows.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ try {
# UI (build target cbm-with-ui); against a non-UI binary it reports a precondition
# (exit 2) and is skipped rather than failed.
$guards = @(
"tests\windows\test_worker_trace.py",
"tests\windows\test_non_ascii_path.py",
"tests\windows\test_non_ascii_cache_dump.py",
"tests\windows\test_daemon_lifecycle.py",
Expand Down
99 changes: 99 additions & 0 deletions tests/test_smoke_wrapper_run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
"""The smoke watchdog reports hangs separately and cleans the command tree."""

import importlib.util
import pathlib
import queue
import subprocess
import sys
import threading
import unittest
from unittest import mock

SPEC = importlib.util.spec_from_file_location(
"smoke_wrapper_run",
pathlib.Path(__file__).resolve().parents[1] / "scripts/smoke-wrapper-run.py",
)
runner = importlib.util.module_from_spec(SPEC)
SPEC.loader.exec_module(runner)


class WrapperWatchdogTest(unittest.TestCase):
def test_preserves_actual_exit(self):
for code in (0, 7):
with self.subTest(code=code):
self.assertEqual(runner.run([sys.executable, "-c", f"raise SystemExit({code})"]), code)

def test_start_failure(self):
with self.assertRaises(FileNotFoundError):
runner.run([str(pathlib.Path(__file__).resolve().parent / "missing-wrapper-2877")])

def test_hang_stops_real_process(self):
# Inject the outer expiry only after the child is ready, without sleeps.
real_popen = subprocess.Popen
child = None

def start(command, **options):
nonlocal child
child = real_popen(command, stdout=subprocess.PIPE, text=True, **options)
ready = queue.Queue()
threading.Thread(target=lambda: ready.put(child.stdout.readline()), daemon=True).start()
self.assertEqual(ready.get(timeout=30), "ready\n")
real_wait = child.wait
expired = False

def wait(timeout):
nonlocal expired
if not expired:
expired = True
raise subprocess.TimeoutExpired(command, timeout)
return real_wait(timeout=timeout)

child.wait = wait
return child

try:
# taskkill uses Popen too; let that separate command run normally.
def spawn(command, **options):
return real_popen(command, **options) if command[0] == "taskkill" else start(command, **options)
with mock.patch.object(runner.subprocess, "Popen", side_effect=spawn), \
mock.patch.object(runner.sys, "stderr"):
self.assertEqual(runner.run([
sys.executable, "-u", "-c",
"import sys, threading; sys.stdout.buffer.write(b'ready\\r\\n'); "
"sys.stdout.buffer.flush(); threading.Event().wait()",
]), 124)
self.assertIsNotNone(child.poll())
finally:
if child is not None:
if child.poll() is None:
child.kill()
child.wait(timeout=30)
child.stdout.close()

def test_hang_kills_tree_and_reaps(self):
for platform in ("posix", "nt"):
with self.subTest(platform=platform), \
mock.patch.object(runner.os, "name", platform), \
mock.patch.object(runner.subprocess, "Popen") as popen, \
mock.patch.object(runner.subprocess, "run") as taskkill, \
mock.patch.object(runner.os, "killpg", create=True) as killpg, \
mock.patch.object(runner.signal, "SIGKILL", 9, create=True), \
mock.patch.object(runner.sys, "stderr") as diagnostic:
process = popen.return_value
process.pid = 123
process.wait.side_effect = [subprocess.TimeoutExpired("candidate", 600), 0]
self.assertEqual(runner.run(["candidate"]), 124)
popen.assert_called_once_with(["candidate"], start_new_session=platform != "nt")
self.assertEqual(process.wait.call_args_list, [mock.call(timeout=600), mock.call(timeout=30)])
if platform == "nt":
taskkill.assert_called_once_with(
["taskkill", "/PID", "123", "/T", "/F"], check=True, timeout=30)
killpg.assert_not_called()
else:
killpg.assert_called_once_with(123, runner.signal.SIGKILL)
taskkill.assert_not_called()
self.assertIn("HANG go", "".join(call.args[0] for call in diagnostic.write.call_args_list))


if __name__ == "__main__":
unittest.main()
Loading
Loading