Skip to content

AI agent skill that researches any topic across Reddit, X, YouTube, HN, Polymarket, and the web - then synthesizes a grounded summary

fix(subproc): escalate to SIGKILL when child ignores SIGTERM (#433)

Ilia Alshanetsky87ef4db772df
diff --git a/skills/last30days/scripts/lib/subproc.py b/skills/last30days/scripts/lib/subproc.py
index 74e07d1c..da6d4309 100644
--- a/skills/last30days/scripts/lib/subproc.py
+++ b/skills/last30days/scripts/lib/subproc.py
@@ -87,7 +87,15 @@ def run_with_timeout(
proc.kill()
except (ProcessLookupError, PermissionError, OSError, AttributeError):
proc.kill()
- proc.wait(timeout=5)
+ try:
+ proc.wait(timeout=5)
+ except subprocess.TimeoutExpired:
+ # Child ignored SIGTERM (or our killpg lost the race); escalate.
+ try:
+ os.killpg(os.getpgid(proc.pid), signal.SIGKILL)
+ except (ProcessLookupError, PermissionError, OSError):
+ proc.kill()
+ proc.wait()
raise SubprocTimeout(f"Command {cmd[0]} timed out after {timeout}s")
return SubprocResult(
diff --git a/tests/test_subproc.py b/tests/test_subproc.py
index faca80cc..afd09ee6 100644
--- a/tests/test_subproc.py
+++ b/tests/test_subproc.py
@@ -142,5 +142,19 @@ class TestRunWithTimeout(unittest.TestCase):
self.assertEqual(result.returncode, 0)
self.assertEqual(result.stdout.strip(), "ok")
+ def test_sigterm_ignoring_child_is_sigkill_escalated(self):
+ """A child that ignores SIGTERM must be escalated to SIGKILL.
+
+ Without escalation, ``proc.wait(timeout=5)`` raises
+ ``subprocess.TimeoutExpired`` past the ``except`` block instead of the
+ documented ``SubprocTimeout``, and the child stays alive.
+ """
+ with self.assertRaises(subproc.SubprocTimeout):
+ subproc.run_with_timeout(
+ ["sh", "-c", "trap '' TERM; sleep 30"],
+ timeout=1,
+ )
+
+
if __name__ == "__main__":
unittest.main()