Secure, Fast, and Extensible Sandbox runtime for AI agents.
fix(sdk-python): release an async download closed before its first chunk
leoca52f2f8306fc4
diff --git a/sdks/sandbox/python/src/opensandbox/adapters/filesystem_adapter.py b/sdks/sandbox/python/src/opensandbox/adapters/filesystem_adapter.pyindex c5dde5272..3f3645256 100644--- a/sdks/sandbox/python/src/opensandbox/adapters/filesystem_adapter.py+++ b/sdks/sandbox/python/src/opensandbox/adapters/filesystem_adapter.py@@ -256,12 +256,17 @@ class FilesystemAdapter(Filesystem):async def _iter() -> AsyncGenerator[bytes, None]:try:+ yield b""async for chunk in response.body:yield chunkfinally:await response.body.aclose()- return _iter()+ # Start the generator so its finally block also runs when it is+ # closed or dropped before the caller reads the first chunk.+ chunks = _iter()+ await anext(chunks)+ return chunksasync def read_bytes_stream_detailed(self,diff --git a/sdks/sandbox/python/src/opensandbox/adapters/isolated_filesystem_adapter.py b/sdks/sandbox/python/src/opensandbox/adapters/isolated_filesystem_adapter.pyindex 0c9dc4a55..828abf33f 100644--- a/sdks/sandbox/python/src/opensandbox/adapters/isolated_filesystem_adapter.py+++ b/sdks/sandbox/python/src/opensandbox/adapters/isolated_filesystem_adapter.py@@ -186,12 +186,17 @@ class IsolatedFilesystemAdapter(Filesystem):async def _iter() -> AsyncGenerator[bytes, None]:try:+ yield b""async for chunk in response.body:yield chunkfinally:await response.body.aclose()- return _iter()+ # Start the generator so its finally block also runs when it is+ # closed or dropped before the caller reads the first chunk.+ chunks = _iter()+ await anext(chunks)+ return chunksasync def read_bytes_stream_detailed(self,diff --git a/sdks/sandbox/python/tests/test_filesystem_download_close_unread.py b/sdks/sandbox/python/tests/test_filesystem_download_close_unread.pynew file mode 100644index 000000000..d1116909f--- /dev/null+++ b/sdks/sandbox/python/tests/test_filesystem_download_close_unread.py@@ -0,0 +1,61 @@+# Copyright 2026 The OpenSandbox Authors+#+# Licensed under the Apache License, Version 2.0 (the "License");+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an "AS IS" BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.++"""An async download closed before its first chunk must release its response."""++from collections.abc import AsyncIterator+from contextlib import aclosing++import httpx+import pytest++from opensandbox.adapters.filesystem_adapter import FilesystemAdapter+from opensandbox.adapters.isolated_filesystem_adapter import IsolatedFilesystemAdapter+from opensandbox.config import ConnectionConfig+from opensandbox.models.sandboxes import SandboxEndpoint+++class _DownloadStream(httpx.AsyncByteStream):+ def __init__(self) -> None:+ self.closed = False++ async def __aiter__(self) -> AsyncIterator[bytes]:+ yield b"abcd"++ async def aclose(self) -> None:+ self.closed = True++[email protected]("kind", ["sandbox", "isolated"])+async def test_download_closed_before_first_chunk_releases_response(kind: str) -> None:+ source = _DownloadStream()+ response = httpx.Response(200, stream=source)+ config = ConnectionConfig(protocol="http", transport=httpx.MockTransport(lambda _: response))+ endpoint = SandboxEndpoint(endpoint="localhost:44772")+ if kind == "isolated":+ adapter = IsolatedFilesystemAdapter(+ config, endpoint, "12345678-1234-5678-1234-567812345678"+ )+ else:+ adapter = FilesystemAdapter(config, endpoint)+ try:+ chunks = await adapter.read_bytes_stream("/tmp/file")+ async with aclosing(chunks):+ pass+ assert source.closed+ assert response.is_closed+ finally:+ await response.aclose()+ await adapter._httpx_client.aclose()