You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Duplicate response body when running on Lambda and manipulating response body stream in middleware #1185
I have a ASP.NET middleware which temporarily replaces the HTTP response body stream with a memory stream so that it can perform work involving seek operations on the response stream before the response is sent to the client. The code works properly when running locally using Kestrel or the integration tests server, but when running on AWS, all response bodies are duplicated (i.e. {"foo":"bar"} becomes {"foo":"bar"}{"foo":"bar"}).
Expected Behavior
The response should be a valid JSON result on all hosting environments (e.g. {"foo":"bar"}).
Current Behavior
The response is invalid when running on AWS Lambda only (duplicated JSON, e.g. {"foo":"bar"}{"foo":"bar"}).
Reproduction Steps
Simplified middleware code looks like this:
publicasyncTaskInvokeAsync(HttpContextcontext){StreamoriginalResponseBodyStream=context.Response.Body;usingvarbuffer=newMemoryStream();context.Response.Body=buffer;try{awaitthis._next(context);// Do work involving seek on body stream}finally{context.Response.Body.Seek(0,SeekOrigin.Begin);awaitcontext.Response.Body.CopyToAsync(originalResponseBodyStream);context.Response.Body=originalResponseBodyStream;}}
Possible Solution
No response
Additional Information/Context
My ASP.NET application is using the Minimal API hosting pattern. The AWS Lambda hosting mode is set to LambdaEventSource.RestApi.
AWS .NET SDK and/or Package version used
Amazon.Lambda.AspNetCoreServer 7.1.0
Amazon.Lambda.AspNetCoreServer.Hosting 1.1.0
Targeted .NET Platform
.NET 6
Operating System and version
AmazonLinux
Reacted by Ryan Williams and Tanielian V. Barreira
Oh sorry, I made a last-minute change to my reproduction sample and it seems to be related to the buggy behavior! I updated the repo, just deployed the sample, and could reproduce the issue again.
It seems the bug has to do with the following lines:
Fixed in Amazon.Lambda.AspNetCoreServer 10.1.1 (release_2026-05-18). Root cause was the same as #1702: InvokeFeatures.Set<TFeature> did not bump the feature collection revision, so ASP.NET Core kept writing to the original body stream after middleware replaced Response.Body.
Please reopen if it still reproduces on 10.1.1 or later.
Activity
madmox commentedon May 20, 2022
Sample repo to reproduce the issue.
ashishdhingra commentedon May 20, 2022
Hi @madmox,
Good afternoon.
Thanks for sharing the sample code. Unfortunately, I'm unable to reproduce the issue. For reproduction,
Executing
dotnet --versiongives output6.0.202.Please let me know if I'm missing any step.
Thanks,
Ashish
madmox commentedon May 20, 2022
Oh sorry, I made a last-minute change to my reproduction sample and it seems to be related to the buggy behavior! I updated the repo, just deployed the sample, and could reproduce the issue again.
It seems the bug has to do with the following lines:
If I replace this section with the following, it works as expected:
Although
bufferandcontext.Response.Bodyare supposed to be the same object! (context.Response.Body = buffer)dscpinheiro commentedon Oct 20, 2026
Fixed in
Amazon.Lambda.AspNetCoreServer10.1.1 (release_2026-05-18). Root cause was the same as #1702:InvokeFeatures.Set<TFeature>did not bump the feature collection revision, so ASP.NET Core kept writing to the original body stream after middleware replacedResponse.Body.Please reopen if it still reproduces on 10.1.1 or later.