Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions tests/tools/test_terminal_cleanup_cwd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""Regression test for #33367: _get_env_config should not raise when CWD is deleted."""

import os
from unittest.mock import patch


def test_get_env_config_handles_deleted_cwd(monkeypatch):
"""When os.getcwd() raises FileNotFoundError (e.g. tmpfs cleanup on Arch
Linux), _get_env_config should fall back to the home directory instead of
propagating the error. See #33367."""
from tools import terminal_tool as tt

def _raise_fnf(*a, **kw):
raise FileNotFoundError("No such file or directory")

monkeypatch.setattr(os, "getcwd", _raise_fnf)
monkeypatch.setenv("TERMINAL_ENV", "local")

config = tt._get_env_config()

assert config["env_type"] == "local"
assert config["cwd"] == os.path.expanduser("~")


def test_get_env_config_handles_oserror_on_cwd(monkeypatch):
"""os.getcwd() can also raise OSError (errno 10: no current directory)."""
from tools import terminal_tool as tt

def _raise_os(*a, **kw):
raise OSError(10, "No current process")

monkeypatch.setattr(os, "getcwd", _raise_os)
monkeypatch.setenv("TERMINAL_ENV", "local")

config = tt._get_env_config()

assert config["env_type"] == "local"
assert config["cwd"] == os.path.expanduser("~")
8 changes: 7 additions & 1 deletion tools/terminal_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -953,7 +953,13 @@ def _get_env_config() -> Dict[str, Any]:
# remote home, and everything else starts in the backend's default
# root-like cwd.
if env_type == "local":
default_cwd = os.getcwd()
try:
default_cwd = os.getcwd()
except (FileNotFoundError, OSError):
# CWD may have been deleted (e.g. tmpfs cleanup on Arch Linux).
# Fall back to the home directory so the cleanup thread does not
# spam errors.log every 60 seconds. See #33367.
default_cwd = os.path.expanduser("~")
elif env_type == "ssh":
default_cwd = "~"
else:
Expand Down
Loading