Skip to content

fix(tools): guard os.getcwd() in _get_env_config against missing CWD - #33407

Closed
liuhao1024 wants to merge 1 commit into
NousResearch:mainfrom
liuhao1024:fix/terminal-tool-cleanup-file-not-found
Closed

fix(tools): guard os.getcwd() in _get_env_config against missing CWD#33407
liuhao1024 wants to merge 1 commit into
NousResearch:mainfrom
liuhao1024:fix/terminal-tool-cleanup-file-not-found

Conversation

@liuhao1024

Copy link
Copy Markdown
Contributor

Summary

_get_env_config() in tools/terminal_tool.py calls os.getcwd() without protection. When the process's CWD is removed (e.g. by Arch Linux's aggressive tmpfs cleanup of /tmp), this raises FileNotFoundError. The background cleanup thread calls _get_env_config() every 60 seconds, producing a recurring warning in errors.log that never stops — and the cleanup itself never runs, so stale environments accumulate.

Fix

Guard both os.getcwd() call sites in _get_env_config() with try/except (FileNotFoundError, OSError), falling back to os.path.expanduser("~"):

  1. Line 956 (local default CWD): os.getcwd() → wrapped in try/except
  2. Line 972 (docker mount CWD source): os.getcwd() fallback → wrapped in try/except

Regression test

New test file tests/tools/test_get_env_config_cwd_fallback.py with 5 test cases:

  • test_local_env_getcwd_file_not_found — FileNotFoundError falls back to home
  • test_local_env_getcwd_os_error — OSError falls back to home
  • test_local_env_getcwd_normal — normal case still uses real CWD
  • test_local_env_terminal_cwd_overrides_fallback — explicit TERMINAL_CWD still takes precedence
  • test_docker_mount_cwd_getcwd_file_not_found — docker mount path doesn't crash

All 5 new tests pass. Existing test_parse_env_var.py (12 tests) also passes.

Code Intelligence

  • Analyzed: tools/terminal_tool.py:_get_env_config (callers: 4 — _cleanup_thread_worker, create_terminal_environment, create_file_environment, create_web_environment)
  • Blast radius: LOW — only adds graceful fallback, no behavior change for normal operation
  • Related patterns: _cleanup_thread_worker catches all exceptions at line 1242, but the FileNotFoundError clutters logs every 60s and prevents cleanup from running

Fixes #33367

When the process's current working directory is removed (e.g. by Arch
Linux's aggressive tmpfs cleanup), os.getcwd() raises FileNotFoundError.
The cleanup thread calls _get_env_config() every 60 seconds, so this
produced a recurring warning in errors.log that never stopped.

Guard both os.getcwd() call sites (local default_cwd and docker mount
CWD source) with a try/except that falls back to the user's home
directory when the CWD no longer exists.

Fixes NousResearch#33367
@alt-glitch alt-glitch added type/bug Something isn't working tool/terminal Terminal execution and process management P2 Medium — degraded but workaround exists duplicate This issue or pull request already exists labels May 27, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

Duplicate of #33377 — same fix for same call site (os.getcwd() in _get_env_config()). Both fix #33367. Same author.

@liuhao1024

Copy link
Copy Markdown
Contributor Author

Duplicate of #33377 (same fix for in ). Closing in favor of the earlier PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

duplicate This issue or pull request already exists 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.

[Bug]: terminal_tool cleanup thread raises recurring FileNotFoundError in _get_env_config

2 participants