π OpenHands: AI-Driven Development
Fix Docker container creation and error handling (#280)
Kishore Chitrapu73ea0e580362
diff --git a/opendevin/sandbox/sandbox.py b/opendevin/sandbox/sandbox.pyindex bec2a75584..52d2c3be04 100644--- a/opendevin/sandbox/sandbox.py+++ b/opendevin/sandbox/sandbox.py@@ -1,12 +1,13 @@+import atexitimport os+import selectimport sys-import uuidimport time-import select-import docker-from typing import Tuple, Dict, List+import uuidfrom collections import namedtuple-import atexit+from typing import Dict, List, Tuple++import dockerInputType = namedtuple("InputType", ["content"])OutputType = namedtuple("OutputType", ["content"])@@ -178,7 +179,15 @@ class DockerInteractive:self.closed = Truedef stop_docker_container(self):- docker_client = docker.from_env()++ # Initialize docker client. Throws an exception if Docker is not reachable.+ try:+ docker_client = docker.from_env()+ except docker.errors.DockerException as e:+ print('Please check Docker is running using `docker ps`.')+ print(f"Error! {e}", flush=True)+ raise e+try:container = docker_client.containers.get(self.container_name)container.stop()@@ -194,9 +203,17 @@ class DockerInteractive:passdef restart_docker_container(self):- self.stop_docker_container()- docker_client = docker.from_env()try:+ self.stop_docker_container()+ except docker.errors.DockerException as e:+ print(f"Failed to stop container: {e}")+ raise e++ try:+ # Initialize docker client. Throws an exception if Docker is not reachable.+ docker_client = docker.from_env()++ # start the containerself.container = docker_client.containers.run(self.container_image,command="tail -f /dev/null",@@ -246,9 +263,14 @@ if __name__ == "__main__":)args = parser.parse_args()- docker_interactive = DockerInteractive(- workspace_dir=args.directory,- )+ try:+ docker_interactive = DockerInteractive(+ workspace_dir=args.directory,+ )+ except Exception as e:+ print(f"Failed to start Docker container: {e}")+ sys.exit(1)+print("Interactive Docker container started. Type 'exit' or use Ctrl+C to exit.")bg_cmd = docker_interactive.execute_in_background(diff --git a/opendevin/server/session.py b/opendevin/server/session.pyindex 1852f5bd95..77651753a1 100644--- a/opendevin/server/session.py+++ b/opendevin/server/session.py@@ -1,28 +1,25 @@-import osimport asyncio-from typing import Optional, Dict, Type+import os+from typing import Dict, Optional, Typefrom fastapi import WebSocketDisconnectfrom opendevin.action import (Action,- NullAction,- CmdRunAction,- CmdKillAction,+ AgentFinishAction,+ AgentRecallAction,+ AgentThinkAction,BrowseURLAction,+ CmdKillAction,+ CmdRunAction,FileReadAction,FileWriteAction,- AgentRecallAction,- AgentThinkAction,- AgentFinishAction,+ NullAction,)from opendevin.agent import Agentfrom opendevin.controller import AgentControllerfrom opendevin.llm.llm import LLM-from opendevin.observation import (- Observation,- UserMessageObservation-)+from opendevin.observation import Observation, UserMessageObservation# NOTE: this is a temporary solution - but hopefully we can use Action/Observation throughout the codebaseACTION_TYPE_TO_CLASS: Dict[str, Type[Action]] = {@@ -130,7 +127,12 @@ class Session:llm = LLM(model)AgentCls = Agent.get_cls(agent_cls)self.agent = AgentCls(llm)- self.controller = AgentController(self.agent, workdir=directory, callbacks=[self.on_agent_event])+ try:+ self.controller = AgentController(self.agent, workdir=directory, callbacks=[self.on_agent_event])+ except Exception:+ print("Error creating controller.")+ await self.send_error("Error creating controller. Please check Docker is running using `docker ps`.")+ returnawait self.send({"action": "initialize", "message": "Control loop started."})async def start_task(self, start_event):