Skip to content

Commit 7c40b6c

Browse files
authored
Check if buffering is still happening before flushing (#522)
Fixes #521
1 parent a7f63fd commit 7c40b6c

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/Microsoft.AspNetCore.SystemWebAdapters.CoreServices/RegisterAdapterFeaturesMiddleware.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ public async Task InvokeAsync(HttpContextCore context)
3030
}
3131
finally
3232
{
33-
await context.Features.GetRequiredFeature<IHttpResponseBufferingFeature>().FlushAsync();
33+
// The buffering feature may be removed if the response has ended i.e in usage with YARP
34+
if (context.Features.Get<IHttpResponseBufferingFeature>() is { } buffer)
35+
{
36+
await buffer.FlushAsync();
37+
}
3438
}
3539
}
3640
}

test/Microsoft.AspNetCore.SystemWebAdapters.CoreServices.Tests/ResponseStreamTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,18 @@ public async Task BufferingCanBeDisabledAndFlushesUsingPipe()
378378
Assert.Equal("before after", result);
379379
}
380380

381+
[Fact]
382+
public async Task BufferIsNotFlushedIfNotAvailable()
383+
{
384+
var result = await RunAsync(middleware: (ctx, next) =>
385+
{
386+
ctx.Features.Set<IHttpResponseBufferingFeature>(null);
387+
return next(ctx);
388+
});
389+
390+
Assert.Equal(string.Empty, result);
391+
}
392+
381393
private static Task<string> RunAsync(Action<HttpContext> action, Action<IEndpointConventionBuilder>? builder = null, Func<Http.HttpContext, RequestDelegate, Task>? middleware = null)
382394
=> RunAsync(ctx =>
383395
{

0 commit comments

Comments
 (0)