Secure, Fast, and Extensible Sandbox runtime for AI agents.
fix(sdk-python): close the response when a sync byte stream is dropped
Maohao24baf841c712
diff --git a/sdks/sandbox/python/src/opensandbox/sync/adapters/filesystem_adapter.py b/sdks/sandbox/python/src/opensandbox/sync/adapters/filesystem_adapter.pyindex a324ffe47..f6f9bbaba 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]:- return self.read_bytes_stream_detailed(+ body = self.read_bytes_stream_detailed(path,chunk_size=chunk_size,range_header=range_header,@@ -225,6 +225,15 @@ 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 3bfbda787..8d8be35ca 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]:- return self.read_bytes_stream_detailed(+ body = self.read_bytes_stream_detailed(path,chunk_size=chunk_size,range_header=range_header,@@ -182,6 +182,15 @@ 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 a1bb54816..ecfdc66b9 100644--- a/sdks/sandbox/python/tests/test_filesystem_download_response.py+++ b/sdks/sandbox/python/tests/test_filesystem_download_response.py@@ -15,6 +15,7 @@#import asyncio+import gcimport httpximport pytest@@ -26,6 +27,9 @@ from opensandbox.models import ByteRangefrom opensandbox.models.sandboxes import SandboxEndpointfrom opensandbox.services import DownloadFilesystemfrom opensandbox.sync.adapters.filesystem_adapter import FilesystemAdapterSync+from opensandbox.sync.adapters.isolated_filesystem_adapter import (+ IsolatedFilesystemAdapterSync,+)from opensandbox.sync.services import DownloadFilesystemSync@@ -257,6 +261,35 @@ def test_sync_detailed_streams_close_unconsumed_and_partial_responses() -> None:adapter._httpx_client.close()[email protected]("isolated", [False, True])+def test_sync_legacy_stream_closes_response_when_abandoned(isolated: bool) -> None:+ streams: list[_TrackingSyncStream] = []++ def handler(request: httpx.Request) -> httpx.Response:+ stream = _TrackingSyncStream()+ streams.append(stream)+ return httpx.Response(200, request=request, stream=stream)++ config = ConnectionConfigSync(protocol="http", transport=httpx.MockTransport(handler))+ if isolated:+ adapter = IsolatedFilesystemAdapterSync(+ config, _endpoint(), "12345678-1234-5678-1234-567812345678"+ )+ else:+ adapter = FilesystemAdapterSync(config, _endpoint())++ # The legacy API returns a plain iterator, so callers that stop early+ # never call close(); dropping the iterator has to release the response.+ chunks = adapter.read_bytes_stream("/data.bin", chunk_size=2)+ for _chunk in chunks:+ break+ assert streams[0].closed is False+ del chunks+ gc.collect()+ assert streams[0].closed is True+ adapter._httpx_client.close()++@pytest.mark.asyncio@pytest.mark.parametrize("options", [{"offset": 2}, {"limit": 2}])async def test_async_line_reads_report_unknown_total_size(options) -> None: