FoundationAgents/OpenManus · error · RuntimeError

Session not initialized

Error message

Session not initialized

What it means

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.

Source

Thrown at app/sandbox/core/terminal.py:154

                raise
        return buffer.decode("utf-8")

    async def execute(self, command: str, timeout: Optional[int] = None) -> str:
        """Executes a command and returns cleaned output.

        Args:
            command: Shell command to execute.
            timeout: Maximum execution time in seconds.

        Returns:
            Command output as string with prompt markers removed.

        Raises:
            RuntimeError: If session not initialized or execution fails.
            TimeoutError: If command execution exceeds timeout.
        """
        if not self.socket:
            raise RuntimeError("Session not initialized")

        try:
            # Sanitize command to prevent shell injection
            sanitized_command = self._sanitize_command(command)
            full_command = f"{sanitized_command}\necho $?\n"
            self.socket.sendall(full_command.encode())

            async def read_output() -> str:
                buffer = b""
                result_lines = []
                command_sent = False

                while True:
                    try:
                        chunk = self.socket.recv(4096)
                        if not chunk:
                            break

View on GitHub (pinned to 52a13f2a57)

Solutions

  1. Use the AsyncDockerizedTerminal wrapper: 'async with AsyncDockerizedTerminal(container) as term' — its __aenter__ calls init() for you.
  2. If using DockerSession directly, always 'await session.create(workdir, env)' before any execute() call.
  3. After any exception during create(), discard the session object and build a new one rather than retrying execute() on it.
  4. Guard concurrent access with a single owner per session; do not call execute() after close().

Example fix

# before
session = DockerSession(container.id)
await session.execute("ls")  # RuntimeError: Session not initialized

# after
session = DockerSession(container.id)
await session.create("/workspace", {"FOO": "bar"})
await session.execute("ls")
Defensive patterns

Strategy: validation

Validate before calling

if session.socket is None:
    await session.create(workdir, env_vars)
assert session.socket is not None

Type guard

def session_ready(s: DockerSession) -> bool:
    return s.socket is not None and s.exec_id is not None

Try / catch

try:
    out = await session.execute(cmd)
except RuntimeError as e:
    if 'Session not initialized' in str(e):
        await session.create(workdir, env_vars)
        out = await session.execute(cmd)
    else:
        raise

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of FoundationAgents/OpenManus@52a13f2a57 (2026-08-15). Data as JSON: /api/errors/0c01931a397c42db. Report an issue: GitHub.