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>
This commit is contained in:
@@ -286,12 +286,16 @@ def _expand_git_reference(
|
|||||||
args: list[str],
|
args: list[str],
|
||||||
label: str,
|
label: str,
|
||||||
) -> tuple[str | None, str | None]:
|
) -> tuple[str | None, str | None]:
|
||||||
|
try:
|
||||||
result = subprocess.run(
|
result = subprocess.run(
|
||||||
["git", *args],
|
["git", *args],
|
||||||
cwd=cwd,
|
cwd=cwd,
|
||||||
capture_output=True,
|
capture_output=True,
|
||||||
text=True,
|
text=True,
|
||||||
|
timeout=30,
|
||||||
)
|
)
|
||||||
|
except subprocess.TimeoutExpired:
|
||||||
|
return f"{ref.raw}: git command timed out (30s)", None
|
||||||
if result.returncode != 0:
|
if result.returncode != 0:
|
||||||
stderr = (result.stderr or "").strip() or "git command failed"
|
stderr = (result.stderr or "").strip() or "git command failed"
|
||||||
return f"{ref.raw}: {stderr}", None
|
return f"{ref.raw}: {stderr}", None
|
||||||
@@ -449,9 +453,12 @@ def _rg_files(path: Path, cwd: Path, limit: int) -> list[Path] | None:
|
|||||||
cwd=cwd,
|
cwd=cwd,
|
||||||
capture_output=True,
|
capture_output=True,
|
||||||
text=True,
|
text=True,
|
||||||
|
timeout=10,
|
||||||
)
|
)
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
return None
|
return None
|
||||||
|
except subprocess.TimeoutExpired:
|
||||||
|
return None
|
||||||
if result.returncode != 0:
|
if result.returncode != 0:
|
||||||
return None
|
return None
|
||||||
files = [Path(line.strip()) for line in result.stdout.splitlines() if line.strip()]
|
files = [Path(line.strip()) for line in result.stdout.splitlines() if line.strip()]
|
||||||
|
|||||||
Reference in New Issue
Block a user