fix: sanitize PyInstaller loader env for subprocesses#115
Merged
F16shen merged 2 commits intoAI-Shell-Team:mainfrom Apr 21, 2026
Merged
fix: sanitize PyInstaller loader env for subprocesses#115F16shen merged 2 commits intoAI-Shell-Team:mainfrom
F16shen merged 2 commits intoAI-Shell-Team:mainfrom
Conversation
Reviewer's GuideSanitizes subprocess environments under PyInstaller by centralizing LD_LIBRARY_PATH cleanup and ensuring all shell/script/PTY execution paths use the same sanitized environment, plus tests to validate the behavior. Sequence diagram for sanitized environment in BashExecutor.executesequenceDiagram
actor User
participant BashExecutor
participant EnvironmentManager
participant sanitize_subprocess_loader_env
participant Subprocess
User->>BashExecutor: execute(command)
alt env_manager is present
BashExecutor->>EnvironmentManager: get_subprocess_env()
EnvironmentManager->>sanitize_subprocess_loader_env: sanitize_subprocess_loader_env(exported_vars)
sanitize_subprocess_loader_env-->>EnvironmentManager: sanitized_env
EnvironmentManager-->>BashExecutor: sanitized_env
else env_manager is None
BashExecutor->>sanitize_subprocess_loader_env: sanitize_subprocess_loader_env(os_environ_copy)
sanitize_subprocess_loader_env-->>BashExecutor: sanitized_env
end
BashExecutor->>Subprocess: subprocess_run(command, env=sanitized_env)
Class diagram for environment management and subprocess sanitizationclassDiagram
class EnvironmentManager {
+ get_exported_vars() Dict~str,str~
+ get_subprocess_env() Dict~str,str~
}
class sanitize_subprocess_loader_env {
+ sanitize_subprocess_loader_env(env: Mapping~str,str~) Dict~str,str~
}
class BashExecutor {
- env_manager: EnvironmentManager
+ execute(command: str) CommandResult
}
class PtyExecutor {
- env_manager: EnvironmentManager
+ execute_command_with_pty(command: str) CommandResult
}
class ScriptExecutor {
+ _build_runtime_env(script: Script, session: LLMSession) Dict~str,str~
}
class ShellEditor {
+ _build_theme_env(cwd: str, exit_code: int, mode: str) Dict~str,str~
}
EnvironmentManager ..> sanitize_subprocess_loader_env: calls
BashExecutor ..> EnvironmentManager: uses
BashExecutor ..> sanitize_subprocess_loader_env: calls when no env_manager
PtyExecutor ..> EnvironmentManager: uses
PtyExecutor ..> sanitize_subprocess_loader_env: calls when no env_manager
ScriptExecutor ..> sanitize_subprocess_loader_env: calls
ShellEditor ..> sanitize_subprocess_loader_env: calls
Flow diagram for sanitize_subprocess_loader_env logicflowchart TD
A[Start sanitize_subprocess_loader_env] --> B[Copy source env to sanitized]
B --> C{Running in PyInstaller bundle<br/>getattr sys.frozen and sys._MEIPASS}
C -- No --> Z[Return sanitized]
C -- Yes --> D{LD_LIBRARY_PATH_ORIG set}
D -- Yes --> E{LD_LIBRARY_PATH_ORIG nonempty}
E -- Yes --> F[Set LD_LIBRARY_PATH to LD_LIBRARY_PATH_ORIG]
E -- No --> G[Remove LD_LIBRARY_PATH]
F --> Z
G --> Z
D -- No --> H{LD_LIBRARY_PATH set}
H -- No --> Z
H -- Yes --> I[Resolve real _MEIPASS path]
I --> J[Filter LD_LIBRARY_PATH entries<br/>not under _MEIPASS]
J --> K{Filtered list nonempty}
K -- Yes --> L[Set LD_LIBRARY_PATH to<br/>filtered path list]
K -- No --> M[Remove LD_LIBRARY_PATH]
L --> Z
M --> Z
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Contributor
|
Thanks for the pull request. A maintainer will review it when available. Please keep the PR focused, explain the why in the description, and make sure local checks pass before requesting review. Contribution guide: https://github.com/AI-Shell-Team/aish/blob/main/CONTRIBUTING.md |
Contributor
|
This pull request description looks incomplete. Please update the missing sections below before review. Missing items:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR sanitizes subprocess environments when AISH is running from a PyInstaller bundle.
The change removes bundle-private loader paths before launching child processes, while preserving the user's normal shell environment as much as possible. When PyInstaller provides the original loader path state, the subprocess environment restores that directly; otherwise it falls back to pruning only the extracted bundle path.
Why
System commands launched from the packaged app should not inherit AISH's private runtime loader configuration.
Without this cleanup, subprocesses can observe a modified library search path that differs from the user's real environment. That makes shell execution under the frozen build less predictable and can cause linkage behavior that only exists inside the packaged app.
Testing
/home/lixin/workspace/aishell/aish/.venv/bin/python -m pytest tests/shell/ui/test_shell_editor.py tests/tools/test_bash_executor.py -qSummary by Sourcery
Sanitize subprocess environments to strip PyInstaller-specific loader paths while preserving user environment variables across shell, PTY, and script executors.
Bug Fixes:
Enhancements:
Tests: