Skip to content

fix(transport): handle missing or unreadable home directories in _find_cli - #1553

Open
adityajai119 wants to merge 1 commit into
anthropics:mainfrom
adityajai119:fix/cli-fallback-lookup
Open

adityajai119 wants to merge 1 commit into
anthropics:mainfrom
adityajai119:fix/cli-fallback-lookup

Conversation

@adityajai119

Copy link
Copy Markdown

Fixes #1552

What was wrong

When no bundled CLI binary is present and the PATH lookup in SubprocessCLITransport._find_cli() misses, fallback CLI discovery fails in headless, service, or restricted environments:

  • When Path.home() raises RuntimeError (such as a rootless container without HOME or /etc/passwd entry, or a Windows service account without USERPROFILE / HOMEDRIVE), POSIX discovery aborts at Path.home() / ".npm-global/bin/claude" before checking /usr/local/bin/claude, and both Windows and POSIX leak raw RuntimeError out of connect() instead of raising CLINotFoundError.
  • When USERPROFILE="" on Windows (or HOME is relative), Path.home() returns a non-absolute Path('.'), probing .local/bin/claude.exe relative to the working directory.
  • When a fallback parent directory cannot be stat'd (PermissionError on Python 3.10/3.11 or OSError), path.exists() and path.is_file() aborts before checking remaining fallback locations (such as /usr/local/bin/claude) and leaks PermissionError out of connect() instead of raising CLINotFoundError.

Root cause

SubprocessCLITransport._find_cli() evaluates Path.home() unconditionally when building the fallback locations list and iterates locations without guarding OSError, while connect() invokes _find_cli() before its try block.

Fix

  • Resolve Path.home() via a helper that catches (RuntimeError, OSError) and requires home.is_absolute(), omitting home-relative fallback candidates when unavailable while still probing /usr/local/bin/claude on POSIX.
  • Wrap if path.exists() and path.is_file(): in with suppress(OSError): so unreadable fallback directories are skipped and discovery either continues to remaining candidates or raises CLINotFoundError.

Testing

  • .venv\Scripts\python.exe -m pytest tests/test_transport.py -k "CliLookup or PosixCliFallbackLookup" (6 new tests failed before the fix, 13 passed after)
  • .venv\Scripts\python.exe -m pytest tests/ (1746 passed, 21 skipped)
  • .venv\Scripts\python.exe -m ruff check src/ tests/ scripts/
  • .venv\Scripts\python.exe -m ruff format --check src/ tests/ scripts/
  • .venv\Scripts\python.exe -m mypy src/ scripts/

AI-assisted: drafted with an AI coding agent; the tests above were run locally.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

_find_cli() leaks RuntimeError / PermissionError and skips /usr/local/bin/claude when home directory is missing or unreadable

1 participant