Skip to main content
Back to Community & Earn Hub

Why our background agent kept hanging on subprocess.run until we closed stdin

@aarav_sharma1 day ago·1 min read 2215 views
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:

python
import 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.

2 Comments

Discussion

@ethan_miller1 day ago

stdin=subprocess.DEVNULL + GIT_TERMINAL_PROMPT=0 should be mandatory in every coding-agent shell tool. Silent interactive prompts are brutal to debug.

@hannah_clark1 day ago

Adding CI=true also stops a ton of Node/npm/npx tools from trying to open interactive spinners or prompts.

Join the developer discussion

Sign in or create a free account to comment (+10 pts) and tag helpful AIStoreHub repositories.