Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

Fix playground multiprocessing

Tomas Valenta44f5ee1a87ad
diff --git a/api-service/session/playground/base.py b/api-service/session/playground/base.py
index cac5f2c41..1142a89b4 100644
--- a/api-service/session/playground/base.py
+++ b/api-service/session/playground/base.py
@@ -1,5 +1,6 @@
import math
+from multiprocessing.pool import ApplyResult
from typing import Any, List
from asyncio import sleep
@@ -14,14 +15,14 @@ from playground_client.models.read_filesystem_file_response import (
from playground_client.models.session_response import SessionResponse
import playground_client
from session.env import cmd_with_env_vars
-from session.session import GetEnvs, Session
+from session.session import GetEnvs, Session, get_result
class Playground(Session):
port_check_interval = 0.5 # 500ms
max_port_checks = 10
- run_command_timeout_frequency = 2 # in Hz
+ run_command_timeout_frequency = 1 # in Hz
def __init__(self, env_id: str, get_envs: GetEnvs, rootdir="/"):
super().__init__(env_id, get_envs)
@@ -29,7 +30,7 @@ class Playground(Session):
async def get_open_ports(self):
thread: Any = self.api.get_session(self.id, async_req=True)
- response: SessionResponse = thread.get()
+ response: SessionResponse = await get_result(thread)
return response.ports
async def is_port_open(self, port: float) -> bool:
@@ -128,7 +129,9 @@ class Playground(Session):
rootdir: str | None = None,
timeout: float | None = None,
):
- thread: Any = self.api.start_process(
+ thread: ApplyResult[ProcessResponse]
+
+ thread = self.api.start_process(
self.id,
playground_client.StartProcessParams(
cmd=cmd_with_env_vars(cmd, self.env_vars),
@@ -138,9 +141,9 @@ class Playground(Session):
),
wait=True if timeout is None else False,
async_req=True,
- )
+ ) # type: ignore
- response: ProcessResponse = thread.get()
+ response: ProcessResponse = await get_result(thread)
if timeout is None or response.finished:
return response
@@ -151,22 +154,22 @@ class Playground(Session):
self.id,
process_id=response.process_id,
async_req=True,
- )
+ ) # type: ignore
- response: ProcessResponse = thread.get()
+ response: ProcessResponse = await get_result(thread)
if response.finished:
return response
await sleep(1 / self.run_command_timeout_frequency)
if not response.finished:
- thread: Any = self.api.stop_process(
+ thread = self.api.stop_process(
self.id,
process_id=response.process_id,
results=False,
async_req=True,
- )
- thread.get()
+ ) # type: ignore
+ await get_result(thread)
return response
@@ -187,7 +190,7 @@ class Playground(Session):
async_req=True,
)
- response: ProcessResponse = thread.get()
+ response: ProcessResponse = await get_result(thread)
return response.process_id
async def stop_process(self, process_id: str):
@@ -198,26 +201,26 @@ class Playground(Session):
async_req=True,
)
- response: ProcessResponse = thread.get()
+ response: ProcessResponse = await get_result(thread)
return response
async def read_file(self, path: str):
thread: Any = self.api.read_filesystem_file(self.id, path, async_req=True)
- response: ReadFilesystemFileResponse = thread.get()
+ response: ReadFilesystemFileResponse = await get_result(thread)
return response.content
async def list_dir(self, path: str):
thread: Any = self.api.list_filesystem_dir(self.id, path, async_req=True)
- response: ListFilesystemDirResponse = thread.get()
+ response: ListFilesystemDirResponse = await get_result(thread)
return response.entries
async def delete_file(self, path: str):
thread: Any = self.api.delete_filesystem_entry(self.id, path, async_req=True)
- thread.get()
+ await get_result(thread)
async def delete_dir(self, path: str):
thread: Any = self.api.delete_filesystem_entry(self.id, path, async_req=True)
- thread.get()
+ await get_result(thread)
async def write_file(self, path: str, content: str):
print("Writing file", path, content)
@@ -227,11 +230,11 @@ class Playground(Session):
playground_client.WriteFilesystemFileRequest(content=content),
async_req=True,
)
- thread.get()
+ await get_result(thread)
async def make_dir(self, path: str):
thread: Any = self.api.make_filesystem_dir(self.id, path, async_req=True)
- thread.get()
+ await get_result(thread)
async def run_server_with_request(
self,
diff --git a/api-service/session/session.py b/api-service/session/session.py
index f67e95def..ecedd4128 100644
--- a/api-service/session/session.py
+++ b/api-service/session/session.py
@@ -1,3 +1,6 @@
+import asyncio
+
+from multiprocessing.pool import ApplyResult
from typing import Callable, Coroutine, List, Any
import playground_client
@@ -15,9 +18,17 @@ configuration.verify_ssl = False
configuration.ssl_ca_cert = None
configuration.cert_file = None
+
GetEnvs = Callable[[], Coroutine[Any, Any, List[EnvVar]]]
+async def get_result(result: ApplyResult[Any]):
+ while not result.ready():
+ await asyncio.sleep(1) # give other tasks chance to run
+
+ return result.get()
+
+
class Session:
def __init__(self, env_id: str, get_envs: GetEnvs):
self.client = playground_client.ApiClient(configuration, pool_threads=3)
@@ -28,17 +39,14 @@ class Session:
self.get_envs = get_envs
self.id: str = ""
- def __del__(self):
- self.close()
-
async def open(self):
- thread: Any = self.api.create_sessions(
+ thread: ApplyResult[Any] = self.api.create_sessions(
playground_client.CreateSessionsRequest(envID=self.env_id),
_request_timeout=10,
async_req=True,
- )
+ ) # type: ignore
- response: SessionResponse = thread.get()
+ response: SessionResponse = await get_result(thread)
self.id = response.id
async def update_envs(self):
@@ -49,9 +57,9 @@ class Session:
result = await self.get_envs()
self.env_vars = format_env_vars(result)
- def close(self):
+ async def close(self):
if not self.is_closed and self.id is not None:
thread: Any = self.api.delete_session(self.id, async_req=True)
- thread.get()
+ await get_result(thread)
self.is_closed = True
self.client.close()