|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +issue832_rss.py -- direct RSS reproduction for #832 (NON-GATING, manual repro tier). |
| 4 | +
|
| 5 | +WHY THIS IS NOT A C UNIT TEST |
| 6 | +----------------------------- |
| 7 | +The RSS ratchet is a property of mimalloc v3's abandoned-page handling |
| 8 | +(page_reclaim_on_free=0): pages a worker THREAD abandons at exit are not |
| 9 | +reclaimed when the main thread later frees their blocks. That only manifests in |
| 10 | +the PROD binary, which links mimalloc as the global allocator (Makefile.cbm: |
| 11 | +MI_OVERRIDE=1). The C test-runner and the C repro-runner are built CRT+ASan |
| 12 | +(MI_OVERRIDE=0), so mimalloc is inert there and cbm_mem_rss() falls back to |
| 13 | +os_rss() -- a C test would be VACUOUS. Hence this drives the real |
| 14 | +`build/c/codebase-memory-mcp` server over stdio and samples its RSS from `ps`. |
| 15 | +
|
| 16 | +WHAT IT SHOWS |
| 17 | +------------- |
| 18 | +A long-lived MCP server is driven through K index_repository cycles of the same |
| 19 | +fixture. The in-process pipeline (CBM_INDEX_SUPERVISOR=0) is the pre-#832-fix |
| 20 | +background-path behaviour: RSS RATCHETS across cycles. The supervised subprocess |
| 21 | +path (default) is the fix: each child returns 100% of its RSS on exit, so the |
| 22 | +long-lived parent stays ~FLAT. The auto-index (mcp.c) and watcher re-index |
| 23 | +(main.c) paths now route through that same supervised subprocess, so they inherit |
| 24 | +this flat profile; the deterministic routing proof is the GATING guard |
| 25 | +tests/test_mcp.c::index_bg_paths_route_through_supervisor_issue832. |
| 26 | +
|
| 27 | +Inherently noisy (allocator/OS dependent) -> thresholds are generous and this is |
| 28 | +NOT wired into `make test` / `ci-ok`. Run manually: |
| 29 | + make -f Makefile.cbm cbm |
| 30 | + python3 tests/repro/issue832_rss.py |
| 31 | +""" |
| 32 | +import json |
| 33 | +import os |
| 34 | +import shutil |
| 35 | +import subprocess |
| 36 | +import sys |
| 37 | +import tempfile |
| 38 | + |
| 39 | +ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
| 40 | +BINARY = os.path.join(ROOT, "build", "c", "codebase-memory-mcp") |
| 41 | +CYCLES = 10 |
| 42 | +NUM_FILES = 120 # enough files to fan out across worker threads (abandoned heaps) |
| 43 | + |
| 44 | + |
| 45 | +def rss_kb(pid): |
| 46 | + out = subprocess.check_output(["ps", "-o", "rss=", "-p", str(pid)]) |
| 47 | + return int(out.strip()) |
| 48 | + |
| 49 | + |
| 50 | +def make_fixture(d): |
| 51 | + for i in range(NUM_FILES): |
| 52 | + with open(os.path.join(d, f"mod_{i}.py"), "w") as f: |
| 53 | + for j in range(20): |
| 54 | + f.write(f"def fn_{i}_{j}(a, b):\n") |
| 55 | + f.write(f" x = a + b + {i} * {j}\n") |
| 56 | + f.write(" return x\n\n") |
| 57 | + |
| 58 | + |
| 59 | +def run_series(repo, cache, supervised): |
| 60 | + env = dict(os.environ) |
| 61 | + env["CBM_CACHE_DIR"] = cache |
| 62 | + if supervised: |
| 63 | + env.pop("CBM_INDEX_SUPERVISOR", None) |
| 64 | + else: |
| 65 | + env["CBM_INDEX_SUPERVISOR"] = "0" # in-process (pre-fix background behaviour) |
| 66 | + env["CBM_INDEX_WORKER_TIMEOUT_S"] = "120" |
| 67 | + |
| 68 | + proc = subprocess.Popen( |
| 69 | + [BINARY], stdin=subprocess.PIPE, stdout=subprocess.PIPE, |
| 70 | + stderr=subprocess.DEVNULL, env=env, text=True, bufsize=1, |
| 71 | + ) |
| 72 | + |
| 73 | + def rpc(obj): |
| 74 | + proc.stdin.write(json.dumps(obj) + "\n") |
| 75 | + proc.stdin.flush() |
| 76 | + return proc.stdout.readline() |
| 77 | + |
| 78 | + rpc({"jsonrpc": "2.0", "id": 0, "method": "initialize", "params": {}}) |
| 79 | + series = [] |
| 80 | + for k in range(CYCLES): |
| 81 | + rpc({"jsonrpc": "2.0", "id": k + 1, "method": "tools/call", |
| 82 | + "params": {"name": "index_repository", |
| 83 | + "arguments": {"repo_path": repo, "mode": "fast"}}}) |
| 84 | + series.append(rss_kb(proc.pid)) |
| 85 | + try: |
| 86 | + proc.stdin.close() |
| 87 | + proc.wait(timeout=15) |
| 88 | + except Exception: |
| 89 | + proc.kill() |
| 90 | + return series |
| 91 | + |
| 92 | + |
| 93 | +def main(): |
| 94 | + if not os.path.exists(BINARY): |
| 95 | + print(f"missing prod binary: {BINARY}\n build it: make -f Makefile.cbm cbm") |
| 96 | + return 2 |
| 97 | + base = tempfile.mkdtemp(prefix="cbm-832-") |
| 98 | + repo = os.path.join(base, "repo") |
| 99 | + os.makedirs(repo) |
| 100 | + make_fixture(repo) |
| 101 | + try: |
| 102 | + inproc = run_series(repo, os.path.join(base, "c1"), supervised=False) |
| 103 | + superv = run_series(repo, os.path.join(base, "c2"), supervised=True) |
| 104 | + finally: |
| 105 | + shutil.rmtree(base, ignore_errors=True) |
| 106 | + |
| 107 | + def mb(kb): |
| 108 | + return kb / 1024.0 |
| 109 | + |
| 110 | + print(f"cycles={CYCLES} files={NUM_FILES}") |
| 111 | + print("cycle | in-process(MB) | supervised(MB)") |
| 112 | + for i in range(CYCLES): |
| 113 | + print(f" {i:2d} | {mb(inproc[i]):8.1f} | {mb(superv[i]):8.1f}") |
| 114 | + |
| 115 | + ip_peak = max(mb(x) for x in inproc) |
| 116 | + sv_peak = max(mb(x) for x in superv) |
| 117 | + print(f"\nin-process peak resident: {ip_peak:8.1f} MB") |
| 118 | + print(f"supervised peak resident: {sv_peak:8.1f} MB") |
| 119 | + # The decisive, robust signal at laptop-fixture scale is the RESIDENT-LEVEL |
| 120 | + # contrast, not cycle-over-cycle growth: the in-process server keeps the whole |
| 121 | + # index working set resident (it never leaves the long-lived process), while |
| 122 | + # the supervised path returns it every cycle (the child exits) -> the server |
| 123 | + # stays near its idle baseline. The unbounded ratchet in the field (#832, GB |
| 124 | + # over hours) is the same effect amplified by worker-thread count + cycle count |
| 125 | + # beyond what a small fixture surfaces. Generous threshold; report-only, |
| 126 | + # NON-GATING. |
| 127 | + verdict = "SUPERVISED ISOLATION reproduced (server stays near baseline)" \ |
| 128 | + if sv_peak < ip_peak / 2 \ |
| 129 | + else "inconclusive (env-dependent; see numbers)" |
| 130 | + print(f"verdict: {verdict}") |
| 131 | + return 0 |
| 132 | + |
| 133 | + |
| 134 | +if __name__ == "__main__": |
| 135 | + sys.exit(main()) |
0 commit comments