-
Notifications
You must be signed in to change notification settings - Fork 10
fix: exit orphaned stdio MCP servers on parent death #66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jack-arturo
wants to merge
6
commits into
main
Choose a base branch
from
fix/stdio-orphan-exit
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+111
−1
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d0dccd9
fix: exit orphaned stdio MCP servers on parent death
jack-arturo f59cb90
fix: pin parent pid early; document soft stdin EOF
jack-arturo c79c0a7
fix: ship parent_watchdog in setuptools py-modules
jack-arturo aba55b0
fix: satisfy ruff import sorting for parent watchdog
jack-arturo 31be7d6
fix: ruff-format parent watchdog call sites
jack-arturo 7130851
fix: exclude streamdeck_assets from ruff format/lint
jack-arturo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| """Parent-liveness watchdog for stdio MCP servers. | ||
|
|
||
| When an intermediate wrapper keeps stdin open after the client dies, the | ||
| Python MCP stdio loop never sees EOF and the process leaks forever (swap | ||
| thrash). This watchdog exits once os.getppid() changes from the original | ||
| parent (POSIX reparenting). | ||
|
|
||
| Soft stdin EOF is intentional: do NOT hard-exit on EOF alone. One-shot | ||
| pipe clients must be able to finish in-flight handlers. Hard exit is | ||
| reserved for parent-watchdog reparent (and OS signals installed by the | ||
| host process). | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import os | ||
| import threading | ||
| import time | ||
| from collections.abc import Callable | ||
|
|
||
| DEFAULT_PARENT_WATCHDOG_S = 30.0 | ||
| MIN_PARENT_WATCHDOG_S = 0.1 | ||
|
|
||
|
|
||
| def parse_watchdog_interval_s(raw: str | None) -> float: | ||
| if raw is None or raw.strip() == "": | ||
| return DEFAULT_PARENT_WATCHDOG_S | ||
| try: | ||
| value = float(raw) | ||
| except ValueError: | ||
| return DEFAULT_PARENT_WATCHDOG_S | ||
| if value <= 0: | ||
| return DEFAULT_PARENT_WATCHDOG_S | ||
| return max(value, MIN_PARENT_WATCHDOG_S) | ||
|
|
||
|
|
||
| def start_parent_watchdog( | ||
| parent_pid: int, | ||
| interval_s: float, | ||
| on_dead: Callable[[], None], | ||
| *, | ||
| is_parent_gone: Callable[[int], bool] | None = None, | ||
| ) -> threading.Thread: | ||
| probe = is_parent_gone or (lambda pid: os.getppid() != pid) | ||
|
jack-arturo marked this conversation as resolved.
jack-arturo marked this conversation as resolved.
|
||
| fired = {"value": False} | ||
|
|
||
| def _loop() -> None: | ||
| while not fired["value"]: | ||
| if probe(parent_pid): | ||
| fired["value"] = True | ||
| on_dead() | ||
| return | ||
| time.sleep(interval_s) | ||
|
|
||
| thread = threading.Thread(target=_loop, name="mcp-parent-watchdog", daemon=True) | ||
| thread.start() | ||
| return thread | ||
|
|
||
|
|
||
| def install_stdio_parent_watchdog( | ||
| env_name: str, | ||
| *, | ||
| parent_pid: int | None = None, | ||
| ) -> None: | ||
| # Prefer a pid captured before any await in main — getppid() is dynamic. | ||
| pinned = os.getppid() if parent_pid is None else parent_pid | ||
| interval = parse_watchdog_interval_s(os.environ.get(env_name)) | ||
|
|
||
| def _exit() -> None: | ||
| os._exit(0) | ||
|
|
||
| start_parent_watchdog(pinned, interval, _exit) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import time | ||
|
|
||
| from parent_watchdog import ( | ||
| DEFAULT_PARENT_WATCHDOG_S, | ||
| parse_watchdog_interval_s, | ||
| start_parent_watchdog, | ||
| ) | ||
|
|
||
|
|
||
| def test_parse_defaults(): | ||
| assert parse_watchdog_interval_s(None) == DEFAULT_PARENT_WATCHDOG_S | ||
| assert parse_watchdog_interval_s("") == DEFAULT_PARENT_WATCHDOG_S | ||
| assert parse_watchdog_interval_s("nope") == DEFAULT_PARENT_WATCHDOG_S | ||
| assert parse_watchdog_interval_s("0") == DEFAULT_PARENT_WATCHDOG_S | ||
| assert parse_watchdog_interval_s("-1") == DEFAULT_PARENT_WATCHDOG_S | ||
|
|
||
|
|
||
| def test_parse_positive_floor(): | ||
| assert parse_watchdog_interval_s("2.5") == 2.5 | ||
| assert parse_watchdog_interval_s("0.01") == 0.1 | ||
|
|
||
|
|
||
| def test_watchdog_fires_once(): | ||
| hits = [] | ||
| start_parent_watchdog(1, 0.05, lambda: hits.append(1), is_parent_gone=lambda _pid: True) | ||
| time.sleep(0.2) | ||
| assert hits == [1] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.