{"record":{"id":"bc5b900193607bce","repo":"FoundationAgents/OpenManus","slug":"failed-to-create-working-directory-e","errorCode":null,"errorMessage":"Failed to create working directory: {e}","messagePattern":"Failed to create working directory: (.+?)","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"app/sandbox/core/terminal.py","lineNumber":300,"sourceCode":"\n        Raises:\n            RuntimeError: If initialization fails.\n        \"\"\"\n        await self._ensure_workdir()\n\n        self.session = DockerSession(self.container.id)\n        await self.session.create(self.working_dir, self.env_vars)\n\n    async def _ensure_workdir(self) -> None:\n        \"\"\"Ensures working directory exists in container.\n\n        Raises:\n            RuntimeError: If directory creation fails.\n        \"\"\"\n        try:\n            await self._exec_simple(f\"mkdir -p {self.working_dir}\")\n        except APIError as e:\n            raise RuntimeError(f\"Failed to create working directory: {e}\")\n\n    async def _exec_simple(self, cmd: str) -> Tuple[int, str]:\n        \"\"\"Executes a simple command using Docker's exec_run.\n\n        Args:\n            cmd: Command to execute.\n\n        Returns:\n            Tuple of (exit_code, output).\n        \"\"\"\n        result = await asyncio.to_thread(\n            self.container.exec_run, cmd, environment=self.env_vars\n        )\n        return result.exit_code, result.output.decode(\"utf-8\")\n\n    async def run_command(self, cmd: str, timeout: Optional[int] = None) -> str:\n        \"\"\"Runs a command in the container with timeout.\n","sourceCodeStart":282,"sourceCodeEnd":318,"githubUrl":"https://github.com/FoundationAgents/OpenManus/blob/52a13f2a57d8c7f6737eefb02ccf569594d44273/app/sandbox/core/terminal.py#L282-L318","documentation":"AsyncDockerizedTerminal._ensure_workdir runs 'mkdir -p {working_dir}' inside the container via exec_run and wraps any docker APIError in this RuntimeError. mkdir -p rarely fails on its own, so the wrapped APIError usually indicates the exec itself failed: container not running, image lacks a shell (/bin/sh), permission denied at the parent, or an invalid working_dir string.","triggerScenarios":"init() called on a stopped/removed container; working_dir containing characters that break the shell line (spaces unquoted, leading -); minimal images (distroless/scratch) with no mkdir binary; read-only filesystem at the target.","commonSituations":"Container TTL expired before terminal init; distroless images used for sandboxing; working_dir like '/my workspace' passed unquoted; container root filesystem mounted read-only.","solutions":["Check container.status == 'running' (after container.reload()) before creating the terminal.","Quote the path and validate it: f'mkdir -p \"{self.working_dir}\"' and reject paths with shell metacharacters at construction.","For minimal images, use a base image with coreutils, or create the directory at image build time (WORKDIR /workspace in the Dockerfile).","Read the embedded APIError — 404 means container gone (recreate it), 126/127 means missing binary or permission issue in the image."],"exampleFix":"# before\nawait self._exec_simple(f\"mkdir -p {self.working_dir}\")\n\n# after\nawait self._exec_simple(f'mkdir -p \"{self.working_dir}\"')\n# plus, at the caller:\ncontainer.reload()\nif container.status != \"running\":\n    raise RuntimeError(f\"Container {container.id} is {container.status}; cannot init terminal\")","handlingStrategy":"try-catch","validationCode":"container.reload()\nif container.status != 'running':\n    raise RuntimeError(f'cannot init terminal: container is {container.status}')\nimport re\nif re.search(r\"[\\s'\\\";&|]\", working_dir.strip('/').split('/')[-1]):\n    raise ValueError(f'unsafe working_dir: {working_dir!r}')","typeGuard":null,"tryCatchPattern":"try:\n    async with AsyncDockerizedTerminal(container, working_dir='/workspace') as term:\n        ...\nexcept RuntimeError as e:\n    if 'Failed to create working directory' in str(e):\n        raise RuntimeError('terminal init failed (container dead, read-only fs, or bad path)') from e\n    raise","preventionTips":["Use a base image with coreutils; create WORKDIR in the Dockerfile.","Quote the working_dir in the mkdir command.","Treat init failure as fatal for the terminal — rebuild it."],"tags":["docker","working-directory","container-lifecycle"],"backgroundTag":null,"analyzedSha":"52a13f2a57d8c7f6737eefb02ccf569594d44273","analyzedAt":"2026-08-15T02:33:49.993Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}