Skip to content

Return 502 when the ASP.NET Core response exceeds the Lambda payload limit - #2620

Closed
pkryusuf wants to merge 3 commits into
aws:masterfrom
pkryusuf:fix-1815-response-too-large
Closed

pkryusuf wants to merge 3 commits into
aws:masterfrom
pkryusuf:fix-1815-response-too-large

Conversation

@pkryusuf

@pkryusuf pkryusuf commented Nov 21, 2026 •

Copy link
Copy Markdown

Issue #, if available: #1815

Description of changes:

Following the approach suggested in the issue: when the marshalled response body is larger than the 6 MB Lambda payload limit, APIGatewayProxyFunction and APIGatewayHttpApiV2ProxyFunction now return a 502 (and log an error) instead of the invocation failing with an opaque error. The check runs in MarshallResponse before PostMarshallResponseFeature, so users can still customize the 502 response.

  • Utilities.IsResponseBodyTooLarge: the body is measured as it is written into the response JSON (escaped with JavaScriptEncoder.UnsafeRelaxedJsonEscaping, counted in UTF-8 bytes), against 6 MB minus a fixed 16 KB ResponseEnvelopeHeadroom for the status code, headers and property names.
  • Change file added for Amazon.Lambda.AspNetCoreServer (patch).
  • Tests: unit tests for the limit boundary (text, base64, multi-byte UTF-8, null) and end-to-end tests for REST and HTTP API v2 using a new 7 MB LargeResponseController in TestWebApp. All 239 tests in Amazon.Lambda.AspNetCoreServer.Test pass on net8.0 and net10.0.

Notes for review:

  • 502 rather than 413, as suggested in review: the function produced an invalid response, so it is a server-side error.
  • Headers are covered by the 16 KB headroom, not measured, so a response with more headers than that can still fail the old way.
  • ALB (1 MB limit) and response streaming are unchanged; happy to add ALB if wanted.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@pkryusuf
pkryusuf requested review from a team as code owners November 21, 2026 18:11
@boblodgett

Copy link
Copy Markdown
Contributor

Repro from #1815 is fixed.

However, as you mentioned, the check measures the raw body rather than the payload Lambda measures, so it can still go over so #1815 isn't completely fixed. Seems like we'd need the exact encoded body count plus a fixed headroom constant.

@pkryusuf

Copy link
Copy Markdown
Author

Thanks for testing it! I pushed a change that does that:

  • The body is now measured as it is written into the response JSON: escaped with JavaScriptEncoder.UnsafeRelaxedJsonEscaping, like the Lambda serializers, and counted in UTF-8 bytes. Short bodies skip the escaping pass (a character is at most 6 bytes escaped), so the extra work only happens for large responses.
  • A fixed ResponseEnvelopeHeadroom of 16 KB is kept for the status code, headers and property names, so the body limit is 6 MB minus 16 KB.
  • New tests cover quotes, backslashes, newlines and \u0001, and one serializes the largest allowed body (mixed with escaped characters) through DefaultLambdaJsonSerializer and checks the whole response stays under 6 MB.

Headers are not measured, so a response with more than about 16 KB of headers could still go over; I can count them exactly too if you prefer that over a constant.

@normj

normj commented Nov 25, 2026

Copy link
Copy Markdown
Member

I don't think HTTP status code 413 makes sense because that is supposed to be for a request being too large not a response. That means it was a client error for sending something too large. In this case it is a server error with the server, in this case Lambda, creating an invalid response. I understand your desire to differentiate an error that is due to a response too large but from the HTTP spec point of view 502 fits.

[server error response](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status#server_error_responses) status code indicates that a server was acting as a gateway or [proxy](https://developer.mozilla.org/en-US/docs/Glossary/Proxy_server) and that it received an invalid response from the upstream server.

In this case API Gateway or ALB received an invalid response from the upstream server which is the Lambda function.

@pkryusuf pkryusuf changed the title Return 413 when the ASP.NET Core response exceeds the Lambda payload limit Return 502 when the ASP.NET Core response exceeds the Lambda payload limit Nov 25, 2026
@pkryusuf

Copy link
Copy Markdown
Author

Agreed, the function is the one producing the invalid response, so 502 is the right code. I switched both APIGatewayProxyFunction and APIGatewayHttpApiV2ProxyFunction to return 502 (log message, tests and change file updated too); PostMarshallResponseFeature still runs after it, so the response can be customized. All 239 tests in Amazon.Lambda.AspNetCoreServer.Test pass.

@normj

normj commented Nov 25, 2026

Copy link
Copy Markdown
Member

Since we are throwing a 502 isn't that what API Gateway and ALB do by default? So in that I don't think we should do anything in this library. Sorry it sounds like we should have done better research on the issue along time ago and closed as work as expected.

@pkryusuf

Copy link
Copy Markdown
Author

That makes sense. With 502 the client sees the same status API Gateway and ALB already return, and RuntimeSupport already surfaces the Runtime API's 413 as an error in the logs, so the only thing this PR would add is a hook to customize the response in PostMarshallResponseFeature, which isn't worth the extra size check on every response. I'll close this PR; thanks to you and @boblodgett for taking the time to look at it.

@pkryusuf pkryusuf closed this Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants