Secure, Fast, and Extensible Sandbox runtime for AI agents.
fix(sdk-python): close a dropped sync byte stream in the stream itself
Maohao6f6203c8873b
diff --git a/sdks/sandbox/python/src/opensandbox/adapters/download_response.py b/sdks/sandbox/python/src/opensandbox/adapters/download_response.pyindex 7d2e12e18..2df19bf94 100644--- a/sdks/sandbox/python/src/opensandbox/adapters/download_response.py+++ b/sdks/sandbox/python/src/opensandbox/adapters/download_response.py@@ -135,6 +135,12 @@ class _ResponseByteStream:self._closed = Trueself._response.close()+ def __del__(self) -> None:+ # The sync read_bytes_stream hands this out as a plain iterator, so a+ # caller that stops early may drop it without calling close().+ if not getattr(self, "_closed", True):+ self.close()+def __enter__(self) -> "_ResponseByteStream":return selfdiff --git a/sdks/sandbox/python/src/opensandbox/sync/adapters/filesystem_adapter.py b/sdks/sandbox/python/src/opensandbox/sync/adapters/filesystem_adapter.pyindex f6f9bbaba..a324ffe47 100644--- a/sdks/sandbox/python/src/opensandbox/sync/adapters/filesystem_adapter.py+++ b/sdks/sandbox/python/src/opensandbox/sync/adapters/filesystem_adapter.py@@ -217,7 +217,7 @@ class FilesystemAdapterSync(FilesystemSync):offset: int | None = None,limit: int | None = None,) -> Iterator[bytes]:- body = self.read_bytes_stream_detailed(+ return self.read_bytes_stream_detailed(path,chunk_size=chunk_size,range_header=range_header,@@ -225,15 +225,6 @@ class FilesystemAdapterSync(FilesystemSync):limit=limit,).body- # A generator closes the response when the caller drops it early.- def _iter() -> Iterator[bytes]:- try:- yield from body- finally:- body.close()-- return _iter()-def read_bytes_stream_detailed(self,path: str,diff --git a/sdks/sandbox/python/src/opensandbox/sync/adapters/isolated_filesystem_adapter.py b/sdks/sandbox/python/src/opensandbox/sync/adapters/isolated_filesystem_adapter.pyindex 8d8be35ca..3bfbda787 100644--- a/sdks/sandbox/python/src/opensandbox/sync/adapters/isolated_filesystem_adapter.py+++ b/sdks/sandbox/python/src/opensandbox/sync/adapters/isolated_filesystem_adapter.py@@ -174,7 +174,7 @@ class IsolatedFilesystemAdapterSync(FilesystemSync):offset: int | None = None,limit: int | None = None,) -> Iterator[bytes]:- body = self.read_bytes_stream_detailed(+ return self.read_bytes_stream_detailed(path,chunk_size=chunk_size,range_header=range_header,@@ -182,15 +182,6 @@ class IsolatedFilesystemAdapterSync(FilesystemSync):limit=limit,).body- # A generator closes the response when the caller drops it early.- def _iter() -> Iterator[bytes]:- try:- yield from body- finally:- body.close()-- return _iter()-def read_bytes_stream_detailed(self,path: str,diff --git a/sdks/sandbox/python/tests/test_filesystem_download_response.py b/sdks/sandbox/python/tests/test_filesystem_download_response.pyindex ecfdc66b9..2cbde695b 100644--- a/sdks/sandbox/python/tests/test_filesystem_download_response.py+++ b/sdks/sandbox/python/tests/test_filesystem_download_response.py@@ -287,6 +287,17 @@ def test_sync_legacy_stream_closes_response_when_abandoned(isolated: bool) -> Nodel chunksgc.collect()assert streams[0].closed is True++ # The same holds when nothing was read.+ unread = adapter.read_bytes_stream("/data.bin")+ del unread+ gc.collect()+ assert streams[1].closed is True++ # close() before the first chunk releases it too.+ closed = adapter.read_bytes_stream("/data.bin")+ closed.close() # type: ignore[attr-defined]+ assert streams[2].closed is Trueadapter._httpx_client.close()