Skip to content

fix: guard os.getcwd() against deleted CWD (FileNotFoundError) - #37558

Closed
isair wants to merge 1 commit into
NousResearch:mainfrom
isair:fix/terminal-cwd-deleted-guard
Closed

fix: guard os.getcwd() against deleted CWD (FileNotFoundError)#37558
isair wants to merge 1 commit into
NousResearch:mainfrom
isair:fix/terminal-cwd-deleted-guard

Conversation

@isair

@isair isair commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

When a scratch workspace is cleaned up while a terminal session is active, os.getcwd() raises FileNotFoundError and the tool crashes. Wrap it in try/except and fall back to TERMINAL_CWD env var or the user's home directory.

This is a common edge case in CI, ephemeral containers, and workspace-tooling environments where directories may be deleted out from under a running process.

When a scratch workspace is cleaned up while a terminal session
is active, os.getcwd() raises FileNotFoundError and the tool
crashes. Wrap it in try/except and fall back to TERMINAL_CWD
env var or the user's home directory.

This is a common edge case in CI, ephemeral containers, and
workspace-tooling environments where directories may be
deleted out from under a running process.
@alt-glitch

Copy link
Copy Markdown
Collaborator

Duplicate of #33377 — same fix at the same call site (os.getcwd() guard in _get_env_config() in tools/terminal_tool.py, FileNotFoundError fallback when CWD is deleted). #33377 is the open canonical PR; #33407 was a closed earlier copy. Consolidating there.

@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists comp/tools Tool registry, model_tools, toolsets tool/terminal Terminal execution and process management labels Jun 2, 2026
@Morad37

Morad37 commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

One more spot to cover: the Docker mount path still calls os.getcwd() in the docker_mount_cwd_to_workspace branch when TERMINAL_CWD is unset.

So this fixes env_type=local, but env_type=docker with TERMINAL_DOCKER_MOUNT_CWD_TO_WORKSPACE=true can still hit the same FileNotFoundError if the process CWD was deleted. Reusing the guarded helper there would close the same edge case for mounted workspaces too.

@teknium1

teknium1 commented Jun 5, 2026

Copy link
Copy Markdown
Contributor

Merged via #39491 (commit ad69d3e) with your authorship preserved via rebase. Your deleted-CWD guard landed, refactored into a _safe_getcwd() helper applied to all three os.getcwd() sites in the file (local default, Docker cwd-passthrough, debug print) so the same crash cant resurface at a sibling. Thanks!

@teknium1 teknium1 closed this Jun 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/tools Tool registry, model_tools, toolsets 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.

4 participants