The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(code-health): HX006 accepts proc.wait() right after proc.kill()
teknium1dd04dbedb18f
diff --git a/scripts/code_health/config.py b/scripts/code_health/config.pyindex fd6ef4e019d..b410164b050 100644--- a/scripts/code_health/config.py+++ b/scripts/code_health/config.py@@ -73,7 +73,8 @@ RULES: tuple[Rule, ...] = (Rule("S113", "HTTP request without timeout", "pass `timeout=`", "ruff"),Rule("HX006", "subprocess/urlopen without timeout", "pass `timeout=`; an asyncio"" `proc.communicate()` goes inside `asyncio.wait_for(..., timeout=...)` (a child or"- " socket that never answers hangs forever)", "ast"),+ " socket that never answers hangs forever); after `proc.kill()` reap with `proc.wait()`,"+ " never a bare `communicate()` (it blocks while a grandchild holds the pipe)", "ast"),Rule("HX007", "sync config I/O inside async def", "`await asyncio.to_thread(load_config)`"" or read it before entering the event loop", "ast"),Rule("HX008", "asyncio.get_event_loop()", "`asyncio.get_running_loop()` inside a"diff --git a/scripts/code_health/py_rules.py b/scripts/code_health/py_rules.pyindex 9338c6b7dc7..61c73295ca1 100644--- a/scripts/code_health/py_rules.py+++ b/scripts/code_health/py_rules.py@@ -427,9 +427,31 @@ def _process_handles(tree: ast.Module) -> dict[str, str]:_PROCESS_WAITS = {"communicate": 1, "wait": 0}+def _reaped_after_kill(tree: ast.Module, handles: dict[str, str]) -> set[int]:+ """ids of a sync ``proc.wait()`` that directly follows ``proc.kill()``: SIGKILL bounds it.+ Not ``communicate()`` (it reads until every grandchild holding the pipe exits) and not the+ asyncio ``Process.wait()`` (it waits for the pipe transports too); both measured to hang."""+ reaped: set[int] = set()+ for node in ast.walk(tree):+ for field in ("body", "orelse", "finalbody"):+ stmts = getattr(node, field, None)+ if not isinstance(stmts, list):+ continue+ for first, second in zip(stmts, stmts[1:]):+ kill = first.value if isinstance(first, ast.Expr) else None+ wait = second.value if isinstance(second, (ast.Expr, ast.Assign)) else None+ if not (isinstance(kill, ast.Call) and isinstance(wait, ast.Call)):+ continue+ head, _, leaf = _call_name(kill).rpartition(".")+ if (leaf == "kill" and handles.get(head) == "sync"+ and _call_name(wait) == f"{head}.wait"):+ reaped.add(id(wait))+ return reaped++def missing_timeout(tree: ast.Module, ctx: Ctx) -> Iterable[int]:- bounded = _bounded_calls(tree)handles = _process_handles(tree)+ bounded = _bounded_calls(tree) | _reaped_after_kill(tree, handles)for node in ast.walk(tree):if not isinstance(node, ast.Call) or id(node) in bounded:continuediff --git a/tests/scripts/test_code_health.py b/tests/scripts/test_code_health.pyindex 8ee8cb4a7ca..7ca489910a1 100644--- a/tests/scripts/test_code_health.py+++ b/tests/scripts/test_code_health.py@@ -116,6 +116,13 @@ _STUB = {"pkg/b.py": "def legacy(x):\n return x\n"}({}, {"pkg/p.py": _PROC.format(" return await asyncio.wait_for(fut=proc.communicate(), timeout=1)")}, False),# a BOM reads the same from git and from disk, so a new BOM file's debt is still measured({}, {"pkg/bom.py": "\ufeff" + _LEGACY}, True),+ # after kill(), a sync wait() is bounded by SIGKILL; communicate() and asyncio's wait() block+ # while a grandchild holds the pipe (measured), so those stay flagged+ *(({}, {"pkg/k.py": "import subprocess\n\n\ndef f(c):\n proc = subprocess.Popen(c)\n"+ f" try:\n proc.wait(timeout=1)\n except subprocess.TimeoutExpired:\n"+ f" proc.kill()\n {tail}\n"}, blocked)+ for tail, blocked in (("proc.wait()", False), ("proc.communicate()", True))),+ ({}, {"pkg/k.py": _PROC.format(" proc.kill()\n await proc.wait()")}, True),# rules see through import aliases and require the real asyncio deadline API({}, {"pkg/c.py": "from subprocess import run as execute\n\n\ndef f(c):\n return execute(c)\n"}, True),({}, {"pkg/c.py": "def run(c):\n return c\n\n\ndef f(c):\n return run(c)\n"}, False),