{"record":{"id":"0c01931a397c42db","repo":"FoundationAgents/OpenManus","slug":"session-not-initialized","errorCode":null,"errorMessage":"Session not initialized","messagePattern":"Session not initialized","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"app/sandbox/core/terminal.py","lineNumber":154,"sourceCode":"                raise\n        return buffer.decode(\"utf-8\")\n\n    async def execute(self, command: str, timeout: Optional[int] = None) -> str:\n        \"\"\"Executes a command and returns cleaned output.\n\n        Args:\n            command: Shell command to execute.\n            timeout: Maximum execution time in seconds.\n\n        Returns:\n            Command output as string with prompt markers removed.\n\n        Raises:\n            RuntimeError: If session not initialized or execution fails.\n            TimeoutError: If command execution exceeds timeout.\n        \"\"\"\n        if not self.socket:\n            raise RuntimeError(\"Session not initialized\")\n\n        try:\n            # Sanitize command to prevent shell injection\n            sanitized_command = self._sanitize_command(command)\n            full_command = f\"{sanitized_command}\\necho $?\\n\"\n            self.socket.sendall(full_command.encode())\n\n            async def read_output() -> str:\n                buffer = b\"\"\n                result_lines = []\n                command_sent = False\n\n                while True:\n                    try:\n                        chunk = self.socket.recv(4096)\n                        if not chunk:\n                            break\n","sourceCodeStart":136,"sourceCodeEnd":172,"githubUrl":"https://github.com/FoundationAgents/OpenManus/blob/52a13f2a57d8c7f6737eefb02ccf569594d44273/app/sandbox/core/terminal.py#L136-L172","documentation":"DockerSession.execute() requires an attached exec socket; if self.socket is None (create() never ran, or it failed before the socket was grabbed), the command is rejected immediately. This is a lifecycle guard: the session must be created (container exec started, socket connected) before any command can be sent.","triggerScenarios":"Calling execute() before create(), calling it after a previous failure in create() (e.g. the socket-connection error at line 71 left the session half-built), or calling it after close() set the socket to None.","commonSituations":"Using DockerSession directly instead of AsyncDockerizedTerminal and skipping the create step; re-using a session object whose create() raised earlier; racing concurrent callers where one closed the session while another issued a command.","solutions":["Use the AsyncDockerizedTerminal wrapper: 'async with AsyncDockerizedTerminal(container) as term' — its __aenter__ calls init() for you.","If using DockerSession directly, always 'await session.create(workdir, env)' before any execute() call.","After any exception during create(), discard the session object and build a new one rather than retrying execute() on it.","Guard concurrent access with a single owner per session; do not call execute() after close()."],"exampleFix":"# before\nsession = DockerSession(container.id)\nawait session.execute(\"ls\")  # RuntimeError: Session not initialized\n\n# after\nsession = DockerSession(container.id)\nawait session.create(\"/workspace\", {\"FOO\": \"bar\"})\nawait session.execute(\"ls\")","handlingStrategy":"validation","validationCode":"if session.socket is None:\n    await session.create(workdir, env_vars)\nassert session.socket is not None","typeGuard":"def session_ready(s: DockerSession) -> bool:\n    return s.socket is not None and s.exec_id is not None","tryCatchPattern":"try:\n    out = await session.execute(cmd)\nexcept RuntimeError as e:\n    if 'Session not initialized' in str(e):\n        await session.create(workdir, env_vars)\n        out = await session.execute(cmd)\n    else:\n        raise","preventionTips":["Always create() before execute(); prefer AsyncDockerizedTerminal's context manager.","Discard session objects whose create() raised.","One owner coroutine per session; no execute() after close()."],"tags":["terminal","lifecycle","docker"],"backgroundTag":null,"analyzedSha":"52a13f2a57d8c7f6737eefb02ccf569594d44273","analyzedAt":"2026-08-15T02:33:49.993Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}