fix: add CWD existence guard in LocalShell._run_bash() to prevent terminal bricking on deleted directories#17707
Closed
alexwangzheng74 wants to merge 1 commit into
Closed
Conversation
When the session's CWD is deleted mid-session (e.g. on a project dir), subprocess.Popen(cwd=deleted_dir) fails with FileNotFoundError before the shell starts, permanently bricking terminal and execute_code tools. Add a guard that checks os.path.isdir(self.cwd) before spawning the shell and falls back to /Users/alexwang if the directory no longer exists. This allows the agent to recover without a restart.
Collaborator
|
Likely duplicate of #17569 — same root cause (deleted CWD breaks Popen in _run_bash) and same fallback-to-safe-directory fix. |
Collaborator
|
Likely duplicate of #17569 — same root cause: LocalEnvironment._run_bash raises FileNotFoundError when self.cwd is deleted. Both PRs add a fallback guard before Popen. |
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.
Problem
When a session's working directory is deleted mid-session (e.g.
rm -rf ~/projects/some-projectfrom one command), subsequentterminal()andexecute_code()calls permanently fail for the rest of the session.Root cause:
local._run_bash()callssubprocess.Popen(..., cwd=self.cwd). Whenself.cwdpoints to a deleted directory,PopenthrowsFileNotFoundErrorbefore the shell even starts — thebuiltin cd || exit 126wrapper in_wrap_command()never gets a chance to run. Every subsequent tool call hits the same error, and the only recovery is a full agent restart.Fix
Add an
os.path.isdir(self.cwd)check in_run_bash()right beforesubprocess.Popen. If the directory no longer exists, fall back to$HOMEand log a warning. This lets the agent recover gracefully without a restart.12-line change with a comment explaining the edge case. Zero impact on normal operation — the check is a single
os.path.isdir()call that passes almost instantly.Testing
Popen(cwd=deleted)raisesFileNotFoundError, and fallback to$HOMEsucceedstest_wait_for_process_kills_subprocess_on_keyboardinterrupt— unrelated drain thread race)os.path.isdir()on a valid directory returns in microseconds