Prevent macOS watcher bursts from blocking stdio shutdown
Problem
On macOS 26.5.2, codanna serve --watch can remain alive and consume CPU after its stdio MCP parent exits. I first observed it after sleep/wake, then reproduced the same watcher backlog without another sleep cycle.
A process sample showed two related paths:
- the notify/FSEvents callback waiting in Tokio
blocking_sendwhile the 100-event channel was full; - a Tokio worker processing per-file indexing and semantic-vector saves while stdio-owned watcher tasks had no coordinated cancellation.
This can leave the old process holding serve.lock, so a replacement MCP server fails to start.
Reproduction shape
- Start
codanna serve --watchover a medium or large workspace with semantic search enabled. - Produce a large filesystem event burst (sleep/wake or a large checkout/write burst).
- Close the stdio transport while the burst is being processed.
- Observe delayed/non-terminating shutdown, CPU use, and sometimes a retained serve process/lock.
Proposed fix
- Keep the platform watcher callback independent of Tokio backpressure.
- Reconcile large modification bursts through the existing batch indexing lane.
- Give stdio watcher/listener tasks the same coordinated cancellation discipline used by HTTP/HTTPS.
- Add a regression that closes stdin during a 256-file burst and requires clean exit.
This does not change the MCP schema or index format.
Same behaviour on Windows 11 with codanna 0.12.0, with semantic search disabled, so it isn't only the semantic-vector save path.
Setup: a synthetic tree of N one-function .ts files, codanna index, then codanna serve --watch -c settings.toml over stdio with [file_watch] enabled = true and [semantic_search] enabled = false. Send the MCP initialize handshake, rewrite all N files, wait, then close stdin.
| Case | Stdin close to process exit |
|---|---|
| Idle, or one file edited just before close (6 trials) | 0.11-0.20 s |
| 800 files rewritten, stdin closed 10 s later | 89 s, about 140 CPU-seconds used |
| 2,000 files rewritten, stdin closed 30 s or 180 s later | still running 60 s after close, about 2 cores busy (killed at that point) |
So on Windows too, stdio shutdown waits for the watcher backlog to be processed, and the burst re-index is costly here (roughly 0.17 CPU-seconds per one-function file). In practice this left an orphaned serve --watch process behind after the parent MCP client exited, following a large checkout in the watched tree.
Repro script (Python 3, codanna on PATH)
import json, subprocess, sys, time
from pathlib import Path
root, n, settle = Path(sys.argv[1]).resolve(), int(sys.argv[2]), float(sys.argv[3])
tree, index, settings = root / "tree", root / "index", root / "settings.toml"
tree.mkdir(parents=True, exist_ok=True)
def write_all(gen):
for i in range(n):
d = tree / f"d{i % 40}"; d.mkdir(exist_ok=True)
(d / f"m{i}.ts").write_text(f"export function f{i}_{gen}(x: number): number {{ return x + {i} + {gen} }}\n")
write_all(0)
settings.write_text(f'version = 1\nindex_path = "{index.as_posix()}"\n[indexing]\nindexed_paths = [\'{tree}\']\n'
'[semantic_search]\nenabled = false\n[file_watch]\nenabled = true\n[server]\nmode = "stdio"\n')
subprocess.run(["codanna", "index", str(tree), "-c", str(settings)], capture_output=True, timeout=600)
p = subprocess.Popen(["codanna", "serve", "--watch", "-c", str(settings)],
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL)
time.sleep(4)
init = {"jsonrpc": "2.0", "id": 1, "method": "initialize",
"params": {"protocolVersion": "2025-06-18", "capabilities": {}, "clientInfo": {"name": "repro", "version": "0"}}}
p.stdin.write((json.dumps(init) + "\n").encode()); p.stdin.flush(); p.stdout.readline()
write_all(1)
time.sleep(settle)
t0 = time.monotonic(); p.stdin.close()
try:
p.wait(timeout=60); print(f"exited {time.monotonic() - t0:.2f}s after stdin closed")
except subprocess.TimeoutExpired:
print("still running 60s after stdin closed"); p.kill()
Run as python repro.py <empty-dir> 2000 30.