{"record":{"id":"e316312e653fa20d","repo":"FoundationAgents/OpenManus","slug":"sandbox-not-initialized","errorCode":null,"errorMessage":"Sandbox not initialized","messagePattern":"Sandbox not initialized","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"app/sandbox/core/sandbox.py","lineNumber":155,"sourceCode":"        os.makedirs(host_path, exist_ok=True)\n        return host_path\n\n    async def run_command(self, cmd: str, timeout: Optional[int] = None) -> str:\n        \"\"\"Runs a command in the sandbox.\n\n        Args:\n            cmd: Command to execute.\n            timeout: Timeout in seconds.\n\n        Returns:\n            Command output as string.\n\n        Raises:\n            RuntimeError: If sandbox not initialized or command execution fails.\n            TimeoutError: If command execution times out.\n        \"\"\"\n        if not self.terminal:\n            raise RuntimeError(\"Sandbox not initialized\")\n\n        try:\n            return await self.terminal.run_command(\n                cmd, timeout=timeout or self.config.timeout\n            )\n        except TimeoutError:\n            raise SandboxTimeoutError(\n                f\"Command execution timed out after {timeout or self.config.timeout} seconds\"\n            )\n\n    async def read_file(self, path: str) -> str:\n        \"\"\"Reads a file from the container.\n\n        Args:\n            path: File path.\n\n        Returns:\n            File contents as string.","sourceCodeStart":137,"sourceCodeEnd":173,"githubUrl":"https://github.com/FoundationAgents/OpenManus/blob/52a13f2a57d8c7f6737eefb02ccf569594d44273/app/sandbox/core/sandbox.py#L137-L173","documentation":"Raised by DockerSandbox.run_command (app/sandbox/core/sandbox.py:155) when self.terminal is None — i.e. the command API was used before a successful create()/init(). The sandbox object can be constructed cheaply, but the exec terminal only exists after DockerSandbox.create() completes; calling run_command before that is a lifecycle misuse, not a Docker failure.","triggerScenarios":"Constructing DockerSandbox(config) directly and calling run_command without `await sandbox.create()`; using a sandbox after cleanup() set terminal to None; a create() that failed (error 24) leaving the object alive and later reused.","commonSituations":"Skipping the manager and hand-rolling the sandbox lifecycle; forgetting to await the create coroutine (calling create() without await so it never runs); reusing a sandbox reference after an exception path already cleaned it up.","solutions":["Call `sandbox = await DockerSandbox(config).create()` (or manager.create_sandbox + get_sandbox) before any run_command.","After any create/cleanup failure, drop the reference — do not retry commands on the same object.","If using the manager, always go through get_sandbox(sandbox_id) which only returns initialized sandboxes."],"exampleFix":"# before\nsb = DockerSandbox(config)\nout = await sb.run_command(\"ls\")  # RuntimeError: Sandbox not initialized\n\n# after\nsb = await DockerSandbox(config).create()\nout = await sb.run_command(\"ls\")","handlingStrategy":"type-guard","validationCode":"assert sandbox.terminal is not None, \"call await sandbox.create() before run_command\"","typeGuard":"def is_ready(sb: DockerSandbox) -> bool:\n    return sb.terminal is not None","tryCatchPattern":"try:\n    out = await sandbox.run_command(cmd)\nexcept RuntimeError as e:\n    if str(e) == \"Sandbox not initialized\":\n        sandbox = await DockerSandbox(sandbox.config).create()\n        out = await sandbox.run_command(cmd)\n    else:\n        raise","preventionTips":["Always obtain sandboxes via SandboxManager.get_sandbox(sid)","Await create() fully before any command","Drop references after cleanup() or a failed create()","Wrap sandbox usage in a context helper that guarantees init order"],"tags":["sandbox","lifecycle","initialization","api-misuse"],"backgroundTag":null,"analyzedSha":"52a13f2a57d8c7f6737eefb02ccf569594d44273","analyzedAt":"2026-08-15T02:33:49.993Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}