Skip to content

fix(python-sdk): resolve LocalServer binary from custom env when binary is None - #60

Open
Pybsama wants to merge 2 commits into
Graphicle-AI:mainfrom
Pybsama:fix/python-sdk-local-server-env-binary
Open

Pybsama wants to merge 2 commits into
Graphicle-AI:mainfrom
Pybsama:fix/python-sdk-local-server-env-binary

Conversation

@Pybsama

@Pybsama Pybsama commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

Summary

LocalServer.__init__ already computed search_path = env.get("PATH", os.defpath) if env is not None else None for resolving the executable, but when binary was None, it ran os.environ.get("GRAPHICLE_BIN") or shutil.which("graphicle") against the host process environment first. As a result:

  • Passing LocalServer(None, data_dir, env={"PATH": custom_bin}) raised FileNotFoundError when graphicle was only in custom_bin, or selected a host graphicle instead of the one in custom_bin.
  • Passing LocalServer(None, data_dir, env={"GRAPHICLE_BIN": custom_exe}) ignored env["GRAPHICLE_BIN"].
  • Passing an isolated env leaked the host process's GRAPHICLE_BIN.

This change computes search_path and env_bin from env when env is not None (falling back to os.environ only when env is None), and resolves shutil.which("graphicle", path=search_path).

Verification

  • Ran python3 -B -m unittest discover -s tests -v in sdk/python: 15/15 OK (including 6 new tests in sdk/python/tests/test_server.py covering custom env["PATH"], custom env["GRAPHICLE_BIN"], empty GRAPHICLE_BIN fallback, isolated env non-leakage, and env=None default behavior).
  • Ran python3 -B -m py_compile graphicle_sdk/server.py tests/test_server.py and git diff --check: clean.

…ry is None

LocalServer.__init__ already computed search_path from the caller-supplied env mapping, but resolved binary=None against os.environ['GRAPHICLE_BIN'] and the host PATH first. That raised FileNotFoundError when graphicle only existed on env['PATH'], preferred a host binary over env['PATH']/env['GRAPHICLE_BIN'], and leaked host GRAPHICLE_BIN into an isolated env.

Compute search_path and env_bin from env when env is provided (falling back to os.environ only when env is None) before resolving binary=None, and add unit tests in sdk/python/tests/test_server.py.

Signed-off-by: sama Pyb <peiyibopybsama@gmail.com>

@VikramLex VikramLex left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this, and for the tests: run against main's server.py, five of the six new tests fail, so they pin the bug well (all 15 SDK tests pass at this head on Python 3.10 and 3.13).

One change before we merge. With this version, a caller who passes an env without PATH (for example env={"HOME": tmp} to keep the server's state separate) and has graphicle on the host PATH, or in the host's GRAPHICLE_BIN, now gets FileNotFoundError: the lookup falls back to os.defpath (/bin:/usr/bin, or .;C:\bin on Windows, which also means the current folder). On main that call worked, because the binary was resolved to an absolute path from the host environment and the child never needed PATH.

Could the lookup use env only when it actually says where the binary is, and keep the host lookup otherwise? Something like:

from_env = env is not None and ("GRAPHICLE_BIN" in env or "PATH" in env)
search_path = env.get("PATH", "") if from_env else None
env_bin = env.get("GRAPHICLE_BIN") if from_env else os.environ.get("GRAPHICLE_BIN")
resolved = binary or env_bin or shutil.which("graphicle", path=search_path)

Your six tests should still hold with that (the isolated case passes PATH, so it stays isolated). Please add one test for env={"HOME": ...} with graphicle only on the host PATH, and, if you like, make the FileNotFoundError message say "include GRAPHICLE_BIN or PATH in env" when env was given.

…and GRAPHICLE_BIN

Only restrict binary resolution to caller-supplied env when env actually
specifies GRAPHICLE_BIN or PATH (matched case-insensitively on Windows).
Callers passing unrelated variables such as HOME retain host PATH /
GRAPHICLE_BIN lookup, explicit binary= retains highest priority, and
explicitly setting PATH or GRAPHICLE_BIN in env stays isolated without
falling back to os.defpath.

Include env hint in FileNotFoundError when env is supplied, and add
regression tests covering HOME-only env, host GRAPHICLE_BIN fallback,
explicit binary precedence, empty GRAPHICLE_BIN isolation, and
platform case sensitivity.

Signed-off-by: sama Pyb <peiyibopybsama@gmail.com>
@Pybsama

Pybsama commented Dec 5, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review @VikramLex — updated in 26c5cc9:

  • Restricted custom-env binary resolution to when env explicitly contains GRAPHICLE_BIN or PATH (case-insensitive on Windows via _lookup_env), defaulting missing PATH in that branch to "" instead of os.defpath.
  • Preserved host GRAPHICLE_BIN / PATH fallback when env only passes unrelated keys (such as HOME), and preserved explicit binary= precedence.
  • Updated the FileNotFoundError message to suggest including GRAPHICLE_BIN or PATH in env when env is supplied.
  • Added regression tests in sdk/python/tests/test_server.py for env={"HOME": ...} with graphicle only on host PATH (and host GRAPHICLE_BIN), explicit binary= precedence, isolated env={"GRAPHICLE_BIN": ""}, and platform case sensitivity (all 20 unittest tests + test_tools passing on Python 3.11, 3.13, and 3.14).

Ready for another look.

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.

2 participants