Why our background agent kept hanging on subprocess.run until we closed stdin
If your agent executes shell or git tools in a background worker, here is a bug that took us way too long to catch.
Every couple of days, a worker container would just stop responding mid-task. CPU was at 0%, memory was fine, and logs ended right after Running: git pull... or npx ....
Eventually we attached to the container and checked ps aux: the child CLI had popped an interactive prompt (Are you sure you want to continue connecting? or Ok to proceed? (y)) and was sitting there forever waiting for a human to press Enter on stdin.
Always kill interactivity at the subprocess call
Never assume a CLI flag like --yes covers every nested tool it spawns. Lock down stdin and env on every agent subprocess call:
pythonimport os import subprocess def run_agent_cmd(cmd: list[str], cwd: str, timeout_s: int = 45) -> subprocess.CompletedProcess: env = os.environ.copy() env.update({ "GIT_TERMINAL_PROMPT": "0", "CI": "true", "PIP_NO_INPUT": "1", "NPM_CONFIG_YES": "true", }) return subprocess.run( cmd, cwd=cwd, env=env, stdin=subprocess.DEVNULL, # Immediate EOF if anything tries to read stdin capture_output=True, text=True, timeout=timeout_s, )
Passing stdin=subprocess.DEVNULL plus GIT_TERMINAL_PROMPT=0 and a hard timeout turned silent multi-hour hangs into clean, immediate errors the agent can actually read and recover from.