Skip to content

armgit / E2B

Public Imported from GitHub
0

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

Explicitly configure keepalive pinging in Python SDK

Tomas Valentaff368d1ba761
diff --git a/packages/python-sdk/e2b/connection_config.py b/packages/python-sdk/e2b/connection_config.py
index 8b0a58e0b..b90f41823 100644
--- a/packages/python-sdk/e2b/connection_config.py
+++ b/packages/python-sdk/e2b/connection_config.py
@@ -4,6 +4,9 @@ from typing import Literal, Optional
REQUEST_TIMEOUT: float = 30.0 # 30s
+KEEPALIVE_PING_INTERVAL_SEC = 50 # 50s
+KEEPALIVE_PING_HEADER = "Keepalive-Ping-Interval"
+
class ConnectionConfig:
@staticmethod
diff --git a/packages/python-sdk/e2b/sandbox_async/filesystem/filesystem.py b/packages/python-sdk/e2b/sandbox_async/filesystem/filesystem.py
index 7fa1dc5cc..6f79277e3 100644
--- a/packages/python-sdk/e2b/sandbox_async/filesystem/filesystem.py
+++ b/packages/python-sdk/e2b/sandbox_async/filesystem/filesystem.py
@@ -4,7 +4,12 @@ from typing import IO, AsyncIterator, List, Literal, Optional, Union, overload
import e2b_connect as connect
import httpcore
import httpx
-from e2b.connection_config import ConnectionConfig, Username
+from e2b.connection_config import (
+ ConnectionConfig,
+ Username,
+ KEEPALIVE_PING_HEADER,
+ KEEPALIVE_PING_INTERVAL_SEC,
+)
from e2b.envd.api import ENVD_API_FILES_ROUTE, ahandle_envd_api_exception
from e2b.envd.filesystem import filesystem_connect, filesystem_pb2
from e2b.envd.rpc import authentication_header, handle_rpc_exception
@@ -260,7 +265,10 @@ class Filesystem:
request_timeout
),
timeout=timeout,
- headers=authentication_header(user),
+ headers={
+ **authentication_header(user),
+ KEEPALIVE_PING_HEADER: str(KEEPALIVE_PING_INTERVAL_SEC),
+ },
)
try:
diff --git a/packages/python-sdk/e2b/sandbox_async/process/process.py b/packages/python-sdk/e2b/sandbox_async/process/process.py
index 90c8b61f7..6a3733549 100644
--- a/packages/python-sdk/e2b/sandbox_async/process/process.py
+++ b/packages/python-sdk/e2b/sandbox_async/process/process.py
@@ -2,7 +2,12 @@ from typing import Dict, List, Literal, Optional, Union, overload
import e2b_connect
import httpcore
-from e2b.connection_config import ConnectionConfig, Username
+from e2b.connection_config import (
+ ConnectionConfig,
+ Username,
+ KEEPALIVE_PING_HEADER,
+ KEEPALIVE_PING_INTERVAL_SEC,
+)
from e2b.envd.process import process_connect, process_pb2
from e2b.envd.rpc import authentication_header, handle_rpc_exception
from e2b.exceptions import SandboxException
@@ -173,7 +178,10 @@ class Process:
cwd=cwd,
),
),
- headers=authentication_header(user),
+ headers={
+ **authentication_header(user),
+ KEEPALIVE_PING_HEADER: str(KEEPALIVE_PING_INTERVAL_SEC),
+ },
timeout=timeout,
request_timeout=self._connection_config.get_request_timeout(
request_timeout
@@ -214,6 +222,9 @@ class Process:
request_timeout=self._connection_config.get_request_timeout(
request_timeout
),
+ headers={
+ KEEPALIVE_PING_HEADER: str(KEEPALIVE_PING_INTERVAL_SEC),
+ },
)
"""Connect to process"""
try:
diff --git a/packages/python-sdk/e2b/sandbox_async/process/process_handle.py b/packages/python-sdk/e2b/sandbox_async/process/process_handle.py
index 569bd7d48..8e4f165be 100644
--- a/packages/python-sdk/e2b/sandbox_async/process/process_handle.py
+++ b/packages/python-sdk/e2b/sandbox_async/process/process_handle.py
@@ -78,7 +78,9 @@ class AsyncProcessHandle:
self,
) -> AsyncGenerator[
Union[
- Tuple[Stdout, None, None], Tuple[None, Stderr, None], Tuple[None, None, PtyOutput]
+ Tuple[Stdout, None, None],
+ Tuple[None, Stderr, None],
+ Tuple[None, None, PtyOutput],
],
None,
]:
diff --git a/packages/python-sdk/e2b/sandbox_async/pty/main.py b/packages/python-sdk/e2b/sandbox_async/pty/main.py
index a4ef671bc..2c66849c8 100644
--- a/packages/python-sdk/e2b/sandbox_async/pty/main.py
+++ b/packages/python-sdk/e2b/sandbox_async/pty/main.py
@@ -7,6 +7,8 @@ from e2b.envd.process import process_connect, process_pb2
from e2b.connection_config import (
Username,
ConnectionConfig,
+ KEEPALIVE_PING_HEADER,
+ KEEPALIVE_PING_INTERVAL_SEC,
)
from e2b.exceptions import SandboxException
from e2b.envd.rpc import authentication_header, handle_rpc_exception
@@ -101,7 +103,10 @@ class Pty:
size=process_pb2.PTY.Size(rows=size.rows, cols=size.cols)
),
),
- headers=authentication_header(user),
+ headers={
+ **authentication_header(user),
+ KEEPALIVE_PING_HEADER: str(KEEPALIVE_PING_INTERVAL_SEC),
+ },
timeout=timeout,
request_timeout=self._connection_config.get_request_timeout(
request_timeout
diff --git a/packages/python-sdk/e2b/sandbox_sync/filesystem/filesystem.py b/packages/python-sdk/e2b/sandbox_sync/filesystem/filesystem.py
index b0be159ee..eea910554 100644
--- a/packages/python-sdk/e2b/sandbox_sync/filesystem/filesystem.py
+++ b/packages/python-sdk/e2b/sandbox_sync/filesystem/filesystem.py
@@ -4,7 +4,12 @@ from typing import IO, Iterator, List, Literal, Optional, Union, overload
import e2b_connect
import httpcore
import httpx
-from e2b.connection_config import ConnectionConfig, Username
+from e2b.connection_config import (
+ ConnectionConfig,
+ Username,
+ KEEPALIVE_PING_HEADER,
+ KEEPALIVE_PING_INTERVAL_SEC,
+)
from e2b.envd.api import ENVD_API_FILES_ROUTE, handle_envd_api_exception
from e2b.envd.filesystem import filesystem_connect, filesystem_pb2
from e2b.envd.rpc import authentication_header, handle_rpc_exception
@@ -255,7 +260,10 @@ class Filesystem:
request_timeout
),
timeout=timeout,
- headers=authentication_header(user),
+ headers={
+ **authentication_header(user),
+ KEEPALIVE_PING_HEADER: str(KEEPALIVE_PING_INTERVAL_SEC),
+ },
)
try:
diff --git a/packages/python-sdk/e2b/sandbox_sync/process/main.py b/packages/python-sdk/e2b/sandbox_sync/process/main.py
index 9f0e745d8..99e50ab0f 100644
--- a/packages/python-sdk/e2b/sandbox_sync/process/main.py
+++ b/packages/python-sdk/e2b/sandbox_sync/process/main.py
@@ -2,7 +2,12 @@ from typing import Callable, Dict, List, Literal, Optional, Union, overload
import e2b_connect
import httpcore
-from e2b.connection_config import ConnectionConfig, Username
+from e2b.connection_config import (
+ ConnectionConfig,
+ Username,
+ KEEPALIVE_PING_HEADER,
+ KEEPALIVE_PING_INTERVAL_SEC,
+)
from e2b.envd.process import process_connect, process_pb2
from e2b.envd.rpc import authentication_header, handle_rpc_exception
from e2b.exceptions import SandboxException
@@ -175,7 +180,10 @@ class Process:
cwd=cwd,
),
),
- headers=authentication_header(user),
+ headers={
+ **authentication_header(user),
+ KEEPALIVE_PING_HEADER: str(KEEPALIVE_PING_INTERVAL_SEC),
+ },
timeout=timeout,
request_timeout=self._connection_config.get_request_timeout(
request_timeout
@@ -209,6 +217,9 @@ class Process:
process_pb2.ConnectRequest(
process=process_pb2.ProcessSelector(pid=pid),
),
+ headers={
+ KEEPALIVE_PING_HEADER: str(KEEPALIVE_PING_INTERVAL_SEC),
+ },
timeout=timeout,
request_timeout=self._connection_config.get_request_timeout(
request_timeout
diff --git a/packages/python-sdk/e2b/sandbox_sync/pty/main.py b/packages/python-sdk/e2b/sandbox_sync/pty/main.py
index e70d0ad23..b170aa2d8 100644
--- a/packages/python-sdk/e2b/sandbox_sync/pty/main.py
+++ b/packages/python-sdk/e2b/sandbox_sync/pty/main.py
@@ -7,6 +7,8 @@ from e2b.envd.process import process_connect, process_pb2
from e2b.connection_config import (
Username,
ConnectionConfig,
+ KEEPALIVE_PING_HEADER,
+ KEEPALIVE_PING_INTERVAL_SEC,
)
from e2b.exceptions import SandboxException
from e2b.envd.rpc import authentication_header, handle_rpc_exception
@@ -96,7 +98,10 @@ class Pty:
size=process_pb2.PTY.Size(rows=size.rows, cols=size.cols)
),
),
- headers=authentication_header(user),
+ headers={
+ **authentication_header(user),
+ KEEPALIVE_PING_HEADER: str(KEEPALIVE_PING_INTERVAL_SEC),
+ },
timeout=timeout,
request_timeout=self._connection_config.get_request_timeout(
request_timeout
diff --git a/packages/python-sdk/tests/async/sandbox_async/pty/test_send_input.py b/packages/python-sdk/tests/async/sandbox_async/pty/test_send_input.py
index a6d354cb8..bd5d2dd84 100644
--- a/packages/python-sdk/tests/async/sandbox_async/pty/test_send_input.py
+++ b/packages/python-sdk/tests/async/sandbox_async/pty/test_send_input.py
@@ -3,7 +3,9 @@ from e2b.sandbox.process.process_handle import PtySize
async def test_send_input(async_sandbox: AsyncSandbox):
- terminal = await async_sandbox.pty.create(PtySize(cols=80, rows=24))
+ terminal = await async_sandbox.pty.create(
+ PtySize(cols=80, rows=24), on_data=lambda x: print(x)
+ )
await async_sandbox.pty.send_stdin(terminal.pid, b"exit\n")
await terminal.wait()
assert terminal.exit_code == 0