diff --git a/opendevin/sandbox/sandbox.py b/opendevin/sandbox/sandbox.py index 52d2c3be04..6c8ea941ed 100644 --- a/opendevin/sandbox/sandbox.py +++ b/opendevin/sandbox/sandbox.py @@ -27,10 +27,11 @@ elif hasattr(os, "getuid"): class BackgroundCommand: - def __init__(self, id: int, command: str, result): + def __init__(self, id: int, command: str, result, pid: int): self.id = id self.command = command self.result = result + self.pid = pid def parse_docker_exec_output(self, logs: bytes) -> Tuple[bytes, bytes]: res = b"" @@ -74,10 +75,6 @@ class BackgroundCommand: break return (logs + last_remains).decode("utf-8") - def kill(self): - # FIXME: this doesn't actually kill the process! - self.result.output.close() - class DockerInteractive: closed = False @@ -161,16 +158,32 @@ class DockerInteractive: self.get_exec_cmd(cmd), socket=True, workdir="/workspace" ) result.output._sock.setblocking(0) - bg_cmd = BackgroundCommand(self.cur_background_id, cmd, result) + pid = self.get_pid(cmd) + bg_cmd = BackgroundCommand(self.cur_background_id, cmd, result, pid) self.background_commands[bg_cmd.id] = bg_cmd self.cur_background_id += 1 return bg_cmd + def get_pid(self, cmd): + exec_result = self.container.exec_run("ps aux") + processes = exec_result.output.decode('utf-8').splitlines() + cmd = " ".join(self.get_exec_cmd(cmd)) + + for process in processes: + if cmd in process: + pid = process.split()[1] # second column is the pid + return pid + return None + def kill_background(self, id: int) -> BackgroundCommand: if id not in self.background_commands: raise ValueError("Invalid background command id") bg_cmd = self.background_commands[id] - bg_cmd.kill() + if bg_cmd.pid is not None: + self.container.exec_run( + f"kill -9 {bg_cmd.pid}", workdir="/workspace" + ) + bg_cmd.result.output.close() self.background_commands.pop(id) return bg_cmd