Skip to content

Commit 8dff353

Browse files
CopilotMihaZupanCopilot
authored
Fix cancellation token handling in LimitArrayPoolWriteStream.WriteAsync methods (#121836)
Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: MihaZupan <[email protected]> Co-authored-by: Miha Zupan <[email protected]> Co-authored-by: Copilot <[email protected]>
1 parent 09e0e72 commit 8dff353

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

src/libraries/System.Net.Http/src/System/Net/Http/HttpContent.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,12 +1103,22 @@ public override void Write(byte[] buffer, int offset, int count)
11031103

11041104
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
11051105
{
1106+
if (cancellationToken.IsCancellationRequested)
1107+
{
1108+
return Task.FromCanceled(cancellationToken);
1109+
}
1110+
11061111
Write(buffer, offset, count);
11071112
return Task.CompletedTask;
11081113
}
11091114

11101115
public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default)
11111116
{
1117+
if (cancellationToken.IsCancellationRequested)
1118+
{
1119+
return ValueTask.FromCanceled(cancellationToken);
1120+
}
1121+
11121122
Write(buffer.Span);
11131123
return default;
11141124
}

src/libraries/System.Net.Http/tests/FunctionalTests/HttpClientTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1437,7 +1437,7 @@ private static async Task<T> WhenCanceled<T>(CancellationToken cancellationToken
14371437
return default(T);
14381438
}
14391439

1440-
private sealed class CustomResponseHandler : HttpMessageHandler
1440+
internal sealed class CustomResponseHandler : HttpMessageHandler
14411441
{
14421442
private readonly Func<HttpRequestMessage, CancellationToken, Task<HttpResponseMessage>> _func;
14431443

src/libraries/System.Net.Http/tests/FunctionalTests/HttpContentTest.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,24 @@ await server.AcceptConnectionAsync(async connection =>
596596
});
597597
}
598598

599+
[Fact]
600+
public async Task GetAsync_WithCustomHandlerReturningStringContent_PreCanceledToken_ThrowsTaskCanceledException()
601+
{
602+
// Regression test for https://github.com/dotnet/runtime/issues/121810
603+
// Ensures that when using a custom handler that returns pre-buffered content (StringContent),
604+
// calling GetAsync with a pre-canceled token properly throws TaskCanceledException
605+
using var handler = new HttpClientTest.CustomResponseHandler((req, ct) => Task.FromResult(new HttpResponseMessage()
606+
{
607+
Content = new StringContent("Hello from CustomHandler")
608+
}));
609+
using var httpClient = new HttpClient(handler);
610+
var cts = new CancellationTokenSource();
611+
cts.Cancel();
612+
613+
await Assert.ThrowsAsync<TaskCanceledException>(() =>
614+
httpClient.GetAsync("https://example.com", cts.Token));
615+
}
616+
599617
[Theory]
600618
[InlineData(true)]
601619
[InlineData(false)]

0 commit comments

Comments
 (0)