Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
Improve sdks performance (#287)
Jakub Novák41fc5ffa90ed
diff --git a/.changeset/shiny-shoes-serve.md b/.changeset/shiny-shoes-serve.mdnew file mode 100644index 000000000..ae3d5189d--- /dev/null+++ b/.changeset/shiny-shoes-serve.md@@ -0,0 +1,6 @@+---+"@e2b/python-sdk": patch+"@e2b/sdk": patch+---++Improve JS & Python SDKs performancediff --git a/packages/js-sdk/src/sandbox/index.ts b/packages/js-sdk/src/sandbox/index.tsindex 1f64c5489..74d1c1560 100644--- a/packages/js-sdk/src/sandbox/index.ts+++ b/packages/js-sdk/src/sandbox/index.ts@@ -214,16 +214,17 @@ export class Sandbox extends SandboxConnection {createDeferredPromise<TerminalOutput>()terminalExited.then(async () => {- const results = await Promise.allSettled([+ Promise.allSettled([this._unsubscribe(onExitSubID),this._unsubscribe(onDataSubID),- ])- this.logger.debug?.(`Terminal "${terminalID}" exited`)+ ]).then((results) => {+ const errMsg = formatSettledErrors(results)+ if (errMsg) {+ this.logger.debug?.(errMsg)+ }+ })- const errMsg = formatSettledErrors(results)- if (errMsg) {- this.logger.error?.(errMsg)- }+ this.logger.debug?.(`Terminal "${terminalID}" exited`)onExit?.()handleFinishUnsubscribing(output)@@ -354,17 +355,18 @@ export class Sandbox extends SandboxConnection {createDeferredPromise<ProcessOutput>()processExited.then(async () => {- const results = await Promise.allSettled([+ Promise.allSettled([this._unsubscribe(onExitSubID),onStdoutSubID ? this._unsubscribe(onStdoutSubID) : undefined,onStderrSubID ? this._unsubscribe(onStderrSubID) : undefined,- ])- this.logger.debug?.(`Process "${processID}" exited`)+ ]).then((results) => {+ const errMsg = formatSettledErrors(results)+ if (errMsg) {+ this.logger.debug?.(errMsg)+ }+ })- const errMsg = formatSettledErrors(results)- if (errMsg) {- this.logger.error?.(errMsg)- }+ this.logger.debug?.(`Process "${processID}" exited`)if (onExit) {onExit(output.exitCode || 0)diff --git a/packages/js-sdk/src/sandbox/sandboxConnection.ts b/packages/js-sdk/src/sandbox/sandboxConnection.tsindex 4206d492c..5433fdad1 100644--- a/packages/js-sdk/src/sandbox/sandboxConnection.ts+++ b/packages/js-sdk/src/sandbox/sandboxConnection.ts@@ -207,20 +207,12 @@ export class SandboxConnection {this.logger.debug?.(`Closing sandbox "${this.sandbox?.instanceID}"`)this.isOpen = false- this.logger.debug?.('Unsubscribing...')- const results = await Promise.allSettled(- this.subscribers.map((s) => this._unsubscribe(s.subID)),- )- results.forEach((r) => {- if (r.status === 'rejected') {- this.logger.warn?.(`Failed to unsubscribe: "${r.reason}"`)- }- })-// This is `ws` way of closing connectionthis.rpc.ws?.terminate?.()// This is the browser WebSocket way of closing connectionthis.rpc.ws?.close?.()+ this.subscribers = []+this.logger.debug?.('Disconnected from the sandbox')}}diff --git a/packages/python-sdk/e2b/sandbox/code_snippet.py b/packages/python-sdk/e2b/sandbox/code_snippet.pyindex 515ab1b4b..b0ed9d54f 100644--- a/packages/python-sdk/e2b/sandbox/code_snippet.py+++ b/packages/python-sdk/e2b/sandbox/code_snippet.py@@ -2,7 +2,7 @@ from typing import Any, Callable, ClassVar, List, Optionalfrom pydantic import BaseModelfrom e2b.sandbox.exception import MultipleExceptions, RpcException, SandboxException-from e2b.sandbox.sandbox_connection import SandboxConnection+from e2b.sandbox.sandbox_connection import SandboxConnection, SubscriptionArgsclass OpenPort(BaseModel):@@ -25,26 +25,25 @@ class CodeSnippetManager(BaseModel):on_scan_ports: Optional[Callable[[List[OpenPort]], Any]] = Nonedef _subscribe(self):+ def on_scan_ports(ports: List[dict]):+ ports = [+ OpenPort(+ ip=port["Ip"],+ port=port["Port"],+ state=port["State"],+ )+ for port in ports+ ]+ if self.on_scan_ports:+ self.on_scan_ports(ports)+try:self.sandbox._handle_subscriptions(- self.sandbox._subscribe(- self.service_name,- lambda ports: self.on_scan_ports(- [- OpenPort(- ip=port["Ip"],- port=port["Port"],- state=port["State"],- )- for port in ports- ]- )- if self.on_scan_ports- else None,- "scanOpenedPorts",+ SubscriptionArgs(+ service=self.service_name,+ handler=on_scan_ports,+ method="scanOpenedPorts",)- if self.on_scan_ports- else None,)except RpcException as e:raise SandboxException(e.message) from ediff --git a/packages/python-sdk/e2b/sandbox/exception.py b/packages/python-sdk/e2b/sandbox/exception.pyindex 39dd04cf2..8565733f9 100644--- a/packages/python-sdk/e2b/sandbox/exception.py+++ b/packages/python-sdk/e2b/sandbox/exception.py@@ -5,6 +5,10 @@ class SandboxException(Exception):pass+class SandboxNotOpenException(Exception):+ pass++class RpcException(SandboxException):def __init__(self,diff --git a/packages/python-sdk/e2b/sandbox/main.py b/packages/python-sdk/e2b/sandbox/main.pyindex 8ce751380..8a4bcfb61 100644--- a/packages/python-sdk/e2b/sandbox/main.py+++ b/packages/python-sdk/e2b/sandbox/main.py@@ -143,7 +143,6 @@ class Sandbox(SandboxConnection):_debug_dev_env=_debug_dev_env,timeout=timeout,)- self._on_close_child = self._close_servicesself._actions: Dict[str, Action[Self]] = {}def add_action(self, action: Action[Self], name: Optional[str] = None) -> "Sandbox":@@ -256,17 +255,6 @@ class Sandbox(SandboxConnection):if self._on_stderr or self._on_stdout:self._handle_start_cmd_logs()- def _close_services(self):- self._terminal._close()- self._process._close()-- def close(self) -> None:- """- Close the sandbox.- """- super().close()- self._close()-def file_url(self) -> str:"""Return a URL that can be used to upload files to the sandbox via a multipart/form-data POST request.diff --git a/packages/python-sdk/e2b/sandbox/process.py b/packages/python-sdk/e2b/sandbox/process.pyindex 62ff4140c..a5d4e9732 100644--- a/packages/python-sdk/e2b/sandbox/process.py+++ b/packages/python-sdk/e2b/sandbox/process.py@@ -1,8 +1,8 @@import loggingimport reimport inspect--from concurrent.futures import ThreadPoolExecutor+import threading+from concurrent.futures import Futurefrom typing import (Any,Callable,@@ -23,10 +23,8 @@ from e2b.sandbox.exception import (CurrentWorkingDirectoryDoesntExistException,)from e2b.sandbox.out import OutStderrResponse, OutStdoutResponse-from e2b.sandbox.sandbox_connection import SandboxConnection-from e2b.utils.future import DeferredFuture+from e2b.sandbox.sandbox_connection import SandboxConnection, SubscriptionArgsfrom e2b.utils.id import create_id-from e2b.utils.threads import shutdown_executorlogger = logging.getLogger(__name__)@@ -97,7 +95,7 @@ class Process:process_id: str,sandbox: SandboxConnection,trigger_exit: Callable[[], Any],- finished: DeferredFuture[ProcessOutput],+ finished: Future[ProcessOutput],output: ProcessOutput,):self._process_id = process_id@@ -225,12 +223,6 @@ class ProcessManager:self._on_stderr = on_stderrself._on_exit = on_exit- def _close(self):- for cleanup in self._process_cleanup:- cleanup()-- self._process_cleanup.clear()-def start(self,cmd: str,@@ -251,7 +243,7 @@ class ProcessManager:on_stderr = on_stderr or self._on_stderron_exit = on_exit or self._on_exit- future_exit = DeferredFuture(self._process_cleanup)+ future_exit = Future()process_id = process_id or create_id(12)unsub_all: Optional[Callable] = None@@ -261,7 +253,7 @@ class ProcessManager:def handle_exit(exit_code: int):output.exit_code = exit_codelogger.info(f"Process {process_id} exited with exit code {exit_code}")- future_exit(True)+ future_exit.set_result(True)def handle_stdout(data: Dict[Any, Any]):out = OutStdoutResponse(**data)@@ -296,17 +288,28 @@ class ProcessManager:logger.exception(f"Error in on_stdout callback: {error}")try:- unsub_all = self._sandbox._handle_subscriptions(- self._sandbox._subscribe(- self._service_name, handle_exit, "onExit", process_id+ subscription_args = [+ SubscriptionArgs(+ service=self._service_name,+ handler=handle_exit,+ method="onExit",+ params=[process_id],),- self._sandbox._subscribe(- self._service_name, handle_stdout, "onStdout", process_id+ SubscriptionArgs(+ service=self._service_name,+ handler=handle_stdout,+ method="onStdout",+ params=[process_id],),- self._sandbox._subscribe(- self._service_name, handle_stderr, "onStderr", process_id+ SubscriptionArgs(+ service=self._service_name,+ handler=handle_stderr,+ method="onStderr",+ params=[process_id],),- )+ ]+ unsub_all = self._sandbox._handle_subscriptions(*subscription_args)+except (RpcException, MultipleExceptions) as e:future_exit.cancel()@@ -317,15 +320,11 @@ class ProcessManager:"Failed to subscribe to RPC services necessary for starting process") from e- future_exit_handler_finish = DeferredFuture[ProcessOutput](- self._process_cleanup- )+ future_exit_handler_finish = Future[ProcessOutput]()def exit_handler():future_exit.result()- logger.info(- f"Handling process exit (id: {process_id})",- )+ logger.info(f"Handling process exit (id: {process_id})")if unsub_all:unsub_all()if on_exit:@@ -338,17 +337,13 @@ class ProcessManager:on_exit(output.exit_code or 0)except TypeError as error:logger.exception(f"Error in on_exit callback: {error}")- future_exit_handler_finish(output)-- executor = ThreadPoolExecutor(thread_name_prefix="e2b-process-exit-handler")- exit_task = executor.submit(exit_handler)+ future_exit_handler_finish.set_result(output)- self._process_cleanup.append(exit_task.cancel)- self._process_cleanup.append(lambda: shutdown_executor(executor))+ threading.Thread(name="e2b-process-exit-handler", daemon=True, target=exit_handler).start()def trigger_exit():logger.info(f"Exiting the process (id: {process_id})")- future_exit(None)+ future_exit.set_result(None)future_exit_handler_finish.result()logger.debug(f"Exited the process (id: {process_id})")diff --git a/packages/python-sdk/e2b/sandbox/sandbox_connection.py b/packages/python-sdk/e2b/sandbox/sandbox_connection.pyindex 3d70b0a68..71f11c98c 100644--- a/packages/python-sdk/e2b/sandbox/sandbox_connection.py+++ b/packages/python-sdk/e2b/sandbox/sandbox_connection.py@@ -1,8 +1,9 @@-import functools+import concurrentimport loggingimport threadingimport tracebackimport warnings+from concurrent.futures import ThreadPoolExecutor, CancelledErrorfrom os import getenvfrom time import sleepfrom typing import Any, Callable, List, Literal, Optional, Union@@ -24,7 +25,7 @@ from e2b.sandbox.exception import (AuthenticationException,MultipleExceptions,SandboxException,- TimeoutException,+ TimeoutException, SandboxNotOpenException,)from e2b.sandbox.sandbox_rpc import Notification, SandboxRpcfrom e2b.utils.future import DeferredFuture@@ -40,6 +41,13 @@ class Subscription(BaseModel):handler: Callable[[Any], None]+class SubscriptionArgs(BaseModel):+ service: str+ handler: Callable[[Any], None]+ method: str+ params: List[Any] = []++class SandboxConnection:_refresh_retries = 4_on_close_child: Optional[Callable[[], Any]] = None@@ -248,8 +256,6 @@ class SandboxConnection:if self._on_close_child:self._on_close_child()- self._subscribers.clear()-for cleanup in self._process_cleanup:cleanup()self._process_cleanup.clear()@@ -333,7 +339,7 @@ class SandboxConnection:params = []if not self.is_open:- raise SandboxException("Sandbox is not open")+ raise SandboxNotOpenException("Sandbox is not open")if not self._rpc:raise SandboxException("Sandbox is not connected")@@ -342,34 +348,58 @@ class SandboxConnection:def _handle_subscriptions(self,- *subs: Optional[Callable[[], None]],+ *subscription_args: SubscriptionArgs,):- results: List[Union[Callable, None, Exception]] = [sub for sub in subs if sub]+ results: List[Union[Callable, None, Exception]] = []+ with ThreadPoolExecutor(thread_name_prefix="process-subscribe") as executor:+ futures = []+ for args in subscription_args:+ future = executor.submit(+ self._subscribe,+ args.service,+ args.handler,+ args.method,+ *args.params,+ )+ futures.append(future)- exceptions = [e for e in results if isinstance(e, Exception)]+ for future in concurrent.futures.as_completed(futures):+ results.append(future.result())- def unsub_all():- return lambda: functools.reduce(- lambda _, f: f(),- [unsub for unsub in results if not isinstance(unsub, Exception)],- )+ results = [sub for sub in results if sub]+ process_exceptions = [e for e in results if isinstance(e, Exception)]- if len(exceptions) > 0:+ def unsub_all():+ def unsub_func():+ for unsub in results:+ if not isinstance(unsub, Exception):+ try:+ unsub()+ except (CancelledError, SandboxNotOpenException, KeyError):+ pass++ threading.Thread(+ name="process-unsubscribe",+ daemon=True,+ target=unsub_func,+ ).start()++ if len(process_exceptions) > 0:unsub_all()- if len(exceptions) == 1:- raise exceptions[0]+ if len(process_exceptions) == 1:+ raise process_exceptions[0]error_message = "\n"- for i, s in enumerate(exceptions):+ for i, s in enumerate(process_exceptions):tb = s.__traceback__ # Get the traceback objectstack_trace = "\n".join(traceback.extract_tb(tb).format())error_message += f'\n[{i}]: {type(s).__name__}("{s}"):\n{stack_trace}\n'raise MultipleExceptions(message=error_message,- exceptions=exceptions,+ exceptions=process_exceptions,)return unsub_alldiff --git a/packages/python-sdk/e2b/sandbox/terminal.py b/packages/python-sdk/e2b/sandbox/terminal.pyindex 4e11606f7..36c08edce 100644--- a/packages/python-sdk/e2b/sandbox/terminal.py+++ b/packages/python-sdk/e2b/sandbox/terminal.py@@ -1,16 +1,15 @@import logging-from concurrent.futures import ThreadPoolExecutor-from typing import Any, Callable, List, Optional+import threading+from concurrent.futures import Future+from typing import Any, Callable, Optionalfrom pydantic import BaseModelfrom e2b.constants import TIMEOUTfrom e2b.sandbox.env_vars import EnvVarsfrom e2b.sandbox.exception import MultipleExceptions, RpcException, TerminalException-from e2b.sandbox.sandbox_connection import SandboxConnection-from e2b.utils.future import DeferredFuture+from e2b.sandbox.sandbox_connection import SandboxConnection, SubscriptionArgsfrom e2b.utils.id import create_id-from e2b.utils.threads import shutdown_executorlogger = logging.getLogger(__file__)@@ -66,7 +65,7 @@ class Terminal:terminal_id: str,sandbox: SandboxConnection,trigger_exit: Callable[[], Any],- finished: DeferredFuture[TerminalOutput],+ finished: Future[TerminalOutput],output: TerminalOutput,):self._terminal_id = terminal_id@@ -137,13 +136,6 @@ class TerminalManager:def __init__(self, sandbox: SandboxConnection):self._sandbox = sandbox- self._process_cleanup: List[Callable[[], Any]] = []-- def _close(self):- for cleanup in self._process_cleanup:- cleanup()-- self._process_cleanup.clear()def start(self,@@ -175,7 +167,7 @@ class TerminalManager:"""env_vars = self._sandbox.env_vars.update(env_vars or {})- future_exit = DeferredFuture(self._process_cleanup)+ future_exit = Future()terminal_id = terminal_id or create_id(12)unsub_all: Optional[Callable[[], Any]] = None@@ -188,11 +180,17 @@ class TerminalManager:try:unsub_all = self._sandbox._handle_subscriptions(- self._sandbox._subscribe(- self._service_name, handle_data, "onData", terminal_id+ SubscriptionArgs(+ service=self._service_name,+ handler=handle_data,+ method="onData",+ params=[terminal_id],),- self._sandbox._subscribe(- self._service_name, future_exit, "onExit", terminal_id+ SubscriptionArgs(+ service=self._service_name,+ handler=future_exit,+ method="onExit",+ params=[terminal_id],),)except (RpcException, MultipleExceptions) as e:@@ -206,9 +204,7 @@ class TerminalManager:) from e# TODO: Handle exit handler finish for exits (the same for processes)- future_exit_handler_finish = DeferredFuture[TerminalOutput](- self._process_cleanup- )+ future_exit_handler_finish = Future[TerminalOutput]( )def exit_handler():future_exit.result()@@ -218,16 +214,12 @@ class TerminalManager:if on_exit:on_exit()- future_exit_handler_finish(output)-- executor = ThreadPoolExecutor(thread_name_prefix="e2b-terminal-exit-handler")- exit_task = executor.submit(exit_handler)+ future_exit_handler_finish.set_result(output)- self._process_cleanup.append(exit_task.cancel)- self._process_cleanup.append(lambda: shutdown_executor(executor))+ threading.Thread(name="e2b-terminal-exit-handler", daemon=True, target=exit_handler).start()def trigger_exit():- future_exit(None)+ future_exit.set_result(None)future_exit_handler_finish.result()try:diff --git a/packages/python-sdk/e2b/utils/future.py b/packages/python-sdk/e2b/utils/future.pyindex a093ac1f8..269de3090 100644--- a/packages/python-sdk/e2b/utils/future.py+++ b/packages/python-sdk/e2b/utils/future.py@@ -1,7 +1,7 @@import asynciofrom asyncio import AbstractEventLoop-from concurrent.futures import Future+from concurrent.futures import Future, CancelledErrorfrom typing import Any, Awaitable, Callable, Generic, List, Optional, TypeVarT = TypeVar("T")@@ -19,7 +19,11 @@ class DeferredFuture(Generic[T]):self._future.set_result(result)def result(self, timeout: Optional[float] = None) -> T:- return self._future.result(timeout=timeout)+ try:+ return self._future.result(timeout=timeout)+ # The thread was interrupted while waiting, it's okay to release the thread+ except CancelledError:+ passdef done(self) -> bool:return self._future.done()