From 83043e9aa8368f29cf9015b2b086fefea7c70388 Mon Sep 17 00:00:00 2001 From: Teknium <127238744+teknium1@users.noreply.github.com> Date: Fri, 27 Mar 2026 17:51:14 -0700 Subject: [PATCH] fix: add timeout to subprocess calls in context_references (#3469) _expand_git_reference() and _rg_files() called subprocess.run() without a timeout. On a large repository, @diff, @staged, or @git:N references could hang the agent indefinitely while git or ripgrep processes slow output. - Add timeout=30 to git subprocess in _expand_git_reference() with a user-friendly error message on TimeoutExpired - Add timeout=10 to rg subprocess in _rg_files() returning None on timeout (falls back to os.walk folder listing) Co-authored-by: memosr.eth <96793918+memosr@users.noreply.github.com> --- agent/context_references.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/agent/context_references.py b/agent/context_references.py index 795e37c6..09ba982d 100644 --- a/agent/context_references.py +++ b/agent/context_references.py @@ -286,12 +286,16 @@ def _expand_git_reference( args: list[str], label: str, ) -> tuple[str | None, str | None]: - result = subprocess.run( - ["git", *args], - cwd=cwd, - capture_output=True, - text=True, - ) + try: + result = subprocess.run( + ["git", *args], + cwd=cwd, + capture_output=True, + text=True, + timeout=30, + ) + except subprocess.TimeoutExpired: + return f"{ref.raw}: git command timed out (30s)", None if result.returncode != 0: stderr = (result.stderr or "").strip() or "git command failed" return f"{ref.raw}: {stderr}", None @@ -449,9 +453,12 @@ def _rg_files(path: Path, cwd: Path, limit: int) -> list[Path] | None: cwd=cwd, capture_output=True, text=True, + timeout=10, ) except FileNotFoundError: return None + except subprocess.TimeoutExpired: + return None if result.returncode != 0: return None files = [Path(line.strip()) for line in result.stdout.splitlines() if line.strip()]