AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(backend/copilot): bash_exec's kill-tree no longer leaves children alive when another process exits mid-listing (#15254)
Bentlyfc7e45a811d6
diff --git a/autogpt_platform/backend/backend/copilot/tools/bash_exec.py b/autogpt_platform/backend/backend/copilot/tools/bash_exec.pyindex 242fd9b61a..b27b0fa45f 100644--- a/autogpt_platform/backend/backend/copilot/tools/bash_exec.py+++ b/autogpt_platform/backend/backend/copilot/tools/bash_exec.py@@ -77,23 +77,36 @@ _HUNG_CAP_MARGIN_SECONDS = 120# shell first, so it cannot start the next step once a child dies, then stops# and kills every process under it. The shell itself is left for envd's kill.# Exits non-zero if any of them is still alive afterwards (e.g. one run as root).+# /proc is read through cat because awk aborts on an input file that vanished+# after the glob expanded, which any process exiting meanwhile causes, and the+# listing would then come back empty. A child can fork between a listing and+# its STOP, so the stopping repeats until the listing holds still; if it never+# does, whatever forked after the last listing may survive, so that exits 1 too._KILL_TREE_SCRIPT = r"""p=__PID__kill -STOP "$p" 2>/dev/null || exit 0under() {- awk -v root="$p" '+ cat /proc/[0-9]*/stat 2>/dev/null | awk -v root="$p" '{ pid = $1; s = $0; sub(/.*\) /, "", s); split(s, f, " ")parent[pid] = f[2]; state[pid] = f[1] }END { q[1] = root; n = 1while (n) { r = q[n--]for (k in parent) if (parent[k] == r) {q[++n] = k; if (state[k] != "Z") print k } } }- ' /proc/[0-9]*/stat 2>/dev/null+ ' | sort -n}-kids=$(under); [ -n "$kids" ] && kill -STOP $kids 2>/dev/null-kids=$(under); [ -n "$kids" ] && kill -KILL $kids 2>/dev/null-sleep 0.2-[ -z "$(under)" ]+kids= settled=+for _ in 1 2 3 4 5 6 7 8 9 10; do+ now=$(under); [ "$now" = "$kids" ] && settled=1 && break+ kids=$now; [ -n "$kids" ] && kill -STOP $kids 2>/dev/null+done+[ -n "$kids" ] && kill -KILL $kids 2>/dev/null+[ -n "$settled" ] || exit 1+for _ in 1 2 3 4 5 6 7 8 9 10; do+ [ -z "$(under)" ] && exit 0+ sleep 0.1+done+exit 1"""diff --git a/autogpt_platform/backend/backend/copilot/tools/bash_exec_test.py b/autogpt_platform/backend/backend/copilot/tools/bash_exec_test.pyindex a9194542c7..23dc90001a 100644--- a/autogpt_platform/backend/backend/copilot/tools/bash_exec_test.py+++ b/autogpt_platform/backend/backend/copilot/tools/bash_exec_test.py@@ -2,7 +2,9 @@import asyncioimport itertools+import osimport re+import signalimport subprocessimport sysimport time@@ -19,7 +21,7 @@ from e2b.envd.rpc import handle_rpc_exceptionfrom e2b.exceptions import NotFoundException, TimeoutExceptionfrom ._test_data import make_session-from .bash_exec import BashExecTool+from .bash_exec import _KILL_TREE_SCRIPT, BashExecToolfrom .models import BashExecResponse, ErrorResponse_USER = "user-bash-exec-test"@@ -919,8 +921,57 @@ def _alive(pid: int) -> bool:def test_kill_tree_script_kills_a_compound_commands_children():# The reviewer's repro: killing the shell of `sleep 30 && echo done` left# sleep running under a new parent.- from .bash_exec import _KILL_TREE_SCRIPT+ _kill_tree_of_sleep_then_echo()+[email protected](sys.platform != "linux", reason="reads /proc")+def test_kill_tree_script_holds_while_other_processes_exit():+ # A process exiting between the /proc glob and awk reading its stat file+ # aborted awk, so the listing came back empty and the child was left+ # running, or stopped and orphaned once the shell was killed.+ churn = subprocess.Popen(["bash", "-c", "while :; do /bin/true; done"])+ try:+ for _ in range(10):+ _kill_tree_of_sleep_then_echo()+ finally:+ churn.kill()+ churn.wait(timeout=10)++[email protected](sys.platform != "linux", reason="reads /proc")+def test_kill_tree_script_kills_children_forked_while_it_stops_them():+ # A child forked between a listing and its STOP outlived a single pass; the+ # outer shell's `; :` keeps the forking loop a child rather than p itself.+ shell = subprocess.Popen(+ ["bash", "-c", 'bash -c "while :; do sleep 30 & sleep 0.005; done"; :'],+ start_new_session=True,+ )+ try:+ time.sleep(0.3)+ subprocess.run(+ ["bash", "-c", _KILL_TREE_SCRIPT.replace("__PID__", str(shell.pid))],+ check=True,+ timeout=10,+ )+ assert [p for p in _session(shell.pid) if p != shell.pid and _alive(p)] == []+ finally:+ os.killpg(shell.pid, signal.SIGKILL)+ shell.wait(timeout=10)+++def _session(sid: int) -> list[int]:+ found = []+ for stat in Path("/proc").glob("[0-9]*/stat"):+ try:+ fields = stat.read_text().rsplit(") ", 1)[1].split()+ except (OSError, IndexError):+ continue+ if int(fields[3]) == sid:+ found.append(int(stat.parent.name))+ return found+++def _kill_tree_of_sleep_then_echo() -> None:shell = subprocess.Popen(["bash", "-c", "sleep 30 && echo done"])try:for _ in range(50):