Skip to content

Commit e4ce738

Browse files
cliffhallclaude
andcommitted
fix(fetch): bound the cleanup after a Readability.js timeout
After a timeout the runner drained the killed child's pipes with an unbounded communicate(), so a helper that left the process group and kept stdout or stderr open hung the call anyway. It now only reaps the child, for at most 5 s. On Windows, a taskkill that cannot start or does not finish no longer skips killing the direct child. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent ab56c39 commit e4ce738

2 files changed

Lines changed: 73 additions & 8 deletions

File tree

‎src/fetch/src/mcp_server_fetch/readability_js.py‎

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@
5353
INSTALL_TIMEOUT_SECONDS = 120.0
5454
# How long `node -v` may take.
5555
VERSION_TIMEOUT_SECONDS = 10.0
56+
# How long cleanup after a timeout may wait: for `taskkill`, and for the
57+
# killed child to be reaped.
58+
KILL_TIMEOUT_SECONDS = 5.0
5659
# readabilipy's `package.json` declares `"engines": {"node": ">=14.0.0"}`.
5760
MIN_NODE_MAJOR = 14
5861
# What `ExtractArticle.js` requires; all three present means installed.
@@ -83,13 +86,19 @@ def _forward(output: str) -> None:
8386
def _kill_tree(proc: subprocess.Popen[str]) -> None:
8487
"""Kill ``proc`` and everything it started (npm runs node, `npm.cmd` runs cmd)."""
8588
if sys.platform == "win32":
86-
subprocess.run(
87-
["taskkill", "/F", "/T", "/PID", str(proc.pid)],
88-
stdin=subprocess.DEVNULL,
89-
stdout=subprocess.DEVNULL,
90-
stderr=subprocess.DEVNULL,
91-
check=False,
92-
)
89+
try:
90+
subprocess.run(
91+
["taskkill", "/F", "/T", "/PID", str(proc.pid)],
92+
stdin=subprocess.DEVNULL,
93+
stdout=subprocess.DEVNULL,
94+
stderr=subprocess.DEVNULL,
95+
check=False,
96+
timeout=KILL_TIMEOUT_SECONDS,
97+
)
98+
except (OSError, subprocess.SubprocessError):
99+
# taskkill could not start or did not finish: the direct child
100+
# is still killed below.
101+
pass
93102
else:
94103
try:
95104
# start_new_session made the child a process-group leader.
@@ -126,7 +135,13 @@ def _run(
126135
stdout, stderr = proc.communicate(timeout=timeout)
127136
except subprocess.TimeoutExpired:
128137
_kill_tree(proc)
129-
proc.communicate()
138+
# Reap the child, but do not drain its pipes: a descendant that left
139+
# its process group (or survived taskkill) can hold them open forever.
140+
# The pipes close when `proc` is collected.
141+
try:
142+
proc.wait(timeout=KILL_TIMEOUT_SECONDS)
143+
except subprocess.TimeoutExpired:
144+
pass
130145
_log(f"{Path(argv[0]).name} did not finish within {timeout:g} s and was killed")
131146
return None
132147
return subprocess.CompletedProcess(argv, proc.returncode, stdout, stderr)

‎src/fetch/tests/test_readability_subprocess.py‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,56 @@ def kill(self) -> None:
425425
assert killed == [True]
426426

427427

428+
def test_kill_tree_kills_the_child_when_taskkill_fails(
429+
monkeypatch: pytest.MonkeyPatch,
430+
) -> None:
431+
killed: list[bool] = []
432+
timeouts: list[Any] = []
433+
434+
class Proc:
435+
pid = 4242
436+
437+
def kill(self) -> None:
438+
killed.append(True)
439+
440+
def run(argv: list[str], **kwargs: Any) -> None:
441+
timeouts.append(kwargs.get("timeout"))
442+
raise OSError("taskkill is blocked")
443+
444+
monkeypatch.setattr(readability_js.sys, "platform", "win32")
445+
monkeypatch.setattr(readability_js.subprocess, "run", run)
446+
readability_js._kill_tree(cast("subprocess.Popen[str]", Proc()))
447+
assert timeouts == [readability_js.KILL_TIMEOUT_SECONDS]
448+
assert killed == [True]
449+
450+
451+
@posix_only
452+
def test_timeout_returns_while_a_detached_helper_holds_the_pipes(
453+
bindir: Path, jsdir: Path, monkeypatch: pytest.MonkeyPatch, tmp_path: Path
454+
) -> None:
455+
# A helper that leaves the process group (its own session) survives the
456+
# group kill and keeps stdout and stderr open; the run must still end.
457+
install(jsdir)
458+
pid = tmp_path / "helper.pid"
459+
helper = (
460+
"import os, time; os.setsid(); "
461+
f"open({str(pid)!r}, 'w').write(str(os.getpid())); time.sleep(3600)"
462+
)
463+
tool(bindir, "node", NODE_VERSION + f'"{sys.executable}" -c "{helper}" &\nwait\n')
464+
monkeypatch.setattr(readability_js, "EXTRACT_TIMEOUT_SECONDS", 1.0)
465+
started = time.monotonic()
466+
try:
467+
assert readability_js.extract_article(ARTICLE) is None
468+
assert (
469+
time.monotonic() - started < 1.0 + readability_js.KILL_TIMEOUT_SECONDS + 3
470+
)
471+
finally:
472+
deadline = time.monotonic() + 5
473+
while not pid.exists() and time.monotonic() < deadline: # pragma: no cover
474+
time.sleep(0.05)
475+
os.kill(int(pid.read_text()), 9)
476+
477+
428478
@posix_only
429479
def test_kill_tree_tolerates_a_group_already_gone(
430480
monkeypatch: pytest.MonkeyPatch,

0 commit comments

Comments
 (0)