Skip to content

armgit / E2B

Public Imported from GitHub
0

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

fix(python-sdk): close gRPC streams on watcher/command teardown (#1346)

Mish Ushakova9bb287fc156
diff --git a/.changeset/fix-async-stream-leak.md b/.changeset/fix-async-stream-leak.md
new file mode 100644
index 000000000..d3ce9c0b3
--- /dev/null
+++ b/.changeset/fix-async-stream-leak.md
@@ -0,0 +1,7 @@
+---
+"@e2b/python-sdk": patch
+---
+
+fix(python-sdk): close gRPC streams on `AsyncWatchHandle.stop()` and `AsyncCommandHandle.disconnect()`
+
+`stop()` / `disconnect()` previously only cancelled the consumer task and left the underlying server-streaming RPC open (the `await self._events.aclose()` was commented out as a Python 3.8 workaround). On long-lived sandboxes this leaked file descriptors and eventually produced `Code.internal: error creating watcher: too many open files`. Python 3.8 is no longer supported (`pyproject.toml` pins `^3.10`), so the workaround is removed and the streams are now closed explicitly.
diff --git a/packages/python-sdk/e2b/sandbox_async/commands/command_handle.py b/packages/python-sdk/e2b/sandbox_async/commands/command_handle.py
index dfe34e838..054ff41bc 100644
--- a/packages/python-sdk/e2b/sandbox_async/commands/command_handle.py
+++ b/packages/python-sdk/e2b/sandbox_async/commands/command_handle.py
@@ -137,8 +137,11 @@ class AsyncCommandHandle:
You can reconnect to the command using `sandbox.commands.connect` method.
"""
self._wait.cancel()
- # BUG: In Python 3.8 closing async generator can throw RuntimeError.
- # await self._events.aclose()
+ await asyncio.wait([self._wait])
+ try:
+ await self._events.aclose()
+ except Exception:
+ pass
async def _handle_events(self):
try:
diff --git a/packages/python-sdk/e2b/sandbox_async/filesystem/watch_handle.py b/packages/python-sdk/e2b/sandbox_async/filesystem/watch_handle.py
index 33759d88c..6a32de50c 100644
--- a/packages/python-sdk/e2b/sandbox_async/filesystem/watch_handle.py
+++ b/packages/python-sdk/e2b/sandbox_async/filesystem/watch_handle.py
@@ -33,8 +33,11 @@ class AsyncWatchHandle:
Stop watching the directory.
"""
self._wait.cancel()
- # BUG: In Python 3.8 closing async generator can throw RuntimeError.
- # await self._events.aclose()
+ await asyncio.wait([self._wait])
+ try:
+ await self._events.aclose()
+ except Exception:
+ pass
async def _iterate_events(self):
try: