Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sandbox): add container process timeout kill machenism #316

Merged
merged 2 commits into from
Mar 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions opendevin/sandbox/sandbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from typing import Dict, List, Tuple

import docker
import concurrent.futures

InputType = namedtuple("InputType", ["content"])
OutputType = namedtuple("OutputType", ["content"])
Expand Down Expand Up @@ -148,9 +149,21 @@ def read_logs(self, id) -> str:

def execute(self, cmd: str) -> Tuple[int, str]:
# TODO: each execute is not stateful! We need to keep track of the current working directory
exit_code, logs = self.container.exec_run(
self.get_exec_cmd(cmd), workdir="/workspace"
)
def run_command(container, command):
return container.exec_run(command,workdir="/workspace")
# Use ThreadPoolExecutor to control command and set timeout
with concurrent.futures.ThreadPoolExecutor() as executor:
future = executor.submit(run_command, self.container, self.get_exec_cmd(cmd))
try:
exit_code, logs = future.result(timeout=self.timeout)
except concurrent.futures.TimeoutError:
print("Command timed out, killing process...")
pid = self.get_pid(cmd)
if pid is not None:
self.container.exec_run(
f"kill -9 {pid}", workdir="/workspace"
)
return -1, f"Command: \"{cmd}\" timed out"
return exit_code, logs.decode("utf-8")

def execute_in_background(self, cmd: str) -> BackgroundCommand:
Expand Down