fix(terminal): guard os.getcwd() against a deleted CWD - #39491
Merged
Conversation
`os.getcwd()` raises FileNotFoundError when the process's working directory was removed out from under it (e.g. a scratch workspace cleaned up mid-session), crashing terminal env setup. Extract a `_safe_getcwd()` helper that falls back to TERMINAL_CWD, then the user's home, on FileNotFoundError, and route all three `os.getcwd()` call sites in terminal_tool.py through it (local default_cwd, the Docker cwd-passthrough source, and the debug-config print) so the same crash can't resurface at a sibling site. Adds unit tests for the real-cwd path and both fallback branches. Co-authored-by: Teknium <127238744+teknium1@users.noreply.github.com>
Contributor
🔎 Lint report:
|
12 tasks
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
Terminal environment setup no longer crashes when the process's working directory was deleted out from under it.
Salvages #37558 (@isair), refactored into a shared helper and applied to all sites.
Root cause
tools/terminal_tool.pycalled bareos.getcwd()to compute the default cwd. If the CWD was removed mid-session (e.g. a scratch workspace cleaned up),os.getcwd()raisesFileNotFoundError, crashing_get_env_config()before any command can run.Changes
tools/terminal_tool.py: add_safe_getcwd()— returnsos.getcwd(), falling back toTERMINAL_CWDthen~onFileNotFoundError. Route all threeos.getcwd()call sites through it (localdefault_cwd, the Docker cwd-passthrough source at L1061, the debug-config print).tests/tools/test_terminal_task_cwd.py: tests for the real-cwd path and both fallback branches.Why wider than the original
@isair's PR fixed the local
default_cwdsite. The Docker cwd-passthrough computedos.getenv("TERMINAL_CWD") or os.getcwd()with the identical crash, and the debug print had a third. Extracting one helper fixes all three and keeps the behavior consistent rather than inlining the same try/except repeatedly.Validation
os.getcwd()with deleted CWDFileNotFoundErrorcrash-k safe_getcwd