Skip to content

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

fix(csharp): preserve cancellation while buffering downloads

wenmou4e1e6cf985bb
diff --git a/sdks/sandbox/csharp/src/OpenSandbox/Adapters/FilesystemAdapter.cs b/sdks/sandbox/csharp/src/OpenSandbox/Adapters/FilesystemAdapter.cs
index 2501eafe6..fd25d5412 100644
--- a/sdks/sandbox/csharp/src/OpenSandbox/Adapters/FilesystemAdapter.cs
+++ b/sdks/sandbox/csharp/src/OpenSandbox/Adapters/FilesystemAdapter.cs
@@ -219,7 +219,7 @@ internal sealed class FilesystemAdapter : IIdentitySandboxFiles
using var response = await _client.SendWithWakeRetryAsync(
() => BuildDownloadRequest(path, options), cancellationToken, _client.SendTransferAsync).ConfigureAwait(false);
await _client.EnsureSuccessAsync(response, cancellationToken).ConfigureAwait(false);
- var body = await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
+ var body = await HttpClientWrapper.ReadContentBytesAsync(response.Content, cancellationToken).ConfigureAwait(false);
return CreateReadBytesResponse(response, body, options);
}
diff --git a/sdks/sandbox/csharp/src/OpenSandbox/Internal/HttpClientWrapper.cs b/sdks/sandbox/csharp/src/OpenSandbox/Internal/HttpClientWrapper.cs
index 565f18a62..e2adfdbc3 100644
--- a/sdks/sandbox/csharp/src/OpenSandbox/Internal/HttpClientWrapper.cs
+++ b/sdks/sandbox/csharp/src/OpenSandbox/Internal/HttpClientWrapper.cs
@@ -410,23 +410,33 @@ internal sealed class HttpClientWrapper
return await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
}
- private static async Task<byte[]> ReadContentBytesAsync(HttpContent content, CancellationToken cancellationToken)
+ internal static async Task<byte[]> ReadContentBytesAsync(HttpContent content, CancellationToken cancellationToken)
{
- using var source = await content.ReadAsStreamAsync().ConfigureAwait(false);
- // Preallocate from a declared Content-Length so a large download does not pay the
- // doubling growth (and the transient copies it produces) of an unbounded MemoryStream;
- // an unknown or out-of-range length keeps the default growth.
- var declaredLength = content.Headers.ContentLength;
- using var buffer = new MemoryStream(
- declaredLength is > 0 and <= int.MaxValue ? (int)declaredLength.Value : 0);
- var chunk = new byte[81920];
- int read;
- while ((read = await source.ReadAsync(chunk, 0, chunk.Length, cancellationToken).ConfigureAwait(false)) > 0)
+ // Stream acquisition can buffer custom HttpContent before any cancellable read.
+ using var registration = cancellationToken.Register(content.Dispose);
+ try
{
- buffer.Write(chunk, 0, read);
- }
+ using var source = await content.ReadAsStreamAsync().ConfigureAwait(false);
+ cancellationToken.ThrowIfCancellationRequested();
+ // Preallocate from a declared Content-Length so a large download does not pay the
+ // doubling growth (and the transient copies it produces) of an unbounded MemoryStream;
+ // an unknown or out-of-range length keeps the default growth.
+ var declaredLength = content.Headers.ContentLength;
+ using var buffer = new MemoryStream(
+ declaredLength is > 0 and <= int.MaxValue ? (int)declaredLength.Value : 0);
+ var chunk = new byte[81920];
+ int read;
+ while ((read = await source.ReadAsync(chunk, 0, chunk.Length, cancellationToken).ConfigureAwait(false)) > 0)
+ {
+ buffer.Write(chunk, 0, read);
+ }
- return buffer.ToArray();
+ return buffer.ToArray();
+ }
+ catch (Exception) when (cancellationToken.IsCancellationRequested)
+ {
+ throw new OperationCanceledException(cancellationToken);
+ }
}
/// <summary>
diff --git a/sdks/sandbox/csharp/tests/OpenSandbox.Tests/FilesystemAdapterTests.cs b/sdks/sandbox/csharp/tests/OpenSandbox.Tests/FilesystemAdapterTests.cs
index ae352eafc..e8400cbf5 100644
--- a/sdks/sandbox/csharp/tests/OpenSandbox.Tests/FilesystemAdapterTests.cs
+++ b/sdks/sandbox/csharp/tests/OpenSandbox.Tests/FilesystemAdapterTests.cs
@@ -135,6 +135,24 @@ public class FilesystemAdapterTests
}
}
+ [Fact]
+ public async Task ReadFileAsync_ShouldCancelAfterHeaders()
+ {
+ using var content = new StallingContent();
+ var handler = new DownloadHandler();
+ handler.Enqueue(new HttpResponseMessage(HttpStatusCode.OK) { Content = content });
+ using var client = new HttpClient(handler);
+ using var cancellation = new CancellationTokenSource();
+ var adapter = CreateAdapter(client);
+
+ var read = adapter.ReadFileAsync("/data.txt", cancellationToken: cancellation.Token);
+ await content.Started.Task.WaitAsync(TimeSpan.FromSeconds(5));
+ cancellation.Cancel();
+
+ var action = async () => await read.WaitAsync(TimeSpan.FromSeconds(5));
+ await action.Should().ThrowAsync<OperationCanceledException>();
+ }
+
[Fact]
public async Task ListDirectoryAsync_ShouldParseEntryTypeAndSendDepthZero()
{
@@ -330,7 +348,7 @@ public class FilesystemAdapterTests
private static FilesystemAdapter CreateAdapter(HttpClient client)
{
var wrapper = new HttpClientWrapper(client, "http://localhost:8080");
- return new FilesystemAdapter(wrapper, client, "http://localhost:8080", new Dictionary<string, string>());
+ return new FilesystemAdapter(wrapper, "http://localhost:8080", new Dictionary<string, string>());
}
private static HttpResponseMessage DownloadResponse(
@@ -410,4 +428,31 @@ public class FilesystemAdapterTests
base.Dispose(disposing);
}
}
+
+ private sealed class StallingContent : HttpContent
+ {
+ public TaskCompletionSource<bool> Started { get; } = new(TaskCreationOptions.RunContinuationsAsynchronously);
+ private readonly TaskCompletionSource<bool> _release = new(TaskCreationOptions.RunContinuationsAsynchronously);
+
+ protected override Task SerializeToStreamAsync(Stream stream, TransportContext? context) =>
+ SerializeToStreamAsync(stream, context, CancellationToken.None);
+
+ protected override async Task SerializeToStreamAsync(Stream stream, TransportContext? context, CancellationToken cancellationToken)
+ {
+ Started.TrySetResult(true);
+ await _release.Task.WaitAsync(cancellationToken);
+ }
+
+ protected override bool TryComputeLength(out long length)
+ {
+ length = 0;
+ return false;
+ }
+
+ protected override void Dispose(bool disposing)
+ {
+ _release.TrySetResult(true);
+ base.Dispose(disposing);
+ }
+ }
}
diff --git a/sdks/sandbox/python/src/opensandbox/adapters/filesystem_adapter.py b/sdks/sandbox/python/src/opensandbox/adapters/filesystem_adapter.py
index 93d4de72e..d176ebe4b 100644
--- a/sdks/sandbox/python/src/opensandbox/adapters/filesystem_adapter.py
+++ b/sdks/sandbox/python/src/opensandbox/adapters/filesystem_adapter.py
@@ -24,7 +24,7 @@ import json
import logging
import os
import time
-from collections.abc import AsyncIterator
+from collections.abc import AsyncGenerator, AsyncIterator
from io import IOBase, TextIOBase
from typing import TypedDict
@@ -244,17 +244,24 @@ class FilesystemAdapter(Filesystem):
range_header: str | None = None,
offset: int | None = None,
limit: int | None = None,
- ) -> AsyncIterator[bytes]:
+ ) -> AsyncGenerator[bytes, None]:
"""Stream file content as byte chunks."""
- return (
- await self.read_bytes_stream_detailed(
- path,
- chunk_size=chunk_size,
- range_header=range_header,
- offset=offset,
- limit=limit,
- )
- ).body
+ response = await self.read_bytes_stream_detailed(
+ path,
+ chunk_size=chunk_size,
+ range_header=range_header,
+ offset=offset,
+ limit=limit,
+ )
+
+ async def _iter() -> AsyncGenerator[bytes, None]:
+ try:
+ async for chunk in response.body:
+ yield chunk
+ finally:
+ await response.body.aclose()
+
+ return _iter()
async def read_bytes_stream_detailed(
self,
@@ -299,7 +306,7 @@ class FilesystemAdapter(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/src/opensandbox/adapters/isolated_filesystem_adapter.py b/sdks/sandbox/python/src/opensandbox/adapters/isolated_filesystem_adapter.py
index 8df7a2c32..105604701 100644
--- a/sdks/sandbox/python/src/opensandbox/adapters/isolated_filesystem_adapter.py
+++ b/sdks/sandbox/python/src/opensandbox/adapters/isolated_filesystem_adapter.py
@@ -21,7 +21,7 @@ Uses auto-generated isolated_execution API modules with explicit session_id.
import json
import logging
-from collections.abc import AsyncIterator
+from collections.abc import AsyncGenerator
from io import IOBase, TextIOBase
from uuid import UUID
@@ -175,16 +175,23 @@ class IsolatedFilesystemAdapter(Filesystem):
range_header: str | None = None,
offset: int | None = None,
limit: int | None = None,
- ) -> AsyncIterator[bytes]:
- return (
- await self.read_bytes_stream_detailed(
- path,
- chunk_size=chunk_size,
- range_header=range_header,
- offset=offset,
- limit=limit,
- )
- ).body
+ ) -> AsyncGenerator[bytes, None]:
+ response = await self.read_bytes_stream_detailed(
+ path,
+ chunk_size=chunk_size,
+ range_header=range_header,
+ offset=offset,
+ limit=limit,
+ )
+
+ async def _iter() -> AsyncGenerator[bytes, None]:
+ try:
+ async for chunk in response.body:
+ yield chunk
+ finally:
+ await response.body.aclose()
+
+ return _iter()
async def read_bytes_stream_detailed(
self,