Skip to content

πŸ™Œ OpenHands: AI-Driven Development

Support specify timeout for long running tasks (#1756)

Xingyao Wang755a4072b612
diff --git a/opendevin/runtime/docker/exec_box.py b/opendevin/runtime/docker/exec_box.py
index ba686e1175..545a61be7a 100644
--- a/opendevin/runtime/docker/exec_box.py
+++ b/opendevin/runtime/docker/exec_box.py
@@ -106,7 +106,9 @@ class DockerExecBox(Sandbox):
bg_cmd = self.background_commands[id]
return bg_cmd.read_logs()
- def execute(self, cmd: str) -> tuple[int, str]:
+ def execute(self, cmd: str, timeout: int | None = None) -> tuple[int, str]:
+ timeout = timeout if timeout is not None else self.timeout
+
# TODO: each execute is not stateful! We need to keep track of the current working directory
def run_command(container, command):
return container.exec_run(
@@ -119,7 +121,7 @@ class DockerExecBox(Sandbox):
run_command, self.container, self.get_exec_cmd(cmd)
)
try:
- exit_code, logs = future.result(timeout=self.timeout)
+ exit_code, logs = future.result(timeout=timeout)
except concurrent.futures.TimeoutError:
logger.exception(
'Command timed out, killing process...', exc_info=False
diff --git a/opendevin/runtime/docker/local_box.py b/opendevin/runtime/docker/local_box.py
index 212f3ba8b2..44bf469fe0 100644
--- a/opendevin/runtime/docker/local_box.py
+++ b/opendevin/runtime/docker/local_box.py
@@ -33,14 +33,15 @@ class LocalBox(Sandbox):
atexit.register(self.cleanup)
super().__init__()
- def execute(self, cmd: str) -> tuple[int, str]:
+ def execute(self, cmd: str, timeout: int | None = None) -> tuple[int, str]:
+ timeout = timeout if timeout is not None else self.timeout
try:
completed_process = subprocess.run(
cmd,
shell=True,
text=True,
capture_output=True,
- timeout=self.timeout,
+ timeout=timeout,
cwd=config.workspace_base,
env=self._env,
)
diff --git a/opendevin/runtime/docker/ssh_box.py b/opendevin/runtime/docker/ssh_box.py
index b86a6762d2..54a335d187 100644
--- a/opendevin/runtime/docker/ssh_box.py
+++ b/opendevin/runtime/docker/ssh_box.py
@@ -8,7 +8,6 @@ import time
import uuid
from collections import namedtuple
from glob import glob
-from typing import Tuple
import docker
from pexpect import pxssh
@@ -343,7 +342,9 @@ class DockerSSHBox(Sandbox):
f'Command: "{cmd}" timed out. Sending SIGINT to the process: {command_output}',
)
- def execute(self, cmd: str) -> Tuple[int, str]:
+ def execute(self, cmd: str, timeout: int | None = None) -> tuple[int, str]:
+ timeout = timeout if timeout is not None else self.timeout
+
commands = split_bash_commands(cmd)
if len(commands) > 1:
all_output = ''
@@ -356,7 +357,7 @@ class DockerSSHBox(Sandbox):
return exit_code, all_output
return 0, all_output
self.ssh.sendline(cmd)
- success = self.ssh.prompt(timeout=self.timeout)
+ success = self.ssh.prompt(timeout=timeout)
if not success:
logger.exception('Command timed out, killing process...', exc_info=False)
return self._send_interrupt(cmd)
@@ -389,7 +390,7 @@ class DockerSSHBox(Sandbox):
self.ssh.prompt()
exit_code_str = self.ssh.before.decode('utf-8')
logger.debug(f'WAITING FOR exit code: {exit_code_str}')
- if time.time() - _start_time > self.timeout:
+ if time.time() - _start_time > timeout:
return self._send_interrupt(
cmd, command_output, ignore_last_output=True
)
diff --git a/opendevin/runtime/e2b/sandbox.py b/opendevin/runtime/e2b/sandbox.py
index 3e1786b31b..b0d4e043b2 100644
--- a/opendevin/runtime/e2b/sandbox.py
+++ b/opendevin/runtime/e2b/sandbox.py
@@ -72,10 +72,11 @@ class E2BBox(Sandbox):
assert isinstance(proc, E2BProcess)
return '\n'.join([m.line for m in proc.output_messages])
- def execute(self, cmd: str) -> tuple[int, str]:
+ def execute(self, cmd: str, timeout: int | None = None) -> tuple[int, str]:
+ timeout = timeout if timeout is not None else self.timeout
process = self.sandbox.process.start(cmd, env_vars=self._env)
try:
- process_output = process.wait(timeout=self.timeout)
+ process_output = process.wait(timeout=timeout)
except TimeoutException:
logger.info('Command timed out, killing process...')
process.kill()
diff --git a/opendevin/runtime/sandbox.py b/opendevin/runtime/sandbox.py
index fb278a06f6..59c729bfa1 100644
--- a/opendevin/runtime/sandbox.py
+++ b/opendevin/runtime/sandbox.py
@@ -19,7 +19,7 @@ class Sandbox(ABC, PluginMixin):
self._env[key] = value
@abstractmethod
- def execute(self, cmd: str) -> tuple[int, str]:
+ def execute(self, cmd: str, timeout: int | None = None) -> tuple[int, str]:
pass
@abstractmethod