Skip to content

Commit 1ec64b2

Browse files
authored
Fix SSE client cancellation (#9503)
1 parent 6e6a326 commit 1ec64b2

3 files changed

Lines changed: 10 additions & 5 deletions

File tree

src/HotChocolate/AspNetCore/src/Transport.Http/GraphQLHttpResponse.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ public IAsyncEnumerable<OperationResult> ReadAsResultStreamAsync(CancellationTok
165165

166166
if (contentType?.MediaType.EqualsOrdinal(ContentType.EventStream) ?? false)
167167
{
168-
return new SseReader(_message);
168+
return new SseReader(_message, cancellationToken);
169169
}
170170

171171
// The server supports the newer graphql-response+json media type and users are free

src/HotChocolate/AspNetCore/src/Transport.Http/Sse/SseReader.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77
namespace HotChocolate.Transport.Http;
88

9-
internal class SseReader(HttpResponseMessage message) : IAsyncEnumerable<OperationResult>
9+
internal class SseReader(
10+
HttpResponseMessage message,
11+
CancellationToken requestCancellationToken = default) : IAsyncEnumerable<OperationResult>
1012
{
1113
private static readonly StreamPipeReaderOptions s_options = new(
1214
pool: MemoryPool<byte>.Shared,
@@ -18,8 +20,11 @@ internal class SseReader(HttpResponseMessage message) : IAsyncEnumerable<Operati
1820
public async IAsyncEnumerator<OperationResult> GetAsyncEnumerator(
1921
CancellationToken cancellationToken = default)
2022
{
21-
using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
22-
await using var stream = await message.Content.ReadAsStreamAsync(cts.Token);
23+
using var cts = CancellationTokenSource.CreateLinkedTokenSource(
24+
requestCancellationToken,
25+
cancellationToken);
26+
await using var stream =
27+
await message.Content.ReadAsStreamAsync(cts.Token).ConfigureAwait(false);
2328
using var eventBuffer = new PooledArrayWriter();
2429
var reader = PipeReader.Create(stream, s_options);
2530

src/HotChocolate/AspNetCore/test/Transport.Http.Tests/GraphQLHttpClientTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,7 @@ mutation CreateReviewForEpisode(
713713
var canceled = false;
714714
try
715715
{
716-
await foreach (var result in subscriptionResponse.ReadAsResultStreamAsync().WithCancellation(cts.Token))
716+
await foreach (var result in subscriptionResponse.ReadAsResultStreamAsync(cts.Token))
717717
{
718718
result.MatchInlineSnapshot(
719719
"""

0 commit comments

Comments
 (0)