Skip to content

πŸ™Œ 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.py
index bec2a75584..52d2c3be04 100644
--- a/opendevin/sandbox/sandbox.py
+++ b/opendevin/sandbox/sandbox.py
@@ -1,12 +1,13 @@
+import atexit
import os
+import select
import sys
-import uuid
import time
-import select
-import docker
-from typing import Tuple, Dict, List
+import uuid
from collections import namedtuple
-import atexit
+from typing import Dict, List, Tuple
+
+import docker
InputType = namedtuple("InputType", ["content"])
OutputType = namedtuple("OutputType", ["content"])
@@ -178,7 +179,15 @@ class DockerInteractive:
self.closed = True
def 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:
pass
def 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 container
self.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.py
index 1852f5bd95..77651753a1 100644
--- a/opendevin/server/session.py
+++ b/opendevin/server/session.py
@@ -1,28 +1,25 @@
-import os
import asyncio
-from typing import Optional, Dict, Type
+import os
+from typing import Dict, Optional, Type
from fastapi import WebSocketDisconnect
from opendevin.action import (
Action,
- NullAction,
- CmdRunAction,
- CmdKillAction,
+ AgentFinishAction,
+ AgentRecallAction,
+ AgentThinkAction,
BrowseURLAction,
+ CmdKillAction,
+ CmdRunAction,
FileReadAction,
FileWriteAction,
- AgentRecallAction,
- AgentThinkAction,
- AgentFinishAction,
+ NullAction,
)
from opendevin.agent import Agent
from opendevin.controller import AgentController
from 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 codebase
ACTION_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`.")
+ return
await self.send({"action": "initialize", "message": "Control loop started."})
async def start_task(self, start_event):