Skip to content

fix(terminal): guard os.getcwd() against a deleted CWD - #39491

Merged
teknium1 merged 1 commit into
mainfrom
salvage/37558-getcwd-guard
Jun 5, 2026
Merged

fix(terminal): guard os.getcwd() against a deleted CWD#39491
teknium1 merged 1 commit into
mainfrom
salvage/37558-getcwd-guard

Conversation

@teknium1

@teknium1 teknium1 commented Jun 5, 2026

Copy link
Copy Markdown
Contributor

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.py called bare os.getcwd() to compute the default cwd. If the CWD was removed mid-session (e.g. a scratch workspace cleaned up), os.getcwd() raises FileNotFoundError, crashing _get_env_config() before any command can run.

Changes

  • tools/terminal_tool.py: add _safe_getcwd() — returns os.getcwd(), falling back to TERMINAL_CWD then ~ on FileNotFoundError. Route all three os.getcwd() call sites through it (local default_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_cwd site. The Docker cwd-passthrough computed os.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

Before After
os.getcwd() with deleted CWD FileNotFoundError crash falls back to TERMINAL_CWD / ~
-k safe_getcwd 3 passed
Real deleted-CWD E2E (rmdir + verify) passed

`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>
@github-actions

github-actions Bot commented Jun 5, 2026

Copy link
Copy Markdown
Contributor

🔎 Lint report: salvage/37558-getcwd-guard vs origin/main

ruff

Total: 0 on HEAD, 0 on base (➖ 0)

🆕 New issues: none

✅ Fixed issues: none

Unchanged: 0 pre-existing issues carried over.

ty (type checker)

Total: 9850 on HEAD, 9850 on base (➖ 0)

🆕 New issues: none

✅ Fixed issues: none

Unchanged: 5107 pre-existing issues carried over.

Diagnostics are surfaced as warnings — this check never fails the build.

@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists tool/terminal Terminal execution and process management labels Jun 5, 2026
@teknium1
teknium1 merged commit ad69d3e into main Jun 5, 2026
23 checks passed
@teknium1
teknium1 deleted the salvage/37558-getcwd-guard branch June 5, 2026 06:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P2 Medium — degraded but workaround exists tool/terminal Terminal execution and process management type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants