Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

fix(mcp): stop watchdog walk at unnamed or dead ancestor (#3142)

Aman46077b79f0ab
diff --git a/packages/server/src/repowise/server/mcp_server/_watchdog.py b/packages/server/src/repowise/server/mcp_server/_watchdog.py
index b10b9a6c9..b89ad538b 100644
--- a/packages/server/src/repowise/server/mcp_server/_watchdog.py
+++ b/packages/server/src/repowise/server/mcp_server/_watchdog.py
@@ -50,12 +50,10 @@ _DISABLE_ENV = "REPOWISE_MCP_NO_WATCHDOG"
_POLL_INTERVAL_SECONDS = 5.0
-def _is_launcher(name: str | None) -> bool:
- if not name:
- # Unknown name: treat as a launcher so the walk continues to a
- # nameable ancestor rather than anointing a mystery process as
- # the client.
- return True
+def _is_launcher(name: str) -> bool:
+ # Every caller of _is_launcher has already excluded an unresolved name:
+ # compute_watch_set stops the walk before reaching it (an unnamed
+ # ancestor is never appended, let alone tested for launcher-ness).
normalized = name.lower()
if normalized.endswith(".exe"):
normalized = normalized[:-4]
@@ -68,9 +66,19 @@ def compute_watch_set() -> list[ProcInfo]:
Stops at the client so unrelated higher ancestors (shell, terminal,
desktop session) are never watched — those may die while the client
legitimately keeps running. Returns ``[]`` when nothing was resolvable.
+
+ Also stops, without appending, at an ancestor whose name could not be
+ resolved or whose process is already gone: on Windows an unnamed entry
+ is always the chain's last one (``ancestor_chain`` resolves a parent's
+ name from the child's own snapshot entry, so a parent that had already
+ exited before the snapshot was taken comes back nameless), and a process
+ that is already dead at startup cannot later tell us the client left.
+ Watching either would treat a dead process's leftover PID as the client.
"""
watch: list[ProcInfo] = []
for info in ancestor_chain(os.getpid()):
+ if info.name is None or pid_alive(info.pid) is False:
+ break
watch.append(info)
if not _is_launcher(info.name):
break
diff --git a/tests/unit/server/test_mcp_watchdog.py b/tests/unit/server/test_mcp_watchdog.py
index 6fcb54c9a..d847d45fb 100644
--- a/tests/unit/server/test_mcp_watchdog.py
+++ b/tests/unit/server/test_mcp_watchdog.py
@@ -42,10 +42,9 @@ from repowise.server.mcp_server import _watchdog
"bash",
"powershell.exe",
"pwsh",
- None,
],
)
-def test_launcher_names(name: str | None) -> None:
+def test_launcher_names(name: str) -> None:
assert _watchdog._is_launcher(name) is True
@@ -74,6 +73,7 @@ def test_watch_set_stops_at_client(monkeypatch: pytest.MonkeyPatch) -> None:
ProcInfo(pid=104, name="explorer.exe", create_token="e"),
]
monkeypatch.setattr(_watchdog, "ancestor_chain", lambda _pid: chain)
+ monkeypatch.setattr(_watchdog, "pid_alive", lambda _pid: True)
watch = _watchdog.compute_watch_set()
@@ -93,10 +93,67 @@ def test_watch_set_all_launchers_keeps_whole_chain(
ProcInfo(pid=101, name="python3", create_token="b"),
]
monkeypatch.setattr(_watchdog, "ancestor_chain", lambda _pid: chain)
+ monkeypatch.setattr(_watchdog, "pid_alive", lambda _pid: True)
assert [w.pid for w in _watchdog.compute_watch_set()] == [100, 101]
+def test_watch_set_stops_before_an_unnamed_ancestor(monkeypatch: pytest.MonkeyPatch) -> None:
+ """On Windows the chain's last entry is nameless when that ancestor had
+ already exited before the snapshot was read (#3106) — it must never be
+ watched, and nothing above it in the chain is lost by stopping there."""
+ chain = [
+ ProcInfo(pid=100, name="python.exe", create_token="a"),
+ ProcInfo(pid=101, name="bash.exe", create_token="b"),
+ ProcInfo(pid=102, name=None, create_token="c"),
+ ]
+ monkeypatch.setattr(_watchdog, "ancestor_chain", lambda _pid: chain)
+ monkeypatch.setattr(_watchdog, "pid_alive", lambda _pid: True)
+
+ assert [w.pid for w in _watchdog.compute_watch_set()] == [100, 101]
+
+
+def test_watch_set_stops_at_an_unnamed_ancestor_in_the_middle(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ chain = [
+ ProcInfo(pid=100, name="python.exe", create_token="a"),
+ ProcInfo(pid=101, name=None, create_token="b"),
+ ProcInfo(pid=102, name="claude.exe", create_token="c"),
+ ]
+ monkeypatch.setattr(_watchdog, "ancestor_chain", lambda _pid: chain)
+ monkeypatch.setattr(_watchdog, "pid_alive", lambda _pid: True)
+
+ assert [w.pid for w in _watchdog.compute_watch_set()] == [100]
+
+
+def test_watch_set_skips_an_ancestor_already_dead_at_startup(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ """A process gone before the set is even computed can never later tell
+ us the client left, so there is nothing gained by watching it (#3106)."""
+ chain = [
+ ProcInfo(pid=100, name="python.exe", create_token="a"),
+ ProcInfo(pid=101, name="claude.exe", create_token="b"),
+ ]
+ monkeypatch.setattr(_watchdog, "ancestor_chain", lambda _pid: chain)
+ monkeypatch.setattr(_watchdog, "pid_alive", lambda pid: pid != 101)
+
+ assert [w.pid for w in _watchdog.compute_watch_set()] == [100]
+
+
+def test_watch_set_unknown_liveness_is_not_treated_as_dead(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ """Fail open: a liveness probe that can't tell must not shrink the
+ watch set, matching the module's overall conservative failure policy."""
+ chain = [ProcInfo(pid=100, name="claude.exe", create_token="a")]
+ monkeypatch.setattr(_watchdog, "ancestor_chain", lambda _pid: chain)
+ monkeypatch.setattr(_watchdog, "pid_alive", lambda _pid: None)
+
+ assert [w.pid for w in _watchdog.compute_watch_set()] == [100]
+
+
# ---------------------------------------------------------------------------
# _ancestor_died
# ---------------------------------------------------------------------------