Skip to content

Secure, Fast, and Extensible Sandbox runtime for AI agents.

fix(python): close download streams through their responses

wenmou5ce4c8b1a294
diff --git a/sdks/sandbox/python/src/opensandbox/adapters/download_response.py b/sdks/sandbox/python/src/opensandbox/adapters/download_response.py
index 9610186c6..fc9202b4d 100644
--- a/sdks/sandbox/python/src/opensandbox/adapters/download_response.py
+++ b/sdks/sandbox/python/src/opensandbox/adapters/download_response.py
@@ -63,11 +63,10 @@ def create_read_bytes_response(
if response.status_code == 206
else None
)
- total_size = (
- (content_range.total if content_range is not None else -1)
- if response.status_code == 206
- else (-1 if line_based else content_length)
- )
+ if response.status_code == 206:
+ total_size = content_range.total if content_range is not None else -1
+ else:
+ total_size = -1 if line_based else content_length
return ReadBytesResponse(
body=body,
@@ -94,20 +93,12 @@ class _AsyncResponseByteStream:
raise StopAsyncIteration
try:
return await self._iterator.__anext__()
- except StopAsyncIteration:
- await self.aclose()
- raise
except BaseException:
await self.aclose()
raise
async def aclose(self) -> None:
- if self._closed:
- return
self._closed = True
- close_iterator = getattr(self._iterator, "aclose", None)
- if close_iterator is not None:
- await close_iterator()
await self._response.aclose()
async def __aenter__(self) -> "_AsyncResponseByteStream":
@@ -136,20 +127,12 @@ class _ResponseByteStream:
raise StopIteration
try:
return next(self._iterator)
- except StopIteration:
- self.close()
- raise
except BaseException:
self.close()
raise
def close(self) -> None:
- if self._closed:
- return
self._closed = True
- close_iterator = getattr(self._iterator, "close", None)
- if close_iterator is not None:
- close_iterator()
self._response.close()
def __enter__(self) -> "_ResponseByteStream":
@@ -170,7 +153,5 @@ def iter_async_response_bytes(
return _AsyncResponseByteStream(response, chunk_size)
-def iter_response_bytes(
- response: httpx.Response, chunk_size: int
-) -> ReadBytesStream:
+def iter_response_bytes(response: httpx.Response, chunk_size: int) -> ReadBytesStream:
return _ResponseByteStream(response, chunk_size)
diff --git a/sdks/sandbox/python/src/opensandbox/adapters/filesystem_adapter.py b/sdks/sandbox/python/src/opensandbox/adapters/filesystem_adapter.py
index d176ebe4b..c5dde5272 100644
--- a/sdks/sandbox/python/src/opensandbox/adapters/filesystem_adapter.py
+++ b/sdks/sandbox/python/src/opensandbox/adapters/filesystem_adapter.py
@@ -197,7 +197,7 @@ class FilesystemAdapter(Filesystem):
offset: int | None = None,
limit: int | None = None,
) -> ReadBytesResponse[bytes]:
- """Read file content as bytes with support for range and line-based requests.
+ """Read file bytes and HTTP response metadata.
Args:
path: Path to the file to read
@@ -209,7 +209,7 @@ class FilesystemAdapter(Filesystem):
Mutually exclusive with range_header.
Returns:
- File content as bytes
+ File bytes together with HTTP status and range metadata
Raises:
SandboxApiException: If the read operation fails
diff --git a/sdks/sandbox/python/src/opensandbox/adapters/isolated_filesystem_adapter.py b/sdks/sandbox/python/src/opensandbox/adapters/isolated_filesystem_adapter.py
index 105604701..0c9dc4a55 100644
--- a/sdks/sandbox/python/src/opensandbox/adapters/isolated_filesystem_adapter.py
+++ b/sdks/sandbox/python/src/opensandbox/adapters/isolated_filesystem_adapter.py
@@ -230,7 +230,7 @@ class IsolatedFilesystemAdapter(Filesystem):
)
return create_read_bytes_response(
response,
- iter_async_response_bytes(response, chunk_size),
+ iter_async_response_bytes(response, chunk_size),
line_based=offset is not None or limit is not None,
)
except Exception as e:
diff --git a/sdks/sandbox/python/tests/test_filesystem_download_response.py b/sdks/sandbox/python/tests/test_filesystem_download_response.py
index 6cbd3b31f..dfe0b08cb 100644
--- a/sdks/sandbox/python/tests/test_filesystem_download_response.py
+++ b/sdks/sandbox/python/tests/test_filesystem_download_response.py
@@ -14,15 +14,19 @@
# limitations under the License.
#
+import asyncio
+
import httpx
import pytest
from opensandbox.adapters.filesystem_adapter import FilesystemAdapter
from opensandbox.config import ConnectionConfig
from opensandbox.config.connection_sync import ConnectionConfigSync
-from opensandbox.models import ByteRange, ReadBytesResponse
+from opensandbox.models import ByteRange
from opensandbox.models.sandboxes import SandboxEndpoint
+from opensandbox.services import DownloadFilesystem
from opensandbox.sync.adapters.filesystem_adapter import FilesystemAdapterSync
+from opensandbox.sync.services import DownloadFilesystemSync
def _endpoint() -> SandboxEndpoint:
@@ -51,7 +55,7 @@ async def test_async_read_bytes_detailed_exposes_partial_metadata() -> None:
)
response = await adapter.read_bytes_detailed("/data.bin", range_header="bytes=0-4")
- assert isinstance(response, ReadBytesResponse)
+ assert isinstance(adapter, DownloadFilesystem)
assert response.body == b"hello"
assert response.status_code == 206
assert response.is_partial is True
@@ -66,19 +70,24 @@ async def test_async_read_bytes_detailed_exposes_partial_metadata() -> None:
@pytest.mark.asyncio
-async def test_async_read_bytes_detailed_identifies_ignored_and_invalid_ranges() -> (
- None
-):
- ranges = iter([None, "garbage", "bytes 5-9/*"])
-
+ ("status", "raw", "expected_range", "total_size"),
+ [
+ (200, None, None, 5),
+ (206, None, None, -1),
+ (206, "garbage", ByteRange(-1, -1, -1, "garbage"), -1),
+ (206, "bytes 5-9/*", ByteRange(5, 9, -1, "bytes 5-9/*"), -1),
+ ],
+)
+async def test_async_read_bytes_detailed_handles_range_responses(
+ status, raw, expected_range, total_size
+) -> None:
def handler(request: httpx.Request) -> httpx.Response:
- content_range = next(ranges)
- status = 200 if content_range is None else 206
- headers = {"Content-Length": "10"}
- if content_range is not None:
- headers["Content-Range"] = content_range
return httpx.Response(
- status, request=request, content=b"0123456789", headers=headers
+ status,
+ request=request,
+ content=b"hello",
+ headers={"Content-Range": raw} if raw is not None else {},
)
adapter = FilesystemAdapter(
@@ -86,36 +95,10 @@ async def test_async_read_bytes_detailed_identifies_ignored_and_invalid_ranges()
_endpoint(),
)
- ignored = await adapter.read_bytes_detailed("/data.bin", range_header="bytes=5-")
- assert ignored.is_partial is False
- assert ignored.content_range is None
- assert ignored.total_size == 10
-
- malformed = await adapter.read_bytes_detailed("/data.bin")
- assert malformed.content_range == ByteRange(-1, -1, -1, "garbage")
- assert malformed.total_size == -1
-
- unknown = await adapter.read_bytes_detailed("/data.bin")
- assert unknown.content_range == ByteRange(5, 9, -1, "bytes 5-9/*")
- assert unknown.total_size == -1
- await adapter._httpx_client.aclose()
-
-
-async def test_async_read_bytes_detailed_identifies_partial_without_range() -> None:
- def handler(request: httpx.Request) -> httpx.Response:
- return httpx.Response(206, request=request, content=b"hello")
-
- adapter = FilesystemAdapter(
- ConnectionConfig(protocol="http", transport=httpx.MockTransport(handler)),
- _endpoint(),
- )
-
- response = await adapter.read_bytes_detailed("/data.bin")
-
- assert response.is_partial is True
- assert response.content_range is None
- assert response.total_size == -1
+ response = await adapter.read_bytes_detailed("/data.bin", range_header="bytes=5-")
+ assert response.is_partial is (status == 206)
+ assert response.content_range == expected_range
+ assert response.total_size == total_size
await adapter._httpx_client.aclose()
@@ -171,6 +154,50 @@ async def test_async_detailed_and_legacy_streams_close_responses() -> None:
await adapter._httpx_client.aclose()
+async def test_async_detailed_stream_can_close_during_a_read() -> None:
+ reading = asyncio.Event()
+ release = asyncio.Event()
+
+ class Stream(_TrackingAsyncStream):
+ async def __aiter__(self):
+ yield b"a"
+ reading.set()
+ await release.wait()
+ yield b"b"
+
+ source = Stream()
+ response = httpx.Response(200, stream=source)
+ adapter = FilesystemAdapter(
+ ConnectionConfig(
+ protocol="http", transport=httpx.MockTransport(lambda _: response)
+ ),
+ _endpoint(),
+ )
+ pending = None
+ try:
+ download = await adapter.read_bytes_stream_detailed("/data.bin", chunk_size=1)
+ assert await anext(download.body) == b"a"
+ pending = asyncio.create_task(anext(download.body))
+ await asyncio.wait_for(reading.wait(), timeout=2)
+
+ await asyncio.wait_for(download.body.aclose(), timeout=2)
+ assert response.is_closed
+ assert source.closed
+
+ release.set()
+ await pending
+ await download.body.aclose()
+ with pytest.raises(StopAsyncIteration):
+ await anext(download.body)
+ finally:
+ release.set()
+ if pending is not None:
+ await pending
+ await response.aclose()
+ await adapter._httpx_client.aclose()
+
+
def test_sync_detailed_and_legacy_reads_preserve_behavior() -> None:
def handler(request: httpx.Request) -> httpx.Response:
return httpx.Response(
@@ -186,6 +213,7 @@ def test_sync_detailed_and_legacy_reads_preserve_behavior() -> None:
)
detailed = adapter.read_bytes_detailed("/data.bin")
+ assert isinstance(adapter, DownloadFilesystemSync)
assert detailed.body == b"hello"
assert detailed.content_range == ByteRange(0, 4, 10, "bytes 0-4/10")
assert adapter.read_bytes("/data.bin") == b"hello"